一、基本使用,获取提交的参数
后端代码:
- @RequestMapping("testRequestParam")
- public String filesUpload(@RequestParam String inputStr, HttpServletRequest request) {
- System.out.println(inputStr);
- int inputInt = Integer.valueOf(request.getParameter("inputInt"));
- System.out.println(inputInt);
- // ......省略
- return "index";
- }
前端代码:
- <form action="/gadget/testRequestParam" method="post">
- 参数inputStr:<input type="text" name="inputStr">
- 参数intputInt:<input type="text" name="inputInt">
- </form>
前端界面:
执行结果:
test1
123
可以看到spring会自动根据参数名字封装进入,我们可以直接拿这个参数名来用
二、各种异常情况处理
1、可以对传入参数指定参数名
- @RequestParam String inputStr
- // 下面的对传入参数指定为aa,如果前端不传aa参数名,会报错
- @RequestParam(value="aa") String inputStr
错误信息:
HTTP Status 400 - Required String parameter 'aa' is not present
2、可以通过required=false或者true来要求@RequestParam配置的前端参数是否一定要传
- // required=false表示不传的话,会给参数赋值为null,required=true就是必须要有
- @RequestMapping("testRequestParam")
- public String filesUpload(@RequestParam(value="aa", required=true) String inputStr, HttpServletRequest request)
3、如果用@RequestMapping注解的参数是int基本类型,但是required=false,这时如果不传参数值会报错,因为不传值,会赋值为null给int,这个不可以
- @RequestMapping("testRequestParam")
- public String filesUpload(@RequestParam(value="aa", required=true) String inputStr,
- @RequestParam(value="inputInt", required=false) int inputInt
- ,HttpServletRequest request) {
- // ......省略
- return "index";
- }
解决方法:
“Consider declaring it as object wrapper for the corresponding primitive type.”建议使用包装类型代替基本类型,如使用“Integer”代替“int”
self.location.href;//当前页面打开URL页面
window.location.href;//当前页面打开URL页面
this
.location.href;//当前页面打开URL页面
location.href;// 当前页面打开URL页面
parent.location.href;//在父页面打开新页面
top.location.href;//在顶层页面打开新页面
jQuery(function(){}) 或$(function(){}); 它是$(document).ready() 的简写
自SpringMVC4.2之后,RequestParam内部有4个参数:
1、String name
2、String value
3、boolean required
4、String defaultValue
其中name和value分别是对方的别名,即二者没区别,我个人比较喜欢用name,因为它的某些特性使得name这个名字更直观,下面会说到。
先看第一个映射方法的定义:
@RequestMapping("/paramTest0") public @ResponseBody String paramTest(Long id){ String result = ""; result += id; return result; }
①然后我在浏览器地址栏上输入:http://localhost:8080/test/hello/paramTest0
浏览器显示:null
这里引申出了SpringMVC的一个特性,即当浏览器中没有输入相应参数和值,那么SpringMVC不会给id赋值即id值是默认值null,因此参数都最好不要用基础类型。
②在浏览器中输入:http://localhost:8080/test/hello/paramTest0?userName=zhang&userName=li&id=9&userName=shit
浏览器显示:9
说明浏览器中只要有输入需要的参数即可,而不管是否包含多余的参数,且没有规定顺序(因为后台是可以根据key来获取值的,而如果是通过QueryString则顺序也不影响解析参数)。
③在浏览器中输入:http://localhost:8080/test/hello/paramTest0?id=6
显示为:6
这个就不解释了。
#########################################分隔符##################################################
再看第二个映射方法的定义:
@RequestMapping("/paramTest") public @ResponseBody String paramTest(@RequestParam(name="userName", required = false) String[] userNames, Long id){ String result = ""; if(userNames != null){ for(int i=0;i<userNames.length;i++){ result += userNames[i] + "#"; } } result += id; return result; }
①然后在浏览器输入:http://localhost:8080/test/hello/paramTest?userName=zhang&userName=li&id=5&userName=fuck
显示为:zhang#li#fuck#5
由此引申出:首先URL请求的参数部分可以有多对参数的参数名一样,如上面的userName,且它们中间可以用其它参数隔开(上面用了id=5隔开)而不会影响这几个参数名一样的参数值构成“数组”(对于浏览器而言它只是URL中的字符串而已是否重复没半毛钱关系,Tomcat也不会主动将它拼成数组),
相同的参数名的值会被SpringMVC通过request.getQueryString()方法获取完整的参数然后再将相同key的Entry转换成数组(应该是SpringMVC判断参数里有数组就不用getParameter方法而用getQueryString方法获取参数),这里的元素值的顺序则是跟在URL请求中的顺序是对应的。
然后看我们方法参数里的是userNames而非userName,但是仍然显示正确,这是因为RequestParam的name或value属性的值userName才是和
浏览器那边传过来的参数名对应(个人认为这是name比value更直观一点的原因),而它注解的参数userNames就是
此注解"userName"要“赋值”的变量(或说SpringMVC因为有RequestParam而做了个参数映射将客户端请求中的参数值映射到相应方法的参数上,即userNames)。
还要注意,一个@RequestParam只能注解一个参数,即后面的Long id上是没有该注解的。
RequestParam中的required是指这个参数是否客户端必须提供,defaultValue则是如果没有提供该参数默认值是什么(故required=true, defaultValue="xxx"就没意义了)。
作者:Silentdoer
欢迎任何形式的转载,但请务必注明出处。
限于本人水平,如果随笔/文章及代码有表述不当之处,还请不吝赐教。
①request.getParameter(“username”)等价于${param.username },它们一般用在服务器获取页面或客户端的内容,这些内容都是String的;
②request.getAttribute(“username”)等价于${requestScope.username},一般是从服务器传递数据到页面,在页面中获取服务器保存在其中的数据内容。
<%!%>是声明标签
<%=%>获取后台的变量值,比如后台一个session["ab"]="ab";前台<%=session["ab"]%>就能取到值; <%:%>同<%=%>。
<%@ page ... %> | 定义网页依赖属性,比如脚本语言、error页面、缓存需求等等 |
<%@ include ... %> | 包含其他文件 |
<%@ taglib ... %> | 引入标签库的定义 |
request | HttpServletRequest类的实例 |
response | HttpServletResponse类的实例 |
out | JspWriter类的实例,用于把结果输出至网页上 |
session | HttpSession类的实例 |
application | ServletContext类的实例,与应用上下文有关 |
config | ServletConfig类的实例 |
pageContext | PageContext类的实例,提供对JSP页面所有对象以及命名空间的访问 |
page | 类似于Java类中的this关键字 |
Exception | Exception类的对象,代表发生错误的JSP页面中对应的异常对象 |
<th>和<td>一样,也是需要嵌套在<tr>当中的,<tr>嵌套在<table>当中 <table>...</table> 用于定义一个表格开始和结束 <th>...</th> 定义表头单元格。表格中的文字将以粗体显示,在表格中也可以不用此标签,<th>标签必须放在<tr>标签内 <tr>...</tr> 定义一行标签,一组行标签内可以建立多组由<td>或<th>标签所定义的单元格 <td>...</td> 定义单元格标签,一组<td>标签将将建立一个单元格,<td>标签必须放在<tr>标签内
\n 用于字符串的换行
<br/>用于html的换行
JSP 读取表单数据
getParameter(): 使用 request.getParameter() 方法来获取表单参数的值。
getParameterValues(): 获得如checkbox类(名字相同,但值有多个)的数据。 接收数组变量 ,如checkbox类型
getParameterNames():该方法可以取得所有变量的名称,该方法返回一个Emumeration。
getInputStream():调用此方法来读取来自客户端的二进制数据流。
这两个应该是HTML标记吧?
p是段落标记
b是加粗
@RequestParam A) 常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( 由String到 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值。 B)用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST。(不设置这个属性,好像这就是默认值) C) 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定。 在方法参数里面如是:public @ResponseBody JsonResult getPublishedToposByConnStreamId(@RequestParam(value = "streamId", required = false) String streamId) {} @RequestBody A) GET、POST方式提时, 根据request header Content-Type的值来判断: application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理); multipart/form-data, 不能处理(次类型多用来上传文件类型---即使用@RequestBody不能处理这种格式的数据,@RequestParam这个却是可以处理的。); 其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理); B) PUT方式提交时, 根据request header Content-Type的值来判断:(表示没见过put方式滴,可以无视吧。) application/x-www-form-urlencoded, 必须; multipart/form-data, 不能处理; 其他格式, 必须; 说明:request的body部分的数据编码格式由header部分的Content-Type指定; 最后 就我的经验来看: @RequestBody这个一般处理的是在ajax请求中声明contentType: "application/json; charset=utf-8"时候。也就是json数据或者xml(我没用过这个,用的是json) @RequestParam这个一般就是在ajax里面没有声明contentType的时候,为默认的。。。urlencode格式时,用这个。 看我前面几个截图就可以看出来。
springMVC @RequestMapping 设置后 会默认将返回字符串拼成 .jsp 地址形式
1、
@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML
数据,需要注意的呢,在使用此注解之后不会再走试图处理器,而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。
2、
@RequestMapping("/login")@ResponseBody
public User login(User user){
return user;
}
User字段:userName pwd
那么在前台接收到的数据为:'{"userName":"xxx","pwd":"xxx"}'
效果等同于如下代码:
@RequestMapping("/login")
public void login(User user, HttpServletResponse response){
response.getWriter.write(JSONObject.fromObject(user).toString());
spring是目前最流行的框架。创建java web项目时,我们首先会遇到的配置文件就是web.xml,这是javaweb为我们封装的逻辑,不在今天的研究中。下面我们将简单讲讲web.xml中的配置。
一、一个空的web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"> </web-app>
二、标签介绍
web.xml中比较常见的标签以及其加载顺序为:
context-param > listener > filter > servlet
1、 <display-name>Archetype Created Web Application</display-name>
display-name 是标识项目的名称,这个不是很常用,可有可无的,或者说不需要我们去在意的东西。
2、 <context-param>
<context-param> <param-name>webAppRootKey</param-name> <param-value>60000</param-value> </context-param>
context-param 是web.xml首先加载的标签,其下子标签有param-name和param-value.
此所设定的参数,在JSP网页中可以使用下列方法来取得:
${initParam.webAppRootKey}
若在Servlet可以使用下列方法来获得:
String param_name=getServletContext().getInitParamter(“webAppRootKey”);
3、listener
<listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
listenter在项目开始的时候就注入进来,尽在context-param之后,所以正常我们将spring配置在listener 中,这样方法spring 初始化相关的bean。
4、filter
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
filter起到一个过滤的作用,在servlet执行前后,像上面的配置就是在过滤servlet前将编码转换UTF-8,filter-mapping 则是将filter和url路径进行映射。其中init-param则是将初始化需要的参数传入到filter-class中从而进行初始化。filter和filter-mapping中的name必须是相同的,才能起到映射的作用,而filter-mapping 中的url-pattern则是匹配请求路径的。上面‘/*'表示过滤所有请求的servlet,如果写成‘/zxh',则过滤http://localhost:8080/项目名/zxh这个请求。
5、servlet
<servlet> <!-- 配置DispatcherServlet --> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定spring mvc配置文件位置 不指定使用默认情况 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <!-- 设置启动顺序 --> <load-on-startup>1</load-on-startup> </servlet> <!-- ServLet 匹配映射 --> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>*.zxh</url-pattern> </servlet-mapping>
servlet和filter类似,需要先指定servlet对应的class类,然后将这个类和utl路径请求地址进行映射。这里不多说了。
以上就是web.xml文件中出现最多的几个标签。其他的比如:
6、欢迎页
<welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list>
7、错误页
<!-- 后台程序异常错误跳转页面 --> <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/views/error.jsp</location> </error-page> <!-- 500跳转页面--> <error-page> <error-code>500</error-code> <location>/views/500.jsp</location> </error-page> <!-- 404跳转页面 --> <error-page> <error-code>404</error-code> <location>/views/404.jsp</location> </error-page>
三、示例
1、spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name)
2、在web.xml配置监听器ContextLoaderListener(listener-class)
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
3、部署applicationContext的xml文件:contextConfigLocation(context-param下的param-name)
4、DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller(我们写的Action)来处理。
DispatcherServlet(servlet-name、servlet-class、init-param、param-name(contextConfigLocation)、param-value)
在DispatcherServlet的初始化过程中,框架会在web应用的 WEB-INF文件夹下寻找名为[servlet-name]-servlet.xml 的配置文件,生成文件中定义的bean
<?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"> <!-- 在Spring框架中是如何解决从页面传来的字符串的编码问题的呢? 下面我们来看看Spring框架给我们提供过滤器CharacterEncodingFilter 这个过滤器就是针对于每次浏览器请求进行过滤的,然后再其之上添加了父类没有的功能即处理字符编码。 其中encoding用来设置编码格式,forceEncoding用来设置是否理会 request.getCharacterEncoding()方法,设置为true则强制覆盖之前的编码格式。--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 项目中使用Spring 时,applicationContext.xml配置文件中并没有BeanFactory,要想在业务层中的class 文件中直接引用Spring容器管理的bean可通过以下方式--> <!--1、在web.xml配置监听器ContextLoaderListener--> <!--ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。 在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。 它的API说明 第一段说明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。 如果查看ContextLoaderServlet的API,可以看到它也关联了ContextLoader这个类而且它实现了HttpServlet这个接口 第二段,ContextLoader创建的是 XmlWebApplicationContext这样一个类,它实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext-> BeanFactory这样一来spring中的所有bean都由这个类来创建 IUploaddatafileManager uploadmanager = (IUploaddatafileManager) ContextLoaderListener.getCurrentWebApplicationContext().getBean("uploadManager"); --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--2、部署applicationContext的xml文件--> <!--如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml, 在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。 如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数: 在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并以“,”号分隔。 也可以这样applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml, applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。 在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> <!--如果你的DispatcherServlet拦截"/",为了实现REST风格,拦截了所有的请求,那么同时对*.js,*.jpg等静态文件的访问也就被拦截了。--> <!--方案一:激活Tomcat的defaultServlet来处理静态文件--> <!--要写在DispatcherServlet的前面, 让 defaultServlet先拦截请求,这样请求就不会进入Spring了,我想性能是最好的吧。--> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.swf</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.xml</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.json</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.map</url-pattern> </servlet-mapping> <!--使用Spring MVC,配置DispatcherServlet是第一步。DispatcherServlet是一个Servlet,,所以可以配置多个DispatcherServlet--> <!--DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller(我们写的Action)来处理。--> <servlet> <servlet-name>DispatcherServlet</servlet-name><!--在DispatcherServlet的初始化过程中,框架会在web应用的 WEB-INF文件夹下寻找名为[servlet-name]-servlet.xml 的配置文件,生成文件中定义的bean。--> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--指明了配置文件的文件名,不使用默认配置文件名,而使用dispatcher-servlet.xml配置文件。--> <init-param> <param-name>contextConfigLocation</param-name> <!--其中<param-value>**.xml</param-value> 这里可以使用多种写法--> <!--1、不写,使用默认值:/WEB-INF/<servlet-name>-servlet.xml--> <!--2、<param-value>/WEB-INF/classes/dispatcher-servlet.xml</param-value>--> <!--3、<param-value>classpath*:dispatcher-servlet.xml</param-value>--> <!--4、多个值用逗号分隔--> <param-value>classpath:spring/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup><!--是启动顺序,让这个Servlet随Servletp容器一起启动。--> </servlet> <servlet-mapping> <!--这个Servlet的名字是dispatcher,可以有多个DispatcherServlet,是通过名字来区分的。每一个DispatcherServlet有自己的WebApplicationContext上下文对象。同时保存的ServletContext中和Request对象中.--> <!--ApplicationContext是Spring的核心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些,ApplicationContext则是“应用的容器”了:P,Spring把Bean放在这个容器中,在需要的时候,用getBean方法取出--> <servlet-name>DispatcherServlet</servlet-name> <!--Servlet拦截匹配规则可以自已定义,当映射为@RequestMapping("/user/add")时,为例,拦截哪种URL合适?--> <!--1、拦截*.do、*.htm, 例如:/user/add.do,这是最传统的方式,最简单也最实用。不会导致静态文件(jpg,js,css)被拦截。--> <!--2、拦截/,例如:/user/add,可以实现现在很流行的REST风格。很多互联网类型的应用很喜欢这种风格的URL。弊端:会导致静态文件(jpg,js,css)被拦截后不能正常显示。 --> <url-pattern>/</url-pattern> <!--会拦截URL中带“/”的请求。--> </servlet-mapping> <welcome-file-list><!--指定欢迎页面--> <welcome-file>login.html</welcome-file> </welcome-file-list> <error-page> <!--当系统出现404错误,跳转到页面nopage.html--> <error-code>404</error-code> <location>/nopage.html</location> </error-page> <error-page> <!--当系统出现java.lang.NullPointerException,跳转到页面error.html--> <exception-type>java.lang.NullPointerException</exception-type> <location>/error.html</location> </error-page> <session-config><!--会话超时配置,单位分钟--> <session-timeout>360</session-timeout> </session-config> </web-app>
四、spring加载
通过上面的了解,我们可以看出spring核心配置文件就是listener那块。在监听之前我们已经通过context-param将spring配置文件传到上下文中了(application)。下面我们就来看看spring是如何工作的吧
第一步:
点开listener源码,我们发现他有下面几个方法。和继承的关系。我们发现他实现了ContextLoaderListener这个接口,这个接口在参数设置好之后自动执行contextInitialized方法的。
那么我们来看看contextInitialized方法
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " +
"check whether you have multiple ContextLoader* definitions in your web.xml!");
}
Log logger = LogFactory.getLog(ContextLoader.class);
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();
try {
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent ->
// determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
cwac.setParent(parent);
}
configureAndRefreshWebApplicationContext(cwac, servletContext);
}
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = this.context;
}
else if (ccl != null) {
currentContextPerThread.put(ccl, this.context);
}
if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
return this.context;
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
}
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
throw err;
}
}
仔细研究官方解释,就是在这里初始化application,这里会用到contextClass+contextConfigLocation两个参数,如果contextClass在context-param提供了,我们就会根据这一个class去初始化application,很显然我们正常配置都没有配这个,而是配置了后者,配置了后者就会去根据contextConfigLocation中提供的配置文件去解析然后创建相关的bean和application操作,这个方法的最后会执行configureAndRefreshWebApplicationContext方法。这个方法就是在根据contextConfigLocation提供的配置文件中创建相关的bean。
五、springMVC 加载
springMVC其实和spring是一样的,但是他不用再程序开始时访问
<servlet> <!-- 配置DispatcherServlet --> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定spring mvc配置文件位置 不指定使用默认情况 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <!-- 设置启动顺序 --> <load-on-startup>1</load-on-startup> </servlet> <!-- ServLet 匹配映射 --> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>*.zxh</url-pattern> </servlet-mapping>
看DispatcherServlet源码中对contextConfigLocation参数的解释
上面明确指出我们这个参数给XmlWebApplicationContext类的,我们在进入XmlWebApplicationContext类看看究竟。
这样我们很容易理解为什么springmvc默认的配置文件会在WEB-INF/application.xml中的吧。
在dispatcherservlet中有一个初始化方法,这里就初始化配置中一些东西,比如说文件上传适配器的配置等等。
protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}
总结
spring+springmvc在配置中主要就是上面的两个配置,当然spring的强大不是我们一两天能够研究来的,我上面只是简单的研究讨论了一下。
spring与mybatis三种整合方法
1、采用MapperScannerConfigurer,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean。
spring-mybatis.xml:
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自动扫描 --> <context:component-scan base-package="com.hua.saf" /> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="${initialSize}" /> <!-- 连接池最大数量 --> <property name="maxActive" value="${maxActive}" /> <!-- 连接池最大空闲 --> <property name="maxIdle" value="${maxIdle}" /> <!-- 连接池最小空闲 --> <property name="minIdle" value="${minIdle}" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${maxWait}" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件,**表示迭代查找 --> <property name="mapperLocations" value="classpath:com/hua/saf/**/*.xml" /> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 ,包下的类需要使用@MapperScan注解,否则容器注入会失败 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.hua.saf.*" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的 例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀) --> <mapper namespace="com.hua.saf.dao.UserDao"> <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复, 使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型 resultType="com.hua.saf.User"就表示将查询结果封装成一个User类的对象返回,User类就是t_user表所对应的实体类 --> <!-- 根据id查询得到一个user对象--> <select id="getUser" parameterType="int" resultType="com.hua.saf.pojo.User"> select * from t_user where id=#{id} </select> </mapper>
dao类:
/** * 这里的@MapperScan就是上面所讲的Mapper扫描器中所需要的配置,会自动生成代理对象。 * 注意,接口中的方法名称要和对应的MyBatis映射文件中的语句的id值一样,因为生成的 * 动态代理,会根据这个匹配相应的Sql语句执行。另外就是方法的参数和返回值也需要注 * 意。接口中的方法如何定义,对应的MyBatis映射文件就应该进行相应的定义。 * 最后,标注中的userDao是用来作为Spring的Bean的id(或name)进行使用的,方便我 * 们在Service层进行注入使用。 */ @MapperScan public interface UserDao { //此处的方法名必须和mapper中的映射文件中的id同名 //回去映射文件中通过com.hua.saf.dao.UserDao.getUser,即this.getClass().getName()+".getUser" public User getUser(int id); }
service类:
@Service("userService") public class UserServiceImpl implements IUserService { @Resource private UserDao userDao; public User getUser(int id) { return userDao.getUser(id); } }
2、采用接口org.apache.ibatis.session.SqlSession的实现类org.mybatis.spring.SqlSessionTemplate。
mybatis中, sessionFactory可由SqlSessionFactoryBuilder.来创建。MyBatis-Spring 中,使用了SqlSessionFactoryBean来替代。SqlSessionFactoryBean有一个必须属性dataSource,另外其还有一个通用属性configLocation(用来指定mybatis的xml配置文件路径)。
spring-mybatis.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自动扫描 --> <context:component-scan base-package="com.hua.saf" /> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="${initialSize}" /> <!-- 连接池最大数量 --> <property name="maxActive" value="${maxActive}" /> <!-- 连接池最大空闲 --> <property name="maxIdle" value="${maxIdle}" /> <!-- 连接池最小空闲 --> <property name="minIdle" value="${minIdle}" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${maxWait}" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> <!-- 自动扫描mapping.xml文件,**表示迭代查找,也可在sqlMapConfig.xml中单独指定xml文件--> <property name="mapperLocations" value="classpath:com/hua/saf/**/*.xml" /> </bean> <!-- mybatis spring sqlSessionTemplate,使用时直接让spring注入即可 --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.hua.saf.pojo.User" alias="User" /> </typeAliases> </configuration>
User.java
public class User { private int id; private String username; private String password; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
UserDao.java
@Repository public class UserDao{ @Resource private SqlSessionTemplate sqlSessionTemplate; public User getUser(int id) { return sqlSessionTemplate.selectOne(this.getClass().getName() + ".getUser", 1); } }
UserService.java
@Service public class UserService{ @Resource private UserDao userDao; public User getUser(int id) { return userDao.getUser(id); } }
3、采用抽象类org.mybatis.spring.support.SqlSessionDaoSupport提供SqlSession。
spring-mybatis.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自动扫描 --> <context:component-scan base-package="com.hua.saf" /> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="${initialSize}" /> <!-- 连接池最大数量 --> <property name="maxActive" value="${maxActive}" /> <!-- 连接池最大空闲 --> <property name="maxIdle" value="${maxIdle}" /> <!-- 连接池最小空闲 --> <property name="minIdle" value="${minIdle}" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${maxWait}" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> <!-- 自动扫描mapping.xml文件,**表示迭代查找,也可在sqlMapConfig.xml中单独指定xml文件--> <property name="mapperLocations" value="classpath:com/hua/saf/**/*.xml" /> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.hua.saf.pojo.User" alias="User" /> </typeAliases> </configuration>
User.java
public class User { private int id; private String username; private String password; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
UserDao.java
@Repository public class UserDao extends SqlSessionDaoSupport{ public User getUser(int id) { return this.getSqlSession().selectOne(this.getClass().getName() + ".getUser", 1); } //使用SqlSessionDaoSupport必须注意,此处源码1.1.1中有自动注入,1.2中取消了自动注入,需要手工注入,侵入性强 //也可在spring-mybatis.xml中如下配置,但是这种有多少个dao就要配置到少个,多个dao就很麻烦。 //<bean id="userDao" class="com.hua.saf.dao.UserDao"> // <property name="sqlSessionFactory" ref="sqlSessionFactory"/> //</bean> @Resource public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { // TODO Auto-generated method stub super.setSqlSessionFactory(sqlSessionFactory); } }
UserService.java
@Service public class UserService{ @Resource private UserDao userDao; public User getUserss(int id) { return userDao.getUser(1); } }