先做个例子:在页面输入数据传递到Controller处理,再返回到页面显示数据.
1.Controller类
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/addUser")
public String addUser(String name, String age, HttpServletRequest request){
request.setAttribute("result", "addUser-----");
System.out.println("name=" + name);
request.setAttribute("result", name);
return "annotation";
}
@RequestMapping("/delUser")
public String delUser(HttpServletRequest request){
request.setAttribute("result", "delUser-----");
return "annotation";
}
@RequestMapping("/toUser")
public String toUser(HttpServletRequest request){
request.setAttribute("result", "toUser-----");
return "adduserView";
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
My JSP 'welcom.jsp' starting page
3.显示数据页面 annotatio.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
My JSP 'welcom.jsp' starting page
spring mvc学习笔记之注解开启
${result }
get方式传递参数造成的乱码可这样修改:
Tomcat下的server.xml
5.Post方式传递数据乱码解决.
@RequestMapping("/addUser")
public String addUser(String name, String age, HttpServletRequest request){
request.setAttribute("result", "addUser-----");
try {
name = new String(name.getBytes("iso-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
}
System.out.println("name=" + name);
request.setAttribute("result", name);
return "annotation";
}
还有一种方式,通过在web.xml添加一个过滤器,对所有传递的数据进行一次编码转换
Character Encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
Character Encoding
/*
又在网上看到更好的方式解决这个问题,如果还在web.xml中加入过滤器,那请求就被拦截了两次,不太好,可以合并成一个拦截.
继承DispactcherServlet,修改编码方式:
package com.skymr.mvc;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.DispatcherServlet;
public class EncodingDispatcherServlet extends DispatcherServlet{
protected String encoding;
@Override
protected void doService(HttpServletRequest request,
HttpServletResponse response) throws Exception {
request.setCharacterEncoding(encoding);
super.doService(request, response);
}
@Override
public void init(ServletConfig config) throws ServletException {
encoding = config.getInitParameter("encoding");
super.init(config);
}
}
springMVC
com.skymr.mvc.EncodingDispatcherServlet
contextConfigLocation
classpath*:/config/spring_annotation-servlet.xml
encoding
UTF-8
1
springMVC
/
6.传递实体数据
实体类
package com.skymr.mvc.entity;
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Controller类修改
@RequestMapping("/addUser")
public String addUser(User user, HttpServletRequest request){
request.setAttribute("result", "addUser-----");
System.out.println("name=" + user.getName());
request.setAttribute("user", user);
return "annotation";
}
显示
spring mvc学习笔记之数据传递
${user.name },${user.age }