- 浏览: 231740 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
u012414590:
博主,我想问一下,webdav能否集成到自己的服务器上,打个比 ...
WebDav 测试开发笔记 -
icarusliu:
java1997 写道博主,你好,正好用到这个,能不能贴个完整 ...
关于JTextPane中进行搜索结果显示的问题及解决 -
java1997:
博主,你好,正好用到这个,能不能贴个完整的使用代码啊,谢谢
关于JTextPane中进行搜索结果显示的问题及解决 -
RangerWolf:
非常感谢啊!
wsdl文档结构 -
chenk008:
Tomcat的类加载机制
配置
1. Web.xml
a. ContextLoaderListener:
配置该选项后,服务器将会自动去WEB-INF目录下面查找applicationContext.xml文件来加载Spring的相关配置;也可以使用context-param来指定其它位置,如classpath,表示的是在源文件目录下去查找applicationContext.xml文件。当然也可以指定其它名称的配置文件,甚至可以同时加载多个配置文件(当配置的Bean很多的时候,很有必要将不同作用的Bean分别在不同的配置文件中进行配置,在这种情况下多配置文件就很有用处)。
b. Context-param:
在指定的位置去查找Spring的相关配置
c. Servlet与Servlet-mapping:
众所周知,在WEB开发中,Servlet用于处理浏览器的请求;因此,如果需要Spring来处理Servlet请求,就需要进行相关的配置,来让Spring加载与MVC相关的Bean。
当配置完这两个选项后,有新的浏览器请求到来时,如果发现请求格式符合要求(如接下来的配置文件中配置的以.do结束的请求,当然也可以配置成其它名称),就会跳转到Spring的dispatcherServlet中进行统一处理,然后由其寻找到合适的Controller中进行处理。也就是说,dispatcherServlet相当于一个转发器,将所有交给Spring处理的请求正确的分发到不同的Controller中去。
当配置springmvc的servlet及servlet-mapping后,服务器启动时将会去WEB-INF目录下寻找MVC的配置文件进行加载。寻找的规则就是配置的servlet的名称 + -servlet.xml,如配置的名称一般是springmvc,则查找的配置文件的名称就会是springmvc-servlet.xml。
具体的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
当然,也可以将所有的MVC配置项都配置在同一个配置文件applicationContext.xml中去:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
2. ApplicationContext.xml
a. Context:annotation-config
表明Spring是基于注解来进行配置的
b. Context:cpmponent-scan
指定需要扫描的包,也可以指定相关的过滤条件来指定需要装载的或者不需要装载的Bean。
Spring将会去所配置的包中扫描那些被注解的Bean,如@Controller/@Service等注解的Class。注意一般在applicationContext.xml中将Controller的注解忽略,而是在MVC的配置文件中单独进行Controller相关的扫描装配。
这样做的好处就是,用户可以配置多个DispatcherServlet,每个对应于一个配置文件,相互之间配置的Controller不被干扰。这样可以方便地将不同作用的Controller分组存放,修改与更新不影响其它组的Controller。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config/> <context:component-scan base-package="com.liuqi.web.webtest"> <context:exclude-filter type="annotation" expression="org.springframework.web.servlet.mvc.Controller"/> </context:component-scan> </beans>
3. Springmvc-servlet.xml
a. Context:annotation-config与applicationContext.xml中的配置意义是一样的。
b. viewResolver
指定视图渲染器,以一个示例来说明其作用:
@RequestMapping("/aaa") public String test() { System.out.println("The test controller!"); return "test"; }
如上述代码,其返回的是一个test的字符串,Spring将会根据viewResolver中配置的相关项来进行组合,加上前缀与后缀,最终得到/WEB-INF/jsp/test.jsp这样一个地址,因此,该Servlet返回后浏览器将会跳转到该页面去。
因此,viewResolver的作用就是指定Servlet返回时跳转到的页面的前缀与后缀。
需要注意的是,在配置文件中指定的前缀是/web-inf/jsp/,我们知道这个目录下的东西是无法直接通过输入地址来访问的,这样也能够在一定的程度上保证网站的安全性:在这个目录下的页面只能通过Servlet来进行访问,这就保证了所有对页面的该都能在我们的监控下进行,当然再需要添加其它的功能如访问日志的记录等就要简单的多。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config/> <context:component-scan base-package="com.liuqi.web.webtest"> <context:include-filter type="annotation" expression="org.springframework.web.servlet.mvc.Controller"/> </context:component-scan> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"/> </bean> </beans>
4. 完整的配置文件就这样,以下是一个相关的Controller
package com.liuqi.web.webtest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("test") public class ControllerTest { @RequestMapping("/aaa") public String test() { System.out.println("The test controller!"); return "test"; } }
以下对Controller的编写进行说明:
1) requestMapping:用于指定该Controller的请求路径;有两种用法:
a. 用于类文件头
b. 用于各个方法上
用于方法上时,如果此时未在类上进行相关指定,表明这个方法将会处理指定名称+.do的请求,如aaa.do的请求;如类上指定的RequestMapping,如指定的名称是test,那么这个方法将会处理/test/aaa.do的请求。
因此,用于类上时,将会为该类里面所有处理请求的Controller方法添加一个共同的前缀——这跟文件夹类似,所有同一类的Controller,都可以包含一个相同的目录与一个不同的名称,这样这一类的Controller就可以与其它类的Controller区别开来。
2) 在方法中指定参数
在方法中可以指定多个参数,如HttpServletRequest、HttpServletResponse、HttpSession等。可以指定其中一个或者多个或者0个。
@RequestMapping("/bbb") public String bbb(HttpServletRequest request, HttpServletResponse response, PrintWriter out, HttpSession session) { System.out.println("The test of bbb!"); return "bbb"; }
3) 建立Ajax的Controller
那么怎么处理Ajax请求呢?使用返回类型为void类型的方法即可:
@RequestMapping("/c") public void cajax(PrintWriter out) { }
4) 传递参数
另外,还有一点很重要的,如何向Controller传递参数?主要有两种方式:
a. 比较传统的方式:使用HttpServletRequest来传递参数,也就是说在URL中直接附带参数:
@RequestMapping("/bbb") public String bbb(HttpServletRequest request, HttpServletResponse response, PrintWriter out, HttpSession session) { String temp = request.getParameter("a"); System.out.println(temp); System.out.println("The test of bbb!"); return "bbb"; }
访问时,可以使用以下地址进行访问:
http://localhost:8080/webtest/test/bbb.do?a=test
b. RequestParam
@RequestMapping("/aaa") public String test(@RequestParam String test) { System.out.println("The test controller!"); System.out.println(test); return "test"; }
访问地址:
http://localhost:8080/webtest/test/aaa.do?test=test
c. PathVariable
@RequestMapping("/{id}/{p1}/{p2}/c") public void cajax(PrintWriter out, @PathVariable String id, @PathVariable String p1, @PathVariable String p2) { System.out.println(id); System.out.println(p1); System.out.println(p2); }
访问地址:
http://localhost:8080/webtest/test/test/test1/test2/c.do
d. 直接使用参数:该方式是最为简单的一种方式,但缺点就是不能获取get方式传递的参数。
@RequestMapping("/d") public void test(int id, String name) { System.out.println(id); }
完整示例见附件,运行时需要自己添加上Spring所使用的那些包。
- springtest.rar (73.8 KB)
- 下载次数: 2
发表评论
-
使用sitemesh包含velocity生成的静态页面乱码解决
2010-12-18 19:14 1301在applicationContext.xml中的视图解析器中 ... -
在spring当中启用hibernate
2010-01-08 09:56 929有两种方式: 1. 在springContext.xml当中 ... -
一个spring security拦截器的配置
2010-01-06 10:49 3407首先肯定是要将spring security的包下载并包含在项 ... -
spring配置探索2
2010-01-05 11:29 800一 单独的spring配置实例 1. 在web.xml当中配 ... -
spring的配置
2010-01-05 10:30 89334.1.3 Spring的web.xml配置 和其他We ... -
spring学习
2009-07-16 10:46 7181.关于依赖注入(控制反转): 在依赖注入的模式下,创建 ... -
hibernate学习
2009-07-16 10:29 7151.Session的取得: Configura ... -
hibernate的helloworld
2009-07-16 10:03 2329利用hibernate in action来学习hiberna ... -
spring的字符过滤器的使用
2008-10-20 08:55 1219web.xml中加以下代码 xml 代码 <fi ...
相关推荐
Spring boot + Spring Security 简单配置,数据库采用MongoDB
spring+springmvc+mybatis 简单实例 实现简单的界面 主要包含各种配置
spring简单实例,一看就懂,介绍spring基本配置
Spring综合实例 spring配置oracel数据库 事务管理 切面 业务描述:实现用户简单的注册登录功能。 用户注册时候需要输入用户名,密码,邮箱地址,点击注册后成功。 用户登录时可以选择使用用户名或者邮箱地址进行...
基于Java类的配置方法和基于XML或基于注解的配置方式相比,前者通过代码的方式更加灵活地实现Bean的实例化及Bean之间的装配,但后面两者都是通过配置声明的方式,在灵活性上要稍逊一些,但是配置上要更简单一些。
这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...
简单实用struts+spring+hibernate配置实例--.rar 配置简单清晰 最适合初学着学习
找了好多框架搭建的资料,总觉得不是自己想要的东西,感觉心理上有恐惧感,始终不敢自己搭,现在终于搭了一个,至于原理什么的,我自己也还木有搞清楚,不过感觉还是可以用,接下来再来继续完善,呵呵,请朋友们批评...
使用springSecurity3开发的一个简单例子,登录使用sql方式控制,权限使用AOP的方式配置,类似于声明式事务的配置!
Quartz spring配置 简单例子,
Eclipse Spring Boot maven web demo 项目简单实例。 做了Spring Boot maven web配置,可以拿来做初始项目。
spring+mybatis项目简单例子,适合初学者,数据库为mysql, 里面有sql文件,主要配置都有,没有分页,没有详细的代码,只有基础框架
直接看使用说明,只需要配置mysql的数据库即可运行代码 0.1、eclipse配置grandle插件 0.2、eclipse配置lambok插件 1、配置好application-db.properties即可 2、直接运行DemoApplication.java 3、浏览器输入...
struts+spring+ibatis+mysql简单的配置实例
本压缩包包含spring两个简单登录案例(注解+配置+文档),内容是springJDBC+属性注入,注解注入,SpringMVC两个例子分别两个源码两个文档。自己写的一个小例子。
用于webservice的开发,主要用到spring+cxf的方式实现webservice的发布与简单调用测试,不懂得可以留言给我,比较显新的框架配置,希望你会喜欢!(并包含拦截器的配置!)
关于iabtis spring struts2 的简单登录实例。已经完全配置完全。只要创建数据库就行。
spring 与rmi 的整合 1.首先编写接口 interface : 这里的接口要继承Remote, 它是一个标识接口 2.编写实现类 3.编写main方法用于启动 service 4 配置spring文件 编写客户端测试类
Spring 集 成ActiveMQ 配置 异步RPC框架 Missian ActiveMq-JMS简单实例使用tomcat