导依赖html
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> </dependencies>
写springMvc配置文件前端
<?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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 开启注解扫描 --> <context:component-scan base-package="cn.ann"/> <!-- 配置视图解析器 --> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value=""/> <property name="suffix" value=".html"/> </bean> <!-- 开启springMvc注解支持 --> <mvc:annotation-driven/> <!-- 静态资源放行 --> <mvc:default-servlet-handler/> </beans>
web.xmljava
<?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"> <servlet> <!-- 配置springMvc --> <servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
编写Controller类git
@Controller public class HelloController { @RequestMapping("/hello") public String hello() { System.out.println("hello springMvc"); return "success"; } }
<mvc:annotation-driven/>
的做用: 配置这个标签就至关于配置了处理器映射器(HandleMapping), 处理器适配器(HandleAdapter) 和 异常解析器(ExceptionResolver)乱码问题: 添加过滤器便可程序员
<filter> <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
编写自定义的转换器github
public class StringToDateConverter implements Converter<String, Date> { @Override public Date convert(String s) { try { if (Pattern.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$", s)) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.parse(s); } else if (Pattern.matches("^\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}$", s)) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.parse(s); } else if (Pattern.matches("^\\d{4}-\\d{1,2}-\\d{1,2}[T]\\d{1,2}:\\d{1,2}:\\d{1,2}$", s)) { s = s.replace('T', ' '); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.parse(s); } else { throw new RuntimeException("输入格式不正确"); } } catch (ParseException e) { e.printStackTrace(); throw new RuntimeException(e); } } }
注册到spring中web
<!-- 注册自定义转换器 --> <bean id="conversionServiceFactory" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="cn.ann.utils.StringToDateConverter"/> </set> </property> </bean> <!-- 开启springMvc注解支持 --> <mvc:annotation-driven conversion-service="conversionServiceFactory"/>
rest风格:ajax
什么是rest: REST(英文:Representational State Transfer,简称REST)描述了一个架构样式的网络系统,好比 web 应用程序。它首次出如今 2000 年 Roy Fielding 的博士论文中,他是 HTTP 规范的主要编写者之一。在目前主流的三种Web服务交互方案中,REST相比于SOAP(Simple Object Access protocol,简单对象访问协议)以及XML-RPC更加简单明了,不管是对URL的处理仍是对Payload的编码,REST都倾向于用更加简单轻量的方法设计和实现。值得注意的是REST并无一个明确的标准,而更像是一种设计的风格。 它自己并无什么实用性,其核心价值在于如何设计出符合REST风格的网络接口。 restful的优势 它结构清晰、符合标准、易于理解、扩展方便,因此正获得愈来愈多网站的采用。 restful的特性: 资源(Resources):网络上的一个实体,或者说是网络上的一个具体信息。 它能够是一段文本、一张图片、一首歌曲、一种服务,总之就是一个具体的存在。能够用一个URI(统一资源定位符)指向它,每种资源对应一个特定的 URI 。要 获取这个资源,访问它的URI就能够,所以 URI 即为每个资源的独一无二的识别符。 表现层(Representation):把资源具体呈现出来的形式,叫作它的表现层 (Representation)。 好比,文本能够用 txt 格式表现,也能够用 HTML 格式、XML 格式、JSON 格式表现,甚至能够采用二进制格式。 状态转化(State Transfer):每 发出一个请求,就表明了客户端和服务器的一次交互过程。 HTTP协议,是一个无状态协议,即全部的状态都保存在服务器端。所以,若是客户端想要操做服务器,必须经过某种手段,让服务器端发生“状态转化”(State Transfer)。而这种转化是创建在表现层之上的,因此就是 “表现层状态转化”。具体说,就是 HTTP 协议里面,四个表示操做方式的动词:GET、POST、PUT、DELETE。它们分别对应四种基本操做:GET 用来获取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源。
<input type="hidden" name="_method" value="GET|POST|PUT|DELETE"/>
示例代码spring
@RequestMapping("/testModelAttribute") public String testModelAttribute(@ModelAttribute("user01") User user) { System.out.println("testModelAttribute run..."); System.out.println(user); return "success"; } /** * 多用于填充对象的空数据, 能够在使用对象以前将对象的全部信息从数据库中查出来 */ @ModelAttribute public void model(User user, Map<String, User> map) { System.out.println("model run..."+user); // 能够在这里面写从数据库查询的操做 user.setBirthday(new Date()); map.put("user01", user); }
@SessionAttributes(value = "user", types = User.class)
@RequestMapping("/testSessionAttributesPut") public String testSessionAttributesPut(Model model) { User user = new User(); user.setName("sessionZS"); user.setAge(23); user.setBirthday(new Date()); model.addAttribute(user); return "success"; } @RequestMapping("/testSessionAttributesGet") public String testSessionAttributesGet(ModelMap model) { System.out.println(model.get("user")); return "success"; } @RequestMapping("/testSessionAttributesClean") public String testSessionAttributesClean(SessionStatus status) { status.setComplete(); return "success"; }
本文代码: 此处的 springMvc01数据库