基本配置
1.导包
2.配置.xml文件
(1) springmvc.xml(放置在src下,也可放在/WEB-INF/下,web.xml初始化时:/WEB- INF/springmvc.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-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!--扫描包-->
<context:component-scan base-package="com.xxx.controller"></context:component-scan>
<!--注解驱动-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
(2) web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<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:springmvc.xml</param-value> <--/src文件夹下-->
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.常用注解
(1) Controller
(2) @RequestMapping
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>user</title>
</head>
<body>
<form action="doForm" method="post">
<p>name:<input type="text" name="name"></p>
<p>age:<input type="text" name="age"></p>
<p>age:<input type="submit" value="submit"></p>
</form>
</body>
</html>
@RequestMapping(value = "/doFrom", method = {RequestMethod.POST})
// url; 请求方式
public String doFrom(@RequestParam("name") String name, @RequestParam("age") int age) {
// .jsp中的name="xxx"
System.out.println(name + age);
return "index"; //默认转发
}
(3) @cookieValue
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>user</title>
</head>
<body>
<a href="doIndex">测试@cookieValue</a>
</body>
</html>
@Controller
public class UserController {
@RequestMapping("/doIndex")
public String doIndex(@CookieValue(value = "JSESSIONID") String name) {
System.out.println(name);
return "index";
}
(4) @RequestHeader
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>user</title>
</head>
<body>
<a href="doHeader">测试@RequestHeader</a>
</body>
</html>
@RequestMapping("/doHeader")
public String doHeader(@RequestHeader(value = "Accept") String header) {
//"手动敲黑板" value = "Accept"
System.out.println(header);
return "index";
}
(5)@ModelAttribute
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>user</title>
</head>
<body>
<a href="go">测试@ModelAttribute</a>
</body>
</html>
import java.io.Serializable;
public class User implements Serializable {
private int id;
private String name;
private int password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
}
@ModelAttribute
public void doPro(Model model) {
//字符串
model.addAttribute("key", "wangxiaoer");
//对象
User u = new User();
u.setId(1);
u.setName("malaoshi");
u.setPassword(1212);
model.addAttribute("user", u);
}
@RequestMapping("go")
public String go() {
return "go";
}
(6)@SessionAttributes
@Controller
@SessionAttributes(value = {"att1","att2"}) //添加于类上 定义"key"
public class UserController {
@RequestMapping("goAdd")
public String goAdd(Model model) {
model.addAttribute("att1","zhangsan");
model.addAttribute("att2","lisi");
return "redirect:go"; //重定向
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>go.jsp</title>
</head>
<body>
${att1}
${att2}
</body>
</html>