SpringMVC官网文档链接:https://docs.spring.io/spring-framework/docs/current/reference/html/web.html
<!--父工程所需要依赖--> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2.1-b03</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1. 获取前端参数 String method = req.getParameter("method"); if (method.equals("add")){ req.getSession().setAttribute("msg","执行了add方法"); } if (method.equals("delete")){ req.getSession().setAttribute("msg","执行了delete方法"); } //2. 调用业务层 //3. 试图转发或者重定向 req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
<!--注册Servlet--> <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.chelsea.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <!--设置session默认时常--> <session-config> <session-timeout>15</session-timeout> </session-config> <!--欢迎页面--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
<?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"> <!--注册DispatcherServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--关联一个springmvc的配置文件:【servlet-name】:servlet.xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!--启动级别 设置为1--> <!--服务器初始启动可能会有部分请求需要处理,所以此处设置为1,服务器启动时,此分发器就启动--> <load-on-startup>1</load-on-startup> </servlet> <!--/匹配所有的请求(不包括.jsp)--> <!--/*为匹配所有的请求(包括.jsp)--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置以下三个东西,前两个根本不需要配置--> <!--配置一个处理器--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!--配置一个适配器--> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!--配置一个视图解析器:解析DispatcherServlet给他的ModelAndView 1. 获取了ModelAndView的数据 2. 解析了ModelAndView的试图名字 3. 拼接试图名字,找到对应的视图 4. 将试图渲染到界面 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!--前缀--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--后缀--> <property name="suffix" value=".jsp"/> </bean> <!--未使用注解开发时,写一个控制器就得在这配置一下bean--> <bean id="/hello" class="com.chelsea.controller.HelloController"/> </beans>
/*首先实现SpringMVC的Controller接口*/ public class HelloController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { /*modelAndView*/ ModelAndView mv = new ModelAndView(); //封装对象,放到ModelAndView种 mv.addObject("msg","HelloSpringMVC!"); //封装要跳转的试图,放到ModelAndView中 //WEB-INF/jsp/hello.jsp mv.setViewName("hello"); return mv; } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} </body> </html>
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--配置扫描包,让指定包下的注解生效,由IOC容器统一管理--> <context:component-scan base-package="com.chelsea.controller"/> <!--配置Springmvc视图解析器不处理静态资源 .css .js .html .mp3 .mp4--> <mvc:default-servlet-handler/> <!-- 支持mvc注解驱动 在spring一般采用@RequestMapping注解来完成映射关系 要想使@RequestMapping注解生效,必须向上下文中注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter实例 这两个实例分别在类级别和方法级别处理,而annotation-driver配置帮助我们自动完成上述两个实例的注入。 就代替了之前自己配的这两个bean对象 --> <mvc:annotation-driven/> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!--前缀--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--后缀--> <property name="suffix" value=".jsp"/> </bean> </beans>
/*配置此注解,则不会被视图解析器解析,用于返回json格式的字符串*/ /*@RestController*/ @Controller public class HelloController { @RequestMapping("/hello") public String Hello(Model model){ //封装数据 model.addAttribute("msg","HelloServlet!"); /*这个返回的字符串会被视图解析器处理*/ return "hello"; } }
@Controller public class RestFulController { //使用RestFul风格,目前主要是@PathVaruable注解 //还可以指定这个注解,指定post方法 @PostMapping //还可以指定这个注解,指定Get方法 @GetMapping @PostMapping(value = "/add/{a}/{b}") public String test1(@PathVariable int a, @PathVariable int b, Model model){ int res = a+b; model.addAttribute("msg","结果1为:"+res); return "demo1"; } //通过这种方式,根据访问方式不同,来进行不同逻辑控制 @GetMapping(value = "/add/{a}/{b}") public String test2(@PathVariable int a, @PathVariable int b, Model model){ int res = a+b; model.addAttribute("msg","结果2为:"+res); return "demo1"; } }
@Controller @RequestMapping("/user") public class UserController { //@RequsetParam注解用来固定前端传参的对象名,来绑定到形参上,如果名称一样可省略,个人建议加上 @RequestMapping("/t1") public String test1(@RequestParam("username") String name, Model model){ //接收前端参数 System.out.println("接收到的前端数据为:"+name); //将返回的结果返回到前端 model.addAttribute("name",name); //跳转视图 return "demo1"; } /* * 1.接受前端用户传递的参数,判断参数的名字,假设名字直接在方法上,可以直接使用 * 2.假设传递的是一个User对象,则会自动匹配User对象中的字段名,如果一样则ok,否则为null * 实例:url=localhost/user/t1?id=3&username=chelsea&age=20----------------------->结果=User{id=3,name=null,age=20} * * */ //假如前端接受的是对象,可以直接将参数设置为对象,会自动匹配 //前端url为:localhost/user/t2?id=1&name=chelsea&age=19 @RequestMapping("/t2") public String test2(User user){ System.out.println(user); return "demo1"; } }
public class EncodingFilter extends HttpFilter { /*此处继承HttpFilter,也可以实现Filter接口,重写三个方法,init doFilter destory*/ /*记得在webxml文件中配置filter*/ @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); } }
<!--配置手写字符过滤filter,配置所有路径--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>com.chelsea.filter.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<!--配置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>
var obj = {a: 'Hello', b: 'World'}; //这是一个对象,注意键名也是可以使用引号包裹的 var json = '{"a": "Hello", "b": "World"}'; //这是一个 JSON 字符串,本质是一个字符串 //将JSON字符串转换为JS对象 var obj = JSON.parse('{"a": "Hello", "b": "World"}'); //结果是 {a: 'Hello', b: 'World'} //将JS对象转换为JSOn字符串 var json = JSON.stringify({a: 'Hello', b: 'World'}); //结果是 '{"a": "Hello", "b": "World"}'
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency>
//此处只展示了这两个包,其他的未展示而已 import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @Controller /*此注解作用:使本类下的所有方法都绕开视图解析器,返回字符串*/ @RestController public class UserController { /*@Responsebody配置后,就不会走视图解析器,会直接返回一个字符串*/ @ResponseBody @RequestMapping(value = "/j1",produces = "application/json;charset=utf-8") public String json1(){ /*使用jackson,转为json对象*/ ObjectMapper mapper = new ObjectMapper(); User user = new User(1,"韩信",14); String str = null; try { str = mapper.writeValueAsString(user); } catch (JsonProcessingException e) { e.printStackTrace(); } /* * 如果不适用json,则返回的user对象为:User(id=1, name=chelsea, age=14) * 使用json后,返回的json类型的对象为:{"id":1,"name":"chelsea","age":14} * * */ return str; } /*@Responsebody配置后,就不会走视图解析器,会直接返回一个字符串*/ @ResponseBody @RequestMapping(value = "/j2",produces = "application/json;charset=utf-8") public String json2(){ /*使用jackson,转为json对象*/ ObjectMapper mapper = new ObjectMapper(); List<User> userList = new ArrayList<User>(); User user = new User(1,"韩信",14); User user1 = new User(1,"韩信",14); User user2 = new User(1,"韩信",14); User user3 = new User(1,"韩信",14); userList.add(user); userList.add(user1); userList.add(user2); userList.add(user3); String str = null; try { str = mapper.writeValueAsString(userList); } catch (JsonProcessingException e) { e.printStackTrace(); } return str; } }
后续还有Ajax和拦截器,SSM整合等,未完待续!