标签:produces converter over names 日期类 org 类型 ssi string
SpringMVC框架在接收日期类型的参数时,会有问题,解决办法
1)传过来的是一条时间参数时,只需在UserController类中添加一段代码
@InitBinder public void initBinder(ServletRequestDataBinder binder) { // 只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型 binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); }
2)传过来的是一组集合,例如是表单结构
后台UserController代码如下
@RequestMapping("list.do") public String list(User user) { System.out.println(user); return "list"; }
所以需要在User类中添加注解
package com.zhiyou100.zfx.bean; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; public class User { private String uname; private String password; private String sex; private int age; private String address; private String phone; @DateTimeFormat(pattern="yyyy-MM-dd") //添加此注解,格式可以自己定 private Date date; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "User [uname=" + uname + ", password=" + password + ", sex=" + sex + ", age=" + age + ", address=" + address + ", phone=" + phone + ", date=" + date + "]"; } public User(String uname, String password) { super(); this.uname = uname; this.password = password; } public User() { super(); } public User(String uname) { super(); this.uname = uname; } }
package com.zhiyou100.zfx.controller; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.servlet.ModelAndView; import com.zhiyou100.zfx.bean.User; @Controller @RequestMapping("user") //@SessionAttributes(names= {"name","addresds"}) //通过注解来保存 键名叫:name保存的作用域为session 只对model有用 public class UserController { @RequestMapping("list.do") public String list(User user) { System.out.println(user); return "list"; } @RequestMapping("list1.do") public String list1(Date date) { System.out.println(date); return "list"; } @InitBinder public void initBinder(ServletRequestDataBinder binder) { // 只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型 binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); } // @RequestMapping("request.do") // public ModelAndView request() { // //1.使用ModelAndView保存 // ModelAndView mv = new ModelAndView("list"); // mv.addObject("name","张三"); // return mv; // } // @RequestMapping("request.do") // public String request(Model model) { // //2.保存到Model中 // model.addAttribute("name","李四"); // return "list"; // } // @RequestMapping("request.do") // public String request(Map<String,Object> map) { // //3.保存到Map中 // map.put("name", "王五"); // return "list"; // } // @RequestMapping("request.do") // public String request(HttpServletRequest request) { // //4.保存到原始request中 // request.setAttribute("name", "赵六"); // return "list"; // } @RequestMapping("request.do") public String request(HttpSession session) { //5.保存到session中 session.setAttribute("name", "田七"); return "list"; } }
list.jsp中插入图片
<img src="/Spring4/img/002.jpg"> <!--图片路径写绝对路径-->
1)如果web.xml配置为如下:
<servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
静态资源可以正常的显示。
2)如果web.xml配置为如下:
<servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <!-- 只有后缀为do才经过DispatcherServlet <url-pattern>*.do</url-pattern> --> <!-- /表示任何请求都要经过DispatcherServlet --> <url-pattern>/</url-pattern> </servlet-mapping>
静态资源不能正常显示
需要在springMvc-servlet.xml中添加
简单案例
1.加入jackson的jar包
2.新建一个ajax.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="/Spring4/js/jquery-3.4.1.min.js"></script> </head> <body> <script type="text/javascript"> $.ajax({ url:"Ajax/ajax1", type:"POST", success:function(data){ alert(data) } }); </script> </body> </html>
3.新建AjaxController类
package com.zhiyou100.zfx.controller; 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.zfx.bean.User; @Controller @RequestMapping(value="Ajax") public class AjaxController { @RequestMapping("ajax1") @ResponseBody public String ajax() { return "hello"; } }
4.结果页面
如果AjaxController中return的是中文?
package com.zhiyou100.zfx.controller; 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.zfx.bean.User; @Controller @RequestMapping(value="Ajax") public class AjaxController { @RequestMapping("ajax1") @ResponseBody public String ajax() { return "你好"; } }
结果
出现了乱码
原因
如图所示,此jar包的类中的编码设置问题
此类中的StringHttpMessageConverter中的编码不是utf-8
解决办法
方法一
在RequestMapping注解中添加
@RequestMapping(value="Ajax",produces = "text/html;charset=UTF-8")
方法二
在springMvc-servlet.xml中的加载驱动那里修改
结果
标签:produces converter over names 日期类 org 类型 ssi string
原文地址:https://www.cnblogs.com/zfx666/p/11455808.html