javaSSM框架之springMVC框架核心总结
项目目录
/springmvc-servlet.xml核心配置(自定义文件名)
注意:base-package要和controller层保持一致,涵盖处理器映射器,处理器适配器,视图解析器
其余配置亘古不变,请放心使用
<?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.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> //固定不变 <context:component-scan base-package="com.huang.controller" /> <mvc:default-servlet-handler /> <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>12345678910111213141516171819202122
web.xml核心配置
org.springframework.web.servlet.DispatcherServlet和
org.springframework.web.filter.CharacterEncodingFilter是
springMVC提供的servlet调度器和编码过滤器
注意的是前者匹配所有请求,后者匹配所有请求和所有jsp页面
<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-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet><servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern></servlet-mapping><!-- <filter>--><!-- <filter-name>encoding</filter-name>--><!-- <filter-class>com.huang.filter.EncodingFilter</filter-class>--><!-- </filter>--><!-- <filter-mapping>--><!-- <filter-name>encoding</filter-name>--><!-- <url-pattern>/*</url-pattern>--><!-- </filter-mapping>--><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>1234567891011121314151617181920212223242526272829303132333435
controller示例
解析:
1.@Controller注解为控制器
2.return "hello"会根据/springmvc-servlet.xml中视图解析器的prefix和suffix来匹配url对应的前端页面,如:url为 ‘http://localhost:8080/hello/h1’则访问项目目录的/WEB-INF/jsp/hello.jsp
3.Model model;model.addAttribute(“msg”,“12121”);通常用来设置前端数据的回显,下文jackson将详细说明
@Controller@RequestMapping("hello")public class HelloController { @RequestMapping("h1") public String hello(Model model){ model.addAttribute("msg","12121"); return "hello"; } @RequestMapping("h2") public String hello1(Model model){ model.addAttribute("msg1","helloSpringMvc"); return "hello1"; }}123456789101112131415
/hello.jsp文件
${msg} 即对应 model.addAttribute(“msg”,“12121”);
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>Title</title></head><body>${msg}</body></html>123456789
controller层接受前端参数
前端代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>Title</title></head><body> <form action="/test1" method="post"> <input type="text" name="name"> <input type="submit"> </form></body></html>123456789101112
controller层
@Controllerpublic class EncodingController { @PostMapping("/test1") public String test1(String name, Model model) { System.out.println(name); model.addAttribute("msg",name); return "hello"; }}123456789
解析:
前端输入框的name再没使用jackson时,后台拿到的就是name
@PostMapping("/test1") :由于请求有多种方式,所以RequestMapping就对应有PostMapping/GetMapping/PutMapping等等,只要和前端的method="post"匹配即可,RequestMapping通配
public String test1(String name, Model model) {} 形参String name获取到前端传递的参数
jackson使用
1.补充项目目录
2.jar包
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.10.0</version></dependency><dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.10.0</version></dependency>12345678910
3.工具类JsonUtils.getJson类
//抽取出公共的getJson类public class JsonUtils { public static String getJson(Object object,String dateFormat){ //jackson中有ObjectMapper对象 ObjectMapper mapper = new ObjectMapper();// 不使用时间戳的方式 mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);// 自定义日期的格式 SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); mapper.setDateFormat(sdf); try { return mapper.writeValueAsString(object); } catch (JsonProcessingException e) { e.printStackTrace(); } return null; }}123456789101112131415161718
4.controller类示例
@Controllerpublic class JsonController { @RequestMapping("j3") @ResponseBody //不会执行任何视图,只返回一个字符串 public String json3() throws JsonProcessingException { List<User> userList = new ArrayList<User>(); userList.add(new User("黄杰1",20,"男")); userList.add(new User("黄杰2",20,"男")); userList.add(new User("黄杰3",20,"男")); userList.add(new User("黄杰4",20,"男")); return JsonUtils.getJson(userList,""); }}12345678910111213
5./springmvc-servlet.xml中配置解决json乱码(都是写死,可以存为模板)
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value="text/html;charset=UTF-8" /> </bean> <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes" value="application/json;charset=UTF-8" /> </bean> </mvc:message-converters></mvc:annotation-driven>12345678910
6.\效果图
到这里恭喜你,你已经开始进入接口的编写了
附录:jar包依赖
这些依赖包,基本贯穿所有springMVC全过程,可以存为模板
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.jetbrains</groupId> <artifactId>annotations</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!--jsp依赖--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <!--stl表达式的依赖--> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <!--标准库依赖--> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency></dependencies>