spring mvc
在https://mvnrepository.com/查找maven库里spring的包.
围绕DispatchServlet来设计,所有web请求都会到这个类.
从spring 2.5,java5或以上的用户就可以使用基于注解的control声明方式.
老的方式:
1. 在pom.xml里导入Spring依赖
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.14</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.14</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies>
2. 在web.xml中配置DispatchServelet,MVC的配置文件,以及DispatchServlet的mapping.
<servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置Spring mvc下的配置文件的位置和名称 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!--启动级别--> <load-on-startup>1</load-on-startup> </servlet> <!-- /配置所有的请求,不包括.jsp--> <!-- /*配置所有的请求,包括.jsp--> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
3. 在resources下添加文件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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!--添加处理映射器--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!--添加处理器适配器--> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name = "prefix" value="/WEB-INF/views/"></property> <property name = "suffix" value = ".jsp"></property> </bean> <!--Handler--> <bean id="/hello" class="com.ptc.springmvc.handler.HelloWorld"/> </beans>
4.创建controller
package com.ptc.springmvc.handler; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloWorld implements Controller { public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception { //模型和视图 ModelAndView mv = new ModelAndView(); //封装对象,放在ModelAndView中,Model mv.addObject("msg","HelloSpringMVC!"); //封装要跳转的视图,放在ModelAndView中 mv.setViewName("hello"); //: /WEB-INF/jsp/hello.jsp return mv; } }
5. 在WEB-INF/jsp下创建hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
新的方式:
1. 在pom.xml里导入Spring依赖
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.14</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.14</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies>
2. 在web.xml中配置DispatchServelet,MVC的配置文件,以及DispatchServlet的mapping.
<servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置Spring mvc下的配置文件的位置和名称 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!--启动级别--> <load-on-startup>1</load-on-startup> </servlet> <!-- /配置所有的请求,不包括.jsp--> <!-- /*配置所有的请求,包括.jsp--> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
3.在resources下添加文件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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自动扫描的包,让指定报下名的注解生效,由IOC容器统一管理--> <context:component-scan base-package="com.ptc.springmvc"></context:component-scan> <!--让spring mvc不处理静态资源 .css,.js,.html,.mp3,.mp4--> <mvc:default-servlet-handler/> <!--支持mvc注解驱动 在spring中一般采用@RequestMapping来完成映射关系 要想使@RequestMapping注解生效 必须向上下文中注册DefaultAnnotationHandlerMapping 和一个AnnotationMethodHandlerAdapter实例, 这两个实例分布在类和方法级别处理 而annotation-driven配置帮助我们自动完成上面两个实例的注入 我没有配置这个也可以运行 --> <mvc:annotation-driven/> <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name = "prefix" value="/WEB-INF/views/"></property> <property name = "suffix" value = ".jsp"></property> </bean> </beans>
4.创建controller
package com.ptc.springmvc.handler; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorld { /** *1. 使用RequestMapping注解来映射请求的URL * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于InternalResourceViewResolver视图解析器,会做如下解析 * 通过prefix+returnVal+suffix 这样的方式得到实际的物理视图,然后会转发操作 * "/WEB-INF/views/success.jsp" * @return * */ @RequestMapping("/helloworld") public String hello(Model model){ //封装数据 model.addAttribute("msg","hello springmvc!"); return "success"; } }
5.创建success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
${msg}
</head>
<body>
</body>
</html>
转发和重定向
1. 使用HttpServletResponse来实现转发和重定向
package com.ptc.springmvc; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.RegEx; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Controller public class Redirection { @RequestMapping("/test1") public void test1(HttpServletRequest request, HttpServletResponse response) throws IOException { response.getWriter().println("test1....."); } @RequestMapping("/test2") public void test2(HttpServletRequest request,HttpServletResponse response) throws IOException { response.sendRedirect("/springmvc-04-redirection/views/test2.jsp"); } @RequestMapping("/test3") public void test3(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { request.setAttribute("msg","test3....."); request.getRequestDispatcher("/WEB-INF/views/test3.jsp").forward(request,response); } }
2. 使用Springmvc来转发和重定向 -无需视图解析器
要去掉springmvc.xml里配置的视图解析器
这里可以看到forward可以使用WEB-INF下面的jsp,而redirect不能.
WEB-INF是Java的WEB应用的安全目录。所谓安全就是客户端无法访问,只有服务端可以访问的目录。
页面放在WEB-INF目录下面,这样可以限制访问,提高安全性.如JSP,html
既然WEB-INF是安全目录,客户端无法访问,而重定向就相当于用户直接从客户端访问了的路径,自然就不可以啦,只有程序内部转发的时候才能转发到WEB-INF下的JSP。
package com.ptc.springmvc; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class RedirectionMVC { @RequestMapping("/rm/t1") public String test1(){ //转发 return "/views/test2.jsp"; } @RequestMapping("rm/t2") public String test2(Model model){ model.addAttribute("msg","test3..."); //转发2 return "forward:/WEB-INF/views/test3.jsp"; } @RequestMapping("rm/t3") public String test3(){ //重定向 return "redirect:/views/test2.jsp"; } }
处理提交数据
1. 提交的域名称和处理方法的参数名称一致
提交数据:http://localhost:8080//springmvc_05_receivedata/hello?name=roger
package com.ptc.receivedata; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class ReceiveData { @RequestMapping("/hello") public String hello(String name) { System.out.println("name>>>>>>>>>>>" + name); return "hello"; } }
后台打印
2. 提交的域名称和处理方法的参数名称不一致
提交数据:http://localhost:8080//springmvc_05_receivedata/hello?username=roger
使用@RequestParam("username")来指定name对应的域名称.
@Controller public class ReceiveData { //@RequestParam("username") @RequestMapping("/hello2") public String hello2(@RequestParam("username") String name) { System.out.println("name2>>>>>>>>>>>" + name); return "hello"; } }
3. 提交一个对象
提交数据:http://localhost:8080//springmvc_05_receivedata/test?id=1&name=roger&age=36
创建一个实体类
package com.ptc.dojo; public class User { private int id; private String name; private int age; public User(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public User() { } 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 getAge() { return age; } public void setAge(int age) { this.age = age; } }
UserController.java 使用User对象参数,传过来的参数的跟它的属性名字一一对应.
package com.ptc.receivedata; import com.ptc.dojo.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class UserController { @RequestMapping("/test") public String test(User user) { System.out.println("id:" + user.getId() + "|name:" + user.getName() + "|age:" + user.getAge()); return "hello"; } }
结果
数据显示到前端
1. 通过ModelAndView
public class TestController { @RequestMapping("/test") public ModelAndView test() { ModelAndView mv = new ModelAndView(); mv.addObject("msg","test!!!"); mv.setViewName("test"); return mv; } @RequestMapping("/test1") public ModelAndView test1() { ModelAndView mv = new ModelAndView(); mv.addObject("msg","redirect test!!!"); mv.setViewName("redirect:/views/test.jsp"); return mv; } }
2. 通过ModelMap
@RequestMapping("/test2") public String test2(@RequestParam("username") String name, ModelMap modelMap) { modelMap.addAttribute("name",name); return "test2"; }
3. 通过Model
@RequestMapping("/helloworld") public String hello(Model model){ //封装数据 model.addAttribute("msg","hello springmvc!"); return "success"; }
乱码问题
1.重现乱码
form.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/springmvc_05_receivedata/e/t1" method="post"> <input type="text" name="name"> <input type="submit"> </form> </body> </html>
EncodingController.java
package com.ptc.receivedata; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PostMapping; @Controller public class EncodingController { @PostMapping("/e/t1") public String test1(String name,Model model){ model.addAttribute("name",name); return "test2"; } }
输入url:http://localhost:8080//springmvc_05_receivedata/form.jsp
提交之后出现乱码:
2. 解决办法
1) 最烂的操作方法就是自己写过滤器
具体操作如下:
- 新建一个类,EncodingFilter.java,需要继承Filter
package com.ptc.filter; import javax.servlet.*; import java.io.IOException; public class EncodingFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); chain.doFilter(request,response); } @Override public void destroy() { } }
- 去web.xml配置注册
<filter> <filter-name>encoding</filter-name> <filter-class>com.ptc.filter.EncodingFilter</filter-class> </filter> <!--过滤所有的请求--> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2) SpringMVC提供的过滤器,用配置web.xml的方法(一般用这种方法就可以解决乱码问题了)
<!--配置springmvc的乱码过滤器--> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3) 要是还乱码,处理方法:
- 修改tomcat配置文件,设置编码
找到tomcat安装路径,找到service.xml文件
添加 URLEncoding=”UTF-8”
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URLEncoding="UTF-8"/>
- 自定义过滤器
package com.ptc.filter; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Map; /** * 解决get和post请求 全部乱码的过滤器 */ public class GenericEncodingFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //处理response的字符编码 HttpServletResponse myResponse=(HttpServletResponse) response; myResponse.setContentType("text/html;charset=UTF-8"); // 转型为与协议相关对象 HttpServletRequest httpServletRequest = (HttpServletRequest) request; // 对request包装增强 HttpServletRequest myrequest = new MyRequest(httpServletRequest); chain.doFilter(myrequest, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { } } //自定义request对象,HttpServletRequest的包装类 class MyRequest extends HttpServletRequestWrapper { private HttpServletRequest request; //是否编码的标记 private boolean hasEncode; //定义一个可以传入HttpServletRequest对象的构造函数,以便对其进行装饰 public MyRequest(HttpServletRequest request) { super(request);// super必须写 this.request = request; } // 对需要增强方法 进行覆盖 @Override public Map getParameterMap() { // 先获得请求方式 String method = request.getMethod(); if (method.equalsIgnoreCase("post")) { // post请求 try { // 处理post乱码 request.setCharacterEncoding("utf-8"); return request.getParameterMap(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } else if (method.equalsIgnoreCase("get")) { // get请求 Map<String, String[]> parameterMap = request.getParameterMap(); if (!hasEncode) { // 确保get手动编码逻辑只运行一次 for (String parameterName : parameterMap.keySet()) { String[] values = parameterMap.get(parameterName); if (values != null) { for (int i = 0; i < values.length; i++) { try { // 处理get乱码 values[i] = new String(values[i] .getBytes("ISO-8859-1"), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } } hasEncode = true; } return parameterMap; } return super.getParameterMap(); } //取一个值 @Override public String getParameter(String name) { Map<String, String[]> parameterMap = getParameterMap(); String[] values = parameterMap.get(name); if (values == null) { return null; } return values[0]; // 取回参数的第一个值 } //取所有值 @Override public String[] getParameterValues(String name) { Map<String, String[]> parameterMap = getParameterMap(); String[] values = parameterMap.get(name); return values; } }
- 在web.xml配置filter
<filter> <filter-name>encoding</filter-name> <filter-class>com.ptc.filter.GenericEncodingFilter</filter-class> </filter> <!--过滤所有的请求--> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
乱码问题,需要平时多注意,在尽可能能设置编码的地方,都设置为统一编码 UTF-8!
JSON
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript"> var user = { name:"胡峰", age:3, sex:"男" } //将js对象转换为json对象 var json = JSON.stringify(user); console.log(json); console.log("=============="); //将json对象转换为js对象 var object = JSON.parse(json); console.log(object); </script> </head> <body> </body> </html>
比较好的json解析工具:Jackson,fastjson