├── .gitignore
├── src
└── main
│ ├── resources
│ ├── local_zh_CN.properties
│ ├── local.properties
│ ├── applicationContext.xml
│ └── springmvc-servlet.xml
│ ├── webapp
│ ├── index.jsp
│ ├── upload
│ │ ├── 052112090047146.png
│ │ └── 052340331602684.png
│ └── WEB-INF
│ │ ├── jsp
│ │ ├── hello.jsp
│ │ ├── error.jsp
│ │ ├── local.jsp
│ │ ├── show.jsp
│ │ ├── showUser.jsp
│ │ ├── upload.jsp
│ │ ├── addUser.jsp
│ │ ├── ajax.jsp
│ │ └── rest.jsp
│ │ └── web.xml
│ └── java
│ └── com
│ └── spring
│ └── mvc
│ ├── integrate
│ ├── UserService.java
│ ├── UserController.java
│ └── User.java
│ ├── model
│ ├── Person.java
│ └── Rest.java
│ ├── controller
│ ├── AdviceController.java
│ ├── JsonController.java
│ ├── MvcController1.java
│ ├── ExceptionController.java
│ ├── FormController.java
│ ├── RestController.java
│ └── MvcController.java
│ └── interceptor
│ └── MyInterceptor.java
├── README.md
└── pom.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | .classpath
2 | .project
3 | .settings
4 | /target/
5 |
--------------------------------------------------------------------------------
/src/main/resources/local_zh_CN.properties:
--------------------------------------------------------------------------------
1 | username=\u8D26\u53F7
2 | password=\u5BC6\u7801
--------------------------------------------------------------------------------
/src/main/webapp/index.jsp:
--------------------------------------------------------------------------------
1 |
2 |
3 | Hello World!
4 |
5 |
6 |
--------------------------------------------------------------------------------
/src/main/webapp/upload/052112090047146.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Spring-Demo/spring-mvc-action-tutorial/HEAD/src/main/webapp/upload/052112090047146.png
--------------------------------------------------------------------------------
/src/main/webapp/upload/052340331602684.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Spring-Demo/spring-mvc-action-tutorial/HEAD/src/main/webapp/upload/052340331602684.png
--------------------------------------------------------------------------------
/src/main/resources/local.properties:
--------------------------------------------------------------------------------
1 | NotEmpty.user.name=name can't not be empty
2 | Past.user.birth=birth should be a past value
3 | DateTimeFormat.user.birth=the format of input is wrong
4 | typeMismatch.user.birth=the format of input is wrong
5 | typeMismatch.user.id=the format of input is wrong
6 |
7 | username=user name
8 | password=password
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/jsp/hello.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 |
3 |
4 |
5 |
6 | Hello
7 |
8 |
9 | Hello JSP!
10 |
11 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/jsp/error.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 |
3 |
4 |
5 |
6 | Insert title here
7 |
8 |
9 | Error!
10 |
11 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/mvc/integrate/UserService.java:
--------------------------------------------------------------------------------
1 | package com.spring.mvc.integrate;
2 |
3 | import org.springframework.stereotype.Component;
4 |
5 | /**
6 | * User Service
7 | *
8 | * @author Lian
9 | * @date 2016年5月18日
10 | * @since 1.0
11 | */
12 | @Component
13 | public class UserService {
14 |
15 | public UserService() {
16 | System.out.println("UserService Constructor...");
17 | }
18 |
19 | public void save() {
20 | System.out.println("save");
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/jsp/local.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 |
3 |
4 |
5 |
6 | Insert title here
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/jsp/show.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 | <%@ page isELIgnored="false" %>
3 |
4 |
5 |
6 |
7 | Show
8 |
9 |
10 | Show Page
11 | name: ${p.name}
12 | age: ${p.age}
13 |
14 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/mvc/model/Person.java:
--------------------------------------------------------------------------------
1 | package com.spring.mvc.model;
2 |
3 | /**
4 | * Person Model
5 | *
6 | * @author Lian
7 | * @date 2016年5月17日
8 | * @since 1.0
9 | */
10 | public class Person {
11 |
12 | private String name;
13 | private int age;
14 |
15 | public String getName() {
16 | return name;
17 | }
18 |
19 | public void setName(String name) {
20 | this.name = name;
21 | }
22 |
23 | public int getAge() {
24 | return age;
25 | }
26 |
27 | public void setAge(int age) {
28 | this.age = age;
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/jsp/showUser.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 | <%@ page isELIgnored="false" %>
3 |
4 |
5 |
6 |
7 | Show User
8 |
9 |
10 | Show User Page
11 | id: ${user.id}
12 | name: ${user.name}
13 | age: ${user.birth}
14 |
15 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/jsp/upload.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 |
3 |
4 |
5 |
6 | upload
7 |
8 |
9 |
13 |
14 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/mvc/model/Rest.java:
--------------------------------------------------------------------------------
1 | package com.spring.mvc.model;
2 |
3 | /**
4 | * Rest Model
5 | *
6 | * @author Lian
7 | * @date 2016年5月17日
8 | * @since 1.0
9 | */
10 | public class Rest {
11 |
12 | private String method;
13 | private Integer id;
14 |
15 | public Rest(String method, Integer id) {
16 | super();
17 | this.method = method;
18 | this.id = id;
19 | }
20 |
21 | public String getMethod() {
22 | return method;
23 | }
24 |
25 | public void setMethod(String method) {
26 | this.method = method;
27 | }
28 |
29 | public Integer getId() {
30 | return id;
31 | }
32 |
33 | public void setId(Integer id) {
34 | this.id = id;
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/mvc/controller/AdviceController.java:
--------------------------------------------------------------------------------
1 | package com.spring.mvc.controller;
2 |
3 | import org.springframework.web.bind.annotation.ControllerAdvice;
4 | import org.springframework.web.bind.annotation.ExceptionHandler;
5 | import org.springframework.web.servlet.ModelAndView;
6 |
7 | /**
8 | * 控制器: 处理全局异常(所有Controller)
9 | *
10 | * @author Lian
11 | * @date 2016年5月17日
12 | * @since 1.0
13 | */
14 | @ControllerAdvice
15 | public class AdviceController {
16 |
17 | @ExceptionHandler
18 | public ModelAndView exceptionHandler(Exception ex) {
19 | ModelAndView mv = new ModelAndView("error");
20 | mv.addObject("exception", ex);
21 | System.out.println("in AdviceController");
22 | return mv;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/mvc/controller/JsonController.java:
--------------------------------------------------------------------------------
1 | package com.spring.mvc.controller;
2 |
3 | import java.util.Date;
4 |
5 | import org.springframework.stereotype.Controller;
6 | import org.springframework.web.bind.annotation.RequestMapping;
7 | import org.springframework.web.bind.annotation.ResponseBody;
8 |
9 | import com.spring.mvc.integrate.User;
10 |
11 | /**
12 | * 控制器: 返回json格式的字符串
13 | *
14 | * @author Lian
15 | * @date 2016年5月17日
16 | * @since 1.0
17 | */
18 | @Controller
19 | @RequestMapping("/json")
20 | public class JsonController {
21 |
22 | @ResponseBody
23 | @RequestMapping("/user")
24 | public User get() {
25 | User user = new User();
26 | user.setId(1);
27 | user.setName("jayjay");
28 | user.setBirth(new Date());
29 | return user;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/jsp/addUser.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 | <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
3 |
4 |
5 |
6 |
7 | Form
8 |
9 |
10 |
11 | id:
12 | name:
13 | birth:
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/jsp/ajax.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 |
3 |
4 |
5 |
6 |
7 | Ajax Page
8 |
9 |
10 | Ajax Page!
11 | name:
12 |
13 |
14 |
15 |
26 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/mvc/integrate/UserController.java:
--------------------------------------------------------------------------------
1 | package com.spring.mvc.integrate;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.stereotype.Controller;
5 | import org.springframework.web.bind.annotation.ModelAttribute;
6 | import org.springframework.web.bind.annotation.RequestBody;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 |
9 | /**
10 | * User Controller
11 | *
12 | * @author Lian
13 | * @date 2016年5月18日
14 | * @since 1.0
15 | */
16 | @Controller
17 | @RequestMapping("/integrate")
18 | public class UserController {
19 |
20 | @Autowired
21 | private UserService userService;
22 |
23 | @RequestMapping("/user")
24 | public String saveUser(@RequestBody @ModelAttribute User user) {
25 | System.out.println(user);
26 | userService.save();
27 | return "hello";
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/mvc/controller/MvcController1.java:
--------------------------------------------------------------------------------
1 | package com.spring.mvc.controller;
2 |
3 | import org.springframework.stereotype.Controller;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 | import org.springframework.web.bind.annotation.RequestParam;
6 |
7 | /**
8 | * 控制器: 使用@RequestParam注解指定参数的name
9 | *
10 | * @author Lian
11 | * @date 2016年5月17日
12 | * @since 1.0
13 | */
14 | @Controller
15 | @RequestMapping("/test")
16 | public class MvcController1 {
17 |
18 | /**
19 | * @url: http://localhost:8080/spring-mvc-action-tutorial/test/param?id=1&name=lian
20 | *
21 | * @param id
22 | * @param name
23 | * @return
24 | */
25 | @RequestMapping("/param")
26 | public String testRequestParam(@RequestParam(value="id") Integer id, @RequestParam(value="name") String name) {
27 | System.out.println(id + " " + name);
28 | return "/hello";
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/jsp/rest.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 |
3 |
4 |
5 |
6 | Rest Page
7 |
8 |
9 | Rest Page
10 |
11 |
15 |
16 |
19 |
20 |
23 |
24 |
28 |
29 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/mvc/controller/ExceptionController.java:
--------------------------------------------------------------------------------
1 | package com.spring.mvc.controller;
2 |
3 | import org.springframework.stereotype.Controller;
4 | import org.springframework.web.bind.annotation.ExceptionHandler;
5 | import org.springframework.web.bind.annotation.RequestMapping;
6 | import org.springframework.web.servlet.ModelAndView;
7 |
8 | /**
9 | * 控制器: 处理局部异常(Controller内)
10 | *
11 | * @author Lian
12 | * @date 2016年5月17日
13 | * @since 1.0
14 | */
15 | @Controller
16 | @RequestMapping("/exception")
17 | public class ExceptionController {
18 |
19 | @ExceptionHandler
20 | public ModelAndView exceptionHandler(Exception ex) {
21 | ModelAndView mv = new ModelAndView("error");
22 | mv.addObject("exception", ex);
23 | System.out.println("in testExceptionHandler");
24 | return mv;
25 | }
26 |
27 | @RequestMapping("/error")
28 | public String error() {
29 | int i = 5 / 0;
30 | // just for clear warning of unused
31 | i = i + 1;
32 | return "hello";
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/mvc/integrate/User.java:
--------------------------------------------------------------------------------
1 | package com.spring.mvc.integrate;
2 |
3 | import java.util.Date;
4 |
5 | import javax.validation.constraints.Past;
6 |
7 | import org.hibernate.validator.constraints.NotEmpty;
8 | import org.springframework.format.annotation.DateTimeFormat;
9 |
10 | /**
11 | * User Model
12 | *
13 | * @author Lian
14 | * @date 2016年5月18日
15 | * @since 1.0
16 | */
17 | public class User {
18 |
19 | private int id;
20 | @NotEmpty
21 | private String name;
22 | @Past
23 | @DateTimeFormat(pattern="yyyy-MM-dd")
24 | private Date birth;
25 |
26 | public int getId() {
27 | return id;
28 | }
29 |
30 | public void setId(int id) {
31 | this.id = id;
32 | }
33 |
34 | public String getName() {
35 | return name;
36 | }
37 |
38 | public void setName(String name) {
39 | this.name = name;
40 | }
41 |
42 | public Date getBirth() {
43 | return birth;
44 | }
45 |
46 | public void setBirth(Date birth) {
47 | this.birth = birth;
48 | }
49 |
50 | @Override
51 | public String toString() {
52 | return "User [id=" + id + ", name=" + name + ", birth=" + birth + "]";
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/resources/applicationContext.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/mvc/controller/FormController.java:
--------------------------------------------------------------------------------
1 | package com.spring.mvc.controller;
2 |
3 | import java.util.Map;
4 |
5 | import javax.validation.Valid;
6 |
7 | import org.springframework.stereotype.Controller;
8 | import org.springframework.validation.BindingResult;
9 | import org.springframework.web.bind.annotation.RequestMapping;
10 | import org.springframework.web.bind.annotation.RequestMethod;
11 |
12 | import com.spring.mvc.integrate.User;
13 |
14 | /**
15 | * 控制器: 表单的验证--使用Hibernate-validate
16 | *
17 | * @author Lian
18 | * @date 2016年5月18日
19 | * @since 1.0
20 | */
21 | @Controller
22 | @RequestMapping("/form")
23 | public class FormController {
24 |
25 | @RequestMapping("/view")
26 | public String hello() {
27 | return "addUser";
28 | }
29 |
30 | @RequestMapping(value="/add", method=RequestMethod.POST)
31 | public String add(@Valid User user, BindingResult br) {
32 | if(br.getErrorCount() > 0) {
33 | return "addUser";
34 | }
35 | return "showUser";
36 | }
37 |
38 | @RequestMapping(value="/add", method=RequestMethod.GET)
39 | public String add(Map map) {
40 | map.put("user", new User());
41 | return "addUser";
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/mvc/interceptor/MyInterceptor.java:
--------------------------------------------------------------------------------
1 | package com.spring.mvc.interceptor;
2 |
3 | import javax.servlet.http.HttpServletRequest;
4 | import javax.servlet.http.HttpServletResponse;
5 |
6 | import org.springframework.web.servlet.HandlerInterceptor;
7 | import org.springframework.web.servlet.ModelAndView;
8 |
9 | /**
10 | * 自定义拦截器
11 | *
12 | * @author Lian
13 | * @date 2016年4月11日
14 | * @since 1.0
15 | */
16 | public class MyInterceptor implements HandlerInterceptor {
17 |
18 | /**
19 | *
20 | * @param request
21 | * @param response
22 | * @param obj
23 | * @param ex
24 | * @throws Exception
25 | */
26 | @Override
27 | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception ex) throws Exception {
28 | System.out.println("afterCompletion");
29 | }
30 |
31 | /**
32 | *
33 | * @param request
34 | * @param response
35 | * @param obj
36 | * @param mv
37 | * @throws Exception
38 | */
39 | @Override
40 | public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView mv) throws Exception {
41 | System.out.println("postHandler");
42 | }
43 |
44 | /**
45 | *
46 | * @param request
47 | * @param response
48 | * @param obj
49 | * @return
50 | * @throws Exception
51 | */
52 | @Override
53 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
54 | System.out.println("preHandle");
55 | return true;
56 | }
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/mvc/controller/RestController.java:
--------------------------------------------------------------------------------
1 | package com.spring.mvc.controller;
2 |
3 | import org.springframework.web.bind.annotation.PathVariable;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 | import org.springframework.web.bind.annotation.RequestMethod;
6 |
7 | import com.spring.mvc.model.Rest;
8 |
9 | /**
10 | * 控制器: RESTFul风格的SpringMVC
11 | * @attention 响应应该是json格式
12 | * @attention @RestController名字相同
13 | *
14 | * @author Lian
15 | * @date 2016年5月17日
16 | * @since 1.0
17 | */
18 | @org.springframework.web.bind.annotation.RestController
19 | @RequestMapping("/rest")
20 | public class RestController {
21 |
22 | /**
23 | * http://localhost:8080/spring-mvc-action-tutorial/mvc/page/rest
24 | *
25 | * @param id
26 | * @return
27 | */
28 | @RequestMapping(value="/user/{id}", method=RequestMethod.GET)
29 | public Rest get(@PathVariable("id") Integer id) {
30 | Rest rest = new Rest("GET", id);
31 | return rest;
32 | }
33 |
34 | @RequestMapping(value="/user/{id}", method=RequestMethod.POST)
35 | public Rest post(@PathVariable("id") Integer id) {
36 | Rest rest = new Rest("GET", id);
37 | return rest;
38 | }
39 |
40 | @RequestMapping(value="/user/{id}", method=RequestMethod.PUT)
41 | public Rest put(@PathVariable("id") Integer id) {
42 | Rest rest = new Rest("GET", id);
43 | return rest;
44 | }
45 |
46 | @RequestMapping(value="/user/{id}", method=RequestMethod.DELETE)
47 | public Rest delete(@PathVariable("id") Integer id) {
48 | Rest rest = new Rest("GET", id);
49 | return rest;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 史上最全最强SpringMVC详细示例实战教程
2 |
3 | # http://www.cnblogs.com/sunniest/p/4555801.html#commentform
4 |
5 | ## 配置解析
6 |
7 | 1. DispatcherServlet
8 | DispatcherServlet是前置控制器, 配置在web.xml文件中. 拦截匹配的请求, Servlet拦截匹配规则要自己定义, 把拦截下来的请求, 依据相应的规则分发到目标Controller来处理, 是配置SpringMVC的第一步.
9 |
10 | 2. InternalResourceViewResolver
11 | 视图名称解析器
12 |
13 | 3. 以上出现的注解
14 | @Controller: 负责注册一个bean到spring上下文中
15 | @RequestMapping: 注解为控制器指定可以处理哪些URL请求
16 |
17 |
18 |
19 | ## SpringMVC常用注解
20 |
21 | - @Controller
22 | 负责注册一个bean到spring上下文中
23 |
24 | - @RequestMapping
25 | 注解为控制器指定可以处理哪些URL请求
26 |
27 | - @RequestBody
28 | 该注解用于读取Request请求的body部分数据, 使用系统默认配置的HttpMessageConverter进行解析, 然后把相应的数据绑定到要返回的对象上, 再把HttpMessageConverter返回的对象数据绑定到controller中方法的参数上
29 |
30 | - @ResponseBody
31 | 该注解用于将Controller中方法返回的对象, 通过适当的HttpMessageConverter转换为指定格式后, 写入到Response对象的body数据区
32 |
33 | - @ModelAttribute
34 | 在方法定义上使用@ModelAttribute 注解: SpringMVC在调用目标处理方法前, 会先逐个调用在方法级上标注了@ModelAttribute的方法
35 | 在方法的入参前使用 @ModelAttribute 注解:可以从隐含对象中获取隐含的模型数据中获取对象,再将请求参数 –绑定到对象中,再传入入参将方法入参对象添加到模型中
36 |
37 | - @RequestParam
38 | 在处理方法入参处使用@RequestParam 可以把请求参数传递给请求方法
39 |
40 | - @PathVariable
41 | 绑定URL占位符到入参
42 |
43 | - @ExceptionHandler
44 | 注解到方法上, 出现异常时会执行该方法
45 |
46 | - @ControllerAdvice
47 | 使一个Controller成为全局的异常处理类, 类中用ExceptinHandler方法注解的方法可以处理所有Controller发生的异常
48 |
49 |
50 | ## SpringMVC常用注解
51 | 1. 客户端请求提交到DispatcherServlet
52 | 2. 由DispatcherServlet控制器查询一个或多个HandlerMapping, 找到处理请求的Controller
53 | 3. DispatcherServlet将请求提交到Controller
54 | 4. Controller调用业务逻辑处理后,返回ModelAndView
55 | 5. DispatcherServlet查询一个或多个ViewResoler视图解析器,找到ModelAndView指定的视图
56 | 6. 视图负责将结果显示到客户端
57 |
58 | ## 关于jar包的问题
59 | * 本项目使用maven管理jar包,因此没有libjar包之类的
60 | * maven的基本使用并不难,希望大家可以抽出30分钟研究一下
61 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 | Archetype Created Web Application
7 |
8 |
9 | contextConfigLocation
10 | classpath:applicationContext.xml
11 |
12 |
13 |
14 |
17 |
18 | org.springframework.web.context.ContextLoaderListener
19 |
20 |
21 |
22 |
23 |
24 | HiddenHttpMethodFilter
25 | org.springframework.web.filter.HiddenHttpMethodFilter
26 |
27 |
28 | HiddenHttpMethodFilter
29 | /*
30 |
31 |
32 |
33 |
34 | springmvc
35 | org.springframework.web.servlet.DispatcherServlet
36 |
37 | contextConfigLocation
38 | classpath:springmvc-servlet.xml
39 |
40 | 1
41 |
42 |
43 | springmvc
44 | /
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/src/main/resources/springmvc-servlet.xml:
--------------------------------------------------------------------------------
1 |
2 |
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 | error
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | com.spring.mvc
6 | spring-mvc-action-tutorial
7 | war
8 | 0.0.1-SNAPSHOT
9 |
10 | notes Maven Webapp
11 | http://maven.apache.org
12 |
13 |
14 |
15 | 4.1.6.RELEASE
16 |
17 |
18 |
19 |
20 |
21 | org.springframework
22 | spring-aop
23 | ${spring.version}
24 |
25 |
26 | org.springframework
27 | spring-beans
28 | ${spring.version}
29 |
30 |
31 | org.springframework
32 | spring-context
33 | ${spring.version}
34 |
35 |
36 | org.springframework
37 | spring-core
38 | ${spring.version}
39 |
40 |
41 | org.springframework
42 | spring-expression
43 | ${spring.version}
44 |
45 |
46 | org.springframework
47 | spring-web
48 | ${spring.version}
49 |
50 |
51 | org.springframework
52 | spring-webmvc
53 | ${spring.version}
54 |
55 |
56 |
57 |
58 | commons-logging
59 | commons-logging
60 | 1.2
61 |
62 |
63 |
64 |
65 | commons-fileupload
66 | commons-fileupload
67 | 1.3.1
68 |
69 |
70 | commons-io
71 | commons-io
72 | 2.4
73 |
74 |
75 |
76 |
77 | javax.servlet
78 | javax.servlet-api
79 | 3.1.0
80 | provided
81 |
82 |
83 |
84 |
85 | com.fasterxml.jackson.core
86 | jackson-annotations
87 | 2.2.1
88 |
89 |
90 | com.fasterxml.jackson.core
91 | jackson-core
92 | 2.2.1
93 |
94 |
95 | com.fasterxml.jackson.core
96 | jackson-databind
97 | 2.2.1
98 |
99 |
100 |
101 |
102 | com.fasterxml
103 | classmate
104 | 1.0.0
105 |
106 |
107 | org.jboss.logging
108 | jboss-logging
109 | 3.1.3.GA
110 |
111 |
112 | javax.validation
113 | validation-api
114 | 1.1.0.Final
115 |
116 |
117 | org.hibernate
118 | hibernate-validator
119 | 5.1.3.Final
120 |
121 |
122 | org.hibernate
123 | hibernate-validator-annotation-processor
124 | 5.1.3.Final
125 |
126 |
127 |
128 |
129 | spring-mvc-action-tutorial
130 |
131 |
132 |
--------------------------------------------------------------------------------
/src/main/java/com/spring/mvc/controller/MvcController.java:
--------------------------------------------------------------------------------
1 | package com.spring.mvc.controller;
2 |
3 | import java.io.FileOutputStream;
4 | import java.io.IOException;
5 | import java.io.PrintWriter;
6 | import java.text.SimpleDateFormat;
7 | import java.util.Date;
8 |
9 | import javax.servlet.http.HttpServletRequest;
10 |
11 | import org.springframework.beans.propertyeditors.CustomDateEditor;
12 | import org.springframework.stereotype.Controller;
13 | import org.springframework.ui.Model;
14 | import org.springframework.web.bind.ServletRequestDataBinder;
15 | import org.springframework.web.bind.annotation.InitBinder;
16 | import org.springframework.web.bind.annotation.PathVariable;
17 | import org.springframework.web.bind.annotation.RequestMapping;
18 | import org.springframework.web.bind.annotation.RequestMethod;
19 | import org.springframework.web.multipart.MultipartFile;
20 | import org.springframework.web.multipart.MultipartHttpServletRequest;
21 |
22 | import com.spring.mvc.model.Person;
23 |
24 | /**
25 | * Mvc Controller
26 | *
27 | * @author Lian
28 | * @date 2016年5月17日
29 | * @since 1.0
30 | */
31 | @Controller
32 | @RequestMapping("/mvc")
33 | public class MvcController {
34 |
35 | /**
36 | * @url: http://localhost:8080/spring-mvc-action-tutorial/mvc/hello
37 | *
38 | * @return
39 | */
40 | @RequestMapping("/hello")
41 | public String hello() {
42 | return "hello";
43 | }
44 |
45 | /**
46 | * 自动匹配参数: match automatically
47 | * @url: http://localhost:8080/spring-mvc-action-tutorial/mvc/person?name=lian&age=2.4
48 | *
49 | * @param name
50 | * @param age
51 | * @return
52 | */
53 | @RequestMapping("/person")
54 | public String toPerson(String name, double age) {
55 | System.out.println(name + " " + age);
56 | return "hello";
57 | }
58 |
59 | /**
60 | * 自动装箱: boxing automatically
61 | * @url: http://localhost:8080/spring-mvc-action-tutorial/mvc/person1?name=lian&age=2
62 | * http://localhost:8080/spring-mvc-action-tutorial/mvc/person1?name=lian&age=2.0
63 | * @attention: 当参数类型不匹配时, 不会自动装箱
64 | *
65 | * @param p
66 | * @return
67 | */
68 | @RequestMapping("/person1")
69 | public String toPerson(Person p) {
70 | System.out.println(p.getName() + " " + p.getAge());
71 | return "hello";
72 | }
73 |
74 | /**
75 | * 使用InitBinder来处理Date类型的参数
76 | * the parameter was coverted in initBinder
77 | * @url: http://localhost:8080/spring-mvc-action-tutorial/mvc/date?date=2016-05-17
78 | * http://localhost:8080/spring-mvc-action-tutorial/mvc/date?date=2016-05
79 | * http://localhost:8080/spring-mvc-action-tutorial/mvc/date?date=2016-05-17 15:00:00
80 | *
81 | * @param date
82 | * @return
83 | */
84 | @RequestMapping("/date")
85 | public String date(Date date) {
86 | System.out.println(date);
87 | return "hello";
88 | }
89 |
90 | /**
91 | * At the time of initialization, convert the type "String" to type "date"
92 | *
93 | * @param binder
94 | */
95 | @InitBinder
96 | public void initBinder(ServletRequestDataBinder binder) {
97 | System.out.println("InitBinder of date : convert the type of String to type date.");
98 | binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
99 | }
100 |
101 | /**
102 | * 向前台传递参数: pass the parameters to front-end
103 | * 前台可在Request域中取到"p", 另外, 使用HttpServletRequest.setAttribute()方法也可以实现向前台传递参数
104 | * @url: http://localhost:8080/spring-mvc-action-tutorial/mvc/show
105 | *
106 | * @param map
107 | * @return
108 | */
109 | @RequestMapping("/show")
110 | public String showPerson(Model model) {
111 | Person p = new Person();
112 | p.setAge(20);
113 | p.setName("lian");
114 | model.addAttribute("p", p);
115 | return "show";
116 | }
117 |
118 | /**
119 | * 使用Ajax调用: pass the parameters to front-en using ajax
120 | * url: http://localhost:8080/spring-mvc-action-tutorial/mvc/page/ajax
121 | *
122 | * @param name
123 | * @param pw
124 | */
125 | @RequestMapping(value = "/getPerson", method = RequestMethod.POST)
126 | public void getPerson(String name, PrintWriter pw) {
127 | // 也可以使用HttpServletResponse.getWriter()来获取PrintWriter
128 | pw.write("hello, " + name);
129 | }
130 |
131 | /**
132 | * 在Controller中使用redirect方式处理请求
133 | * @url: http://localhost:8080/spring-mvc-action-tutorial/mvc/redirect
134 | *
135 | * @return
136 | */
137 | @RequestMapping("/redirect")
138 | public String redirect() {
139 | return "redirect:hello";
140 | }
141 |
142 | /**
143 | * 文件上传
144 | * @url http://localhost:8080/spring-mvc-action-tutorial/mvc/page/upload
145 | * TODO webapp下的upload文件夹没有部署, fuck. 后来再弄
146 | *
147 | * @param request
148 | * @return
149 | * @throws IOException
150 | */
151 | @RequestMapping(value = "/upload", method = RequestMethod.POST)
152 | public String upload(HttpServletRequest request) throws IOException {
153 | MultipartHttpServletRequest mreq = (MultipartHttpServletRequest) request;
154 | MultipartFile file = mreq.getFile("file");
155 | String fileName = file.getOriginalFilename();
156 | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
157 | FileOutputStream fos = new FileOutputStream(request.getSession().getServletContext().getRealPath("/") + "upload/" + sdf.format(new Date())
158 | + fileName.substring(fileName.lastIndexOf(".")));
159 | fos.write(file.getBytes());
160 | fos.flush();
161 | fos.close();
162 |
163 | return "hello";
164 | }
165 |
166 | /**
167 | * 页面跳转接口
168 | * url: http://localhost:8080/spring-mvc-action-tutorial/mvc/page/{page}
169 | *
170 | * @param page
171 | * @return
172 | */
173 | @RequestMapping(value = "/page/{page}", method = RequestMethod.GET)
174 | public String page(@PathVariable("page") String page) {
175 | return page;
176 | }
177 | }
178 |
--------------------------------------------------------------------------------