发表于: 2018-03-26 22:48:44
1 501
今天完成的事情:
昨天在做表单提交的时候,设置了
contentType="text/html;charset=UTF-8"
pageEncoding="utf-8"
这连个属性但是在提交到浏览器上的时候,还是出现中文乱码的情况。
解决:
在网上查找原因,spring mvc form表单submit直接提交出现乱码一般是服务器端和页面之间编码不一致造成的。
在web.xml中添加过滤器
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
之后可以正常显示中文
今天做的表单用的是springMVC封装的一系列标签
项目结构
两个jsp页面
要使用Spring MVC提供的表单标签,首先需要在视图页面添加:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
student.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>SpringMVC 表单处理</title>
</head>
<body>
<h2>Student Information</h2>
<form:form method="post" action="/addStudent">
<table>
<tr>
<td><form:label path="name">名字:</form:label> </td>
<td><form:input path="name"/> </td>
</tr>
<tr>
<td><form:label path="age">年龄:</form:label></td>
<td><form:input path="age"/> </td>
</tr>
<tr>
<td><form:label path="id">编号:</form:label> </td>
<td><form:input path="id"/> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交表单"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
其中
<form:input path="name"/>
会生成一个type为text的Html input标签,通过path属性来指定要绑定的Model中的值。
<fm:form>标签
action属性:指定表单提交的目标URL,可不指定,则自动提交获取到的表单页面URL。
result.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>springMVC 表单处理</title>
</head>
<body>
<h2>提交的学生信息如下</h2>
<table>
<tr>
<td>名称:</td>
<td>${name}</td>
</tr>
<tr>
<td>年龄:</td>
<td>${age}</td>
</tr>
<tr>
<td>编号:</td>
<td>${id}</td>
</tr>
</table>
</body>
</html>
控制器
@Controller
public class StudentController {
private final Logger logger = LoggerFactory.getLogger(StudentController.class);
@RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView("student", "command", new Student());
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(@ModelAttribute("student")Student student, Model model) {
logger.info("/addStudent POST , the student is"+student);
model.addAttribute("name", student.getName());
model.addAttribute("age", student.getAge());
model.addAttribute("id", student.getId());
return "result";
}
}
页面
明天计划的事情:
继续学习springMVC
遇到的问题:
之前由于action属性没有设置对,导致页面不能正常跳转
必须与request Mapping的value值相匹配
收获:
学习了部分spring MVC封装的标签的用法,自己动手建了个提交表单的项目。
进度:任务二步骤二
任务开始时间:3.23
预计demo时间:3.28
是否延期:否
禅道地址:http://task.ptteng.com/zentao/project-task-562.html
评论