工程目录结构:
项目依赖包:
SpringMVC参数
Vo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.course.vo;
public class User {
private Integer id; private String name; private Integer age; public User() { super(); } public User(Integer id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }
@Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| package com.course.vo;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
public class UserInfo {
private Integer id; private String name; private Integer age; @DateTimeFormat(pattern = "yyyy-MM-dd") private Date hireDate; public UserInfo() { super(); }
public UserInfo(Integer id, String name, Integer age, Date hireDate) { super(); this.id = id; this.name = name; this.age = age; this.hireDate = hireDate; }
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
public Date getHireDate() { return hireDate; }
public void setHireDate(Date hireDate) { this.hireDate = hireDate; }
@Override public String toString() { return "UserInfo [id=" + id + ", name=" + name + ", age=" + age + ", hireDate=" + hireDate + "]"; } }
|
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| package com.course.controller;
import java.util.Arrays;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView;
import com.course.vo.User; import com.course.vo.UserInfo;
@Controller @RequestMapping("user") public class UserController {
@RequestMapping(value = "addUserInfo01", method = RequestMethod.POST) public ModelAndView addUserInfo01(String username, String password, Integer age) { System.out.println(username + " " + password + " " + age); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/view/success.jsp"); return modelAndView; } @RequestMapping(value = "addUserInfo02", method = RequestMethod.POST) public ModelAndView addUserInfo02(String[] hobby) { System.out.println(Arrays.toString(hobby)); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/view/success.jsp"); return modelAndView; } @RequestMapping(value = "addUserInfo03", method = RequestMethod.POST) public ModelAndView addUserInfo03(User user) { System.out.println(user); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/view/success.jsp"); return modelAndView; } @RequestMapping(value = "addUserInfo04", method = RequestMethod.POST) public ModelAndView addUserInfo04(UserInfo userInfo) { System.out.println(userInfo); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/view/success.jsp"); return modelAndView; } }
|
index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body>
<h2>1.parameters: basic data type</h2> <form action="user/addUserInfo01.do" method="post"> <p> name: <input type="text" name="username"> </p> <p> password: <input type="password" name="password"> </p> <p> age: <input type="text" name="age"> </p> <p> <input type="submit" value="submit"> </p> </form> <h2>2.parameters: array type</h2> <form action="user/addUserInfo02.do" method="post"> <p>hobby: </p> <input type="checkbox" name="hobby" value="reading">reading <input type="checkbox" name="hobby" value="riding">riding <input type="checkbox" name="hobby" value="running">running <input type="checkbox" name="hobby" value="swimming">swimming <p> <input type="submit" value="submit"> </p> </form> <h2>3.parameters: object type</h2> <form action="user/addUserInfo03.do" method="post"> <p> id: <input type="text" name="id"> </p> <p> name: <input type="text" name="name"> </p> <p> age: <input type="text" name="age"> </p> <p> <input type="submit" value="submit"> </p> </form> <h2>4.parameters: object type which contain date type</h2> <form action="user/addUserInfo04.do" method="post"> <p> id: <input type="text" name="id"> </p> <p> name: <input type="text" name="name"> </p> <p> age: <input type="text" name="age"> </p> <p> hireDate: <input type="text" name="hireDate"> </p> <p> <input type="submit" value="submit"> </p> </form>
</body> </html>
|
success.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13
| <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <p>Add successfully!</p> </body> </html>
|
springmvc.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.course.controller"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> </beans>
|
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?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" id="WebApp_ID" version="3.0"> <display-name>01_springmvc_hello</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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> </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>
|
1 2 3 4 5 6 7 8
| log4j.rootLogger=INFO, stdout
log4j.logger.org.mybatis.example.BlogMapper=TRACE
log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
|
启动Tomcat,浏览器访问: http://localhost:8080/01_springmvc_init/index.jsp
Controller返回值类型
Controller返回值类型:
- ModelAndView
- String
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package com.course.controller;
import java.util.ArrayList; import java.util.Arrays; import java.util.List;
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView;
import com.course.vo.User; import com.course.vo.UserInfo;
@Controller @RequestMapping("user") public class UserController {
@RequestMapping("getUserList5") public ModelAndView getUserList5() { List<User> userList = new ArrayList<>(); User user; for (int i = 0; i < 5; i++) { user = new User(i, "name" + i, 10 + i); userList.add(user); } ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("userList", userList); modelAndView.setViewName("/WEB-INF/view/UserList.jsp"); return modelAndView; } @RequestMapping("getUserList10") public String getUserList10(Model model) { List<User> userList = new ArrayList<>(); User user; for (int i = 0; i < 10; i++) { user = new User(i, "name" + i, 10 + i); userList.add(user); }
model.addAttribute("userList", userList); return "/WEB-INF/view/UserList.jsp"; } }
|
UserList.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>User List</h1> <hr/> <table align="center" width="60%" border="1" cellspacing="5"> <thead> <tr> <th>Id</th> <th>Name</th> <th>Age</th> <tr> </thead> <tbody> <c:forEach var="user" items="${userList}"> <tr> <td>${user.id}</td> <td>${user.name}</td> <td>${user.age}</td> </tr> </c:forEach> </tbody> </table> </body> </html>
|
启动Tomcat,
浏览器访问: http://localhost:8080/01_springmvc_init/user/getUserList5.do
浏览器访问: http://localhost:8080/01_springmvc_init/user/getUserList10.do
Web对象的获取
Web对象:
- HttpServletRequest
- HttpServletResponse
- HttpSession
- ServletContext
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| package com.course.controller;
import java.util.ArrayList; import java.util.Arrays; import java.util.List;
import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView;
import com.course.util.WebObjectUtil; import com.course.vo.User; import com.course.vo.UserInfo;
@Controller @RequestMapping("user") public class UserController { @RequestMapping(value = "getWebObject") public String getWebObject(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, HttpSession httpSession) { System.out.println("httpServletRequest = " + httpServletRequest); System.out.println("httpServletResponse = " + httpServletResponse); HttpSession httpSession2 = httpServletRequest.getSession(); System.out.println("httpSession = " + httpSession); System.out.println("httpSession2 = " + httpSession2); ServletContext servletContext = httpServletRequest.getServletContext(); ServletContext servletContext2 = httpSession.getServletContext(); System.out.println("servletContext = " + servletContext); System.out.println("servletContext2 = " + servletContext2);
return "/WEB-INF/view/success.jsp"; } @Autowired private HttpServletRequest httpServletRequest; @Autowired private HttpServletResponse httpServletResponse; @Autowired private HttpSession httpSession; @RequestMapping(value = "getWebObject2") public String getWebObject2() { System.out.println("httpServletRequest = " + httpServletRequest); System.out.println("httpServletResponse = " + httpServletResponse); httpSession.setAttribute("aaa", "123"); HttpSession httpSession2 = httpServletRequest.getSession(); System.out.println(httpSession2.getAttribute("aaa")); ServletContext servletContext = httpServletRequest.getServletContext(); ServletContext servletContext2 = httpSession.getServletContext(); System.out.println("servletContext = " + servletContext); System.out.println("servletContext2 = " + servletContext2); return "/WEB-INF/view/success.jsp"; } @RequestMapping(value = "getWebObject3") public String getWebObject3() { WebObjectUtil.getWebObjectUtil(); return "/WEB-INF/view/success.jsp"; } }
|
Util: WebObjectUtil
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.course.util;
import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes;
public class WebObjectUtil {
public static void getWebObjectUtil() { ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest(); HttpServletResponse httpServletResponse = servletRequestAttributes.getResponse(); System.out.println("httpServletRequest = " + httpServletRequest); System.out.println("httpServletResponse = " + httpServletResponse); HttpSession httpSession = httpServletRequest.getSession(); System.out.println("httpSession = " + httpSession); ServletContext servletContext = httpServletRequest.getServletContext(); System.out.println("servletContext = " + servletContext);
} }
|
启动Tomcat,
浏览器访问:
http://localhost:8080/01_springmvc_init/user/getWebObject.do
http://localhost:8080/01_springmvc_init/user/getWebObject2.do
http://localhost:8080/01_springmvc_init/user/getWebObject3.do
结果均为: