web.xml中配置servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>@springmvc</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
spring配置文件中配置组建扫描的包和视图解析器
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
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">
<!-- 配置扫描组件的包 -->
<context:component-scan base-package="com.yawn.controller"></context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 使用InternalResourceViewResolver视图解析器时,不需要配置viewClass属性。默认支持JSTL -->
<!-- <property name="viewClass" ></property> -->
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
编写controller控制器,并加上注解
package com.yawn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping("/start")
private String start() {
System.out.println(">>>>>>>>>>>>>>>>>>-------");
return "start";
}
}
@RequestMapping("")注解可以用在类或者方法,如上使用后,其访问的url为/springmvc/test/start.do
返回的视图为:
/WEB-INF/pages/test/start.jsp
@PathVariable注解:
package com.yawn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/start/{name}/{age}")
private String start(@PathVariable("name") String name, @PathVariable("age") String age) {
System.out.println(">>>>>>>>>>>>>>>>>>-------" + name +" , " + age);
return "start";
}
}
请求/start/yawn/20.do,yawn就可以作为参数name的值,20就可以作为参数age的值。
根据请求方法(get、post)的不同,进行不同的处理:
package com.yawn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class TestController {
@RequestMapping(value="/start", method=RequestMethod.GET)
private String start() {
System.out.println(">>>>>>>>>>>>>>>>>>-------get");
return "start";
}
@RequestMapping(value="/start", method=RequestMethod.POST)
private String postStart(){
System.out.println("------------<<<<<<<<<<<<<<<<<<post");
return "start";
}
}
日期类型参数的注入和绑定:
package com.yawn.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping(value="/start/{date}")
private String start(@PathVariable("date") Date date) { //注入日期对象的参数的绑定
System.out.println(">>>>>>>>>>>>>>>>>>-------" + date);
return "start";
}
@InitBinder
private void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyyMMdd"), false));
}
}
@RequestParam 得到请求url中的参数:
package com.yawn.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class TestController {
@RequestMapping(value="/start")
private String start(@RequestParam("id") int id) {
System.out.println(">>>>>>>>>>>>>>>>>>-------" + id);
return "start";
}
}
@CookieValue 和 @RequestHeader 得到cookie和请求的头信息:
package com.yawn.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping(value="/start")
private String start(@CookieValue("userName") String userName, @RequestHeader("user-Agent") String userAgent) {
System.out.println(">>>>>>>>>>>>>>>>>>-------" + userName + userAgent);
return "start";
}
}
处理请求的方法可以接收的参数:
request
response
session
@RequestParam
@PathVariable
@CookieValue
@RequestHeader
当需要输出信息时,可以用(PrintWriter out,Map model)
当提交登陆表单时,可以直接使用参数(User user,BindingResult result)实体模型和绑定结果。
处理请求方法返回值的类型:
void: ①使用PrintWriter输出 ②自动从请求路径解析返回视图的逻辑名称
String: viewName
User : 返回模型,在页面可以用${user.name},${user.password}取出
List<User> : 在页面可以通过${userList}取出来
Map model : 返回模型(Model类是spring对Map的实现)
ModelAndView:模型和视图(包含视图逻辑名称和一些键值对)
返回视图时重定向到另一个请求:
package com.yawn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping(value="/start")
private String start(@RequestHeader("user-Agent") String userAgent) {
System.out.println(">>>>>>>>>>>>>>>>>>-------" + userAgent);
return "redirect:/start_list";
}
}