标签:efault har imp date 编码 文件中 html 接收 jin
一、回顾
1.MVC流程
2.SpringMVC流程
二、接收的参数为日期Date时
1.在相应的controller页追加
@InitBinder public void initBinder(ServletRequestDataBinder binder){ //只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型 binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); }
2.在实体类中添加
@DateTimeFormat(pattern="yyyy-MM-dd") private Date birthday;
三、controller进行数据保存
1.数据保存到request作用域的方式.
2 数据保存到session作用域的方式.
四、静态资源的映射关系
1.静态资源可以正常显示
<servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC-annotation.xml</param-value> </init-param> <!-- 容器在启动时加载springmvc的配置文件。访问时加载配置文件 --> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
2.需要在springmvc配置文件中添加释放静态资源
<servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC-annotation.xml</param-value> </init-param> <!-- 容器在启动时加载springmvc的配置文件。访问时加载配置文件 --> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
<!-- 释放静态资源 -->
<mvc:default-servlet-handler/>
五、SpringMVC完成ajax功能
1.加入jackson的jar包. springmvc。
2.在响应的方法上加上@ResponseBody :把java对象转化为json对象。方法的返回值可以是对象集合字符串。
package com.zhiyou100.klb.annotation; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.zhiyou100.klb.bean.Users; @Controller //等价于<bean class="com.zhiyou100.klb.annotation.MyController" /> @RequestMapping("ajax") public class AjaxController { @RequestMapping("Ajax1") @ResponseBody //1.把hello作为内容响应给客户 public String ajax1(String name) { System.out.println(name); return "你好"; //把这个内容作为内容返回给回调函数 out.print() } @RequestMapping("Ajax2") @ResponseBody //1.把hello作为内容响应给客户 public Users ajax2(String name) { Users user = new Users("张三","110",15,"mmm","nanjin","151"); return user; //把这个内容作为内容返回给回调函数 out.print() } @RequestMapping("Ajax3") @ResponseBody //1.把hello作为内容响应给客户 public List<Users> ajax3(String name) { Users user1 = new Users("张三","110",15,"mmm","nanjin","151"); Users user2 = new Users("李四","110",15,"mmm","nanjin","151"); Users user3 = new Users("王五","110",15,"mmm","nanjin","151"); List<Users> list = new ArrayList<>(); list.add(user1); list.add(user2); list.add(user3); System.out.println(name); return list; //把这个内容作为内容返回给回调函数 out.print() } }
3.如果ajax返回的为字符串,那么就会出现乱码。解决方法主要有两种
1.在controller文件中进行设置
@RequestMapping(value="ajax",produces = "text/html;charset=UTF-8")
2.在springmvc中进行设置
<!-- 开启注解驱动,设置编码 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg index="0" value="utf-8"></constructor-arg> </bean> </mvc:message-converters> </mvc:annotation-driven>
标签:efault har imp date 编码 文件中 html 接收 jin
原文地址:https://www.cnblogs.com/kklb/p/11455855.html