Spring3 MVC 学习笔记
一、Spring3 MVC基本配置
1.web.xml配置
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2.spring配置文件
上面的web.xml中的init-param标记中的参数主要是配置了spring配置文件的路径和文件名,如果不标明,spring默认的配置文件位置和文件名应该为:
“/WEB-INF/<servlet-name>-servlet.xml”,其中的<servlet-name>在这里应该对应Dispatcher。
配置文件片段:
<!-- 扫描发现组件 -->
<context:component-scanbase-package="net.xiaxin.action"/>
<beanclass=
"org.springframework.web.servlet.
mvc.annotation.DefaultAnnotationHandlerMapping"/>
<beanclass=
"org.springframework.web.servlet.
mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!--使用注解的话,XML配置就会被忽略?-->
<!--Definition of View Resolver -->
<beanid="viewResolver"
class="org.springframework.web.servlet.
view.InternalResourceViewResolver">
<!-- Resolver将把输出结果与输出界面相融合,为表现层提供呈现资源。 -->
<propertyname="viewClass">
<!-- 这里我们使用JSP页面作为输出,使用JstlView -->
<value>
org.springframework.web.servlet.view.JstlView
</value>
</property>
<propertyname="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<propertyname="suffix">
<value>.jsp</value>
</property>
<!—
3、4指定了表现层资源的前缀和后缀,
运行时,Spring 将为指定的表现层资源自动追加前缀和后缀,
以形成一个完整的资源路径。
-->
</bean>
二、使用标注
Spring3增加了大量标注,方便我们进行开发。使用时先要进行标注的扫描:
<context:component-scanbase-package="net.xiaxin.action"/>
其中的base-package中的数据是我们的包名,用来描述扫描此包下的所有类文件。
三、@Controller
这里的标注对应spring2.5的Controller接口及其实现类,被此标注修饰的类名代表这个类为一个Controller,可以实现请求的转发。
四、@RequestMapping
@RequestMapping(value="/login", method=RequestMethod.GET)
value属性值login代表拦截所有login的请求,请求方式为GET方式。
使用此标注处理控制器转发过来的请求。此标注可以添加在类前或者方法前,并形成类似一级、二级的拦截形式。
例:
@Controller
@RequestMapping("/test")
publicclass RegisterController {
@RequestMapping(value="/login", method=RequestMethod.GET)
public String register(HttpServletRequest req) {
req.setAttribute("name", req.getParameter("name"));
req.setAttribute("pwd", req.getParameter("pwd"));
return param;
}
}
上面例子,当请求为/test/login.do时,register方法才会被调用。
注:以下是另外两种用法
@RequestMapping(value = "/pets/{petId}", params="myParam=myValue")
主要用来对请求参数进行匹配。
@RequestMapping(value = "/pets", headers="content-type=text/*")
主要用来对请求头进行匹配。
五、@PathVariable
此标注方便我们动态处理客户端请求,例:
@Controller
@RequestMapping("/test")
publicclass RegisterController {
@RequestMapping(value="/{p}", method=RequestMethod.GET)
public String register(@PathVariable("p") String param) {
System.out.println("the value of param is :"+param);
return param;
}
}
当请求为/test/register.do时,客户端请求被register方法处理时,value="/{p}"的p被赋值为register,由于方法参数param有@PathVariable("p")标注,并且标注中的值”p”同请求中的{p}相同,所以,param也会被赋值为register,供方法内具体处理。
需要注意的是,如果RegisterController中有一个能够具体匹配的RequestMapping参数,请求会首先选择能够具体匹配的方法进行调用处理。如本例中的RegisterController类中还有方法:
@RequestMapping(value="/login", method=RequestMethod.GET)
public String login(String name, String pwd, HttpServletRequest req){
System.out.println(name+"----"+pwd);
req.setAttribute("name", name);
req.setAttribute("pwd", pwd);
return"login";
}
那么请求/test/login.do会被login方法处理,而不会被register方法处理。
六、使用标注的方法说明
首先,使用Spring3 MVC提供给我们的标注,可以让我们方便的将一个类变为受Spring容器管理的Controller对象。然后根据@RequestMapping的配置,找到适合的请求处理方法。我们的方法一般来说可由自己定义参数,Spring容器会根据请求和我们定义的参数进行匹配,通过IOC容器装配进来。比如,我们的方法可以定义为:
1.@RequestMapping(value="/login", method=RequestMethod.GET)
public String login(String name, String pwd){
return"login";
}
请求串中的name和pwd就会被注入到相应的参数中。前提是请求参数与方法参数名一致。
2.@RequestMapping(value="/login", method=RequestMethod.GET)
public String login(HttpServletRequest req){
req.setAttribute("name", name);
req.setAttribute("pwd", pwd);
return"login";
}
请求会被封装成HttpServletRequest对象作为参数提供给方法使用。
七、@RequestParam
上面例子1中提到请求参数要与方法参数名一致才能被注入相应数据,如果遇到不一致的情况,我们就需要通过@RequestParam来注入了。
标注@RequestParam("pwd")中的pwd是请求串中的参数:
http://localhost:8080/ssi/test/login.do?name=zs&pwd=1234
而我们的login方法中的参数为pwds,出现了不一致,所以使用此标注解决。
@RequestMapping(value="/login", method=RequestMethod.GET)
public String login(String name, @RequestParam("pwd") String pwds){
System.out.println(name+"----"+pwds);
return"login";
}
八、@ModelAttribute
可以将页面提交的参数直接绑定成一个模型对象,前提是页面需要使用Spring标签。
九、@ResponseBody
可以让我们从Controller中生成JSON对象进行返回。
前期我们需要加入: jackson-mapper-asl-1.6.2.jar
和 jackson-core-asl-1.6.2.jar
随后对Spring的config配置文件进行更改:
<beanclass="org.springframework.web.servlet.mvc.
annotation.AnnotationMethodHandlerAdapter">
<propertyname="messageConverters">
<list>
<refbean="mappingJacksonHttpMessageConverter"/>
<!-- json转换器 -->
</list>
</property>
</bean>
<beanid="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.
json.MappingJacksonHttpMessageConverter"/>
使用@ResponseBody生成JSON对象返回:
@RequestMapping(value="ajaxGetUser", method=RequestMethod.GET)
public@ResponseBody List<User> ajaxGetUser(
@RequestParam String name, @RequestParam Integer age){
List<User> users = userDao.selectUser(name,age);
return users;
}
这样生成了一个JSON对象,其中包含了若干个User对象的JSON对象字符串(嵌套包含)。
十、RequestContextUtils
通过RequestContextUtils可以得到WebApplicationContext,通过WebApplicationContext能够得到ServletContext
十一、支持的方法参数和返回类型
一)、 参数类型
1.任意的Servlet请求或者相应类型对象,比如:ServletResponse、HttpServletRequest等。
2.Session:HttpSession。
3.org.springframework.web.context.request.WebRequest
4.org.springframework.web.context.request.NativeWebRequest
5.java.util.Locale
6.java.io.InputStream / java.io.Reader
7.java.io.OutputStream / java.io.Writer
8.@PathVariable/ @RequestParam/ @RequestHeader/ @RequestBody
9.HttpEntity<?>
10.java.util.Map
/ org.springframework.ui.Model /org.springframework.ui.ModelMap
11.org.springframework.validation.Errors
12.org.springframework.web.bind.support.SessionStatus
13.可以自行定义Object作为参数,需要使用@ModelAttribute("pet")
例:@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet,
Model model, BindingResult result) {
}
二)、返回类型
1.ModelAndView
2.Model
3.Map
4.View
5.String
6.void