核心代码:
`package controller;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserinfoController {
@RequestMapping(value = "login")
public String loginMethod(String username)
throws UnsupportedEncodingException {
System.out.println("【loginMethod username=】方法》》》》》》" + username);
username = java.net.URLEncoder.encode(username, "utf-8");//%E5%8F%82%E6%95%B0
System.out.println(username);
username = username.replace("%", "_");//_E5_8F_82_E6_95_B0
System.out.println(username);
return "redirect:/listString.spring?canshu=" + username;
}
@RequestMapping(value = "listString")
public String listStringMethod(String canshu, Model model)
throws UnsupportedEncodingException {
canshu = canshu.replace("_", "%");
canshu = java.net.URLDecoder.decode(canshu, "utf-8");
System.out.println("【listStringMethod canshu=】方法》》》》》》" + canshu);
List list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add(canshu);
model.addAttribute("list", list);
return "listString.jsp";
}
}
`
过滤器CharSetFilter.java文件
`package extfilter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharSetFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
arg0.setCharacterEncoding("utf-8");
arg1.setCharacterEncoding("utf-8");
arg2.doFilter(arg0, arg1);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
`
index.jsp
`<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
post:
<br />
<form action="login.spring" method="post">
username:<input type="text" name="username" value="参数"><br />
password:<input type="text" name="password"><br />
<input type="submit" value="submit"><br />
</form>
<br /> get:
<br />
<form action="login.spring" method="get">
username:<input type="text" name="username"><br />
password:<input type="text" name="password"><br />
<input type="submit" value="submit"><br />
</form>
</body>
</html>`
listString.jsp
`<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<c:forEach var="eachString" items="${list}">
${eachString}<br />
</c:forEach>
</body>
</html>`
web.xml文件加以下代码
`<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springlogin</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.spring</url-pattern>
</servlet-mapping>
<!--过滤器 -->
<filter>
<filter-name>charSetFilter</filter-name>
<filter-class>extfilter.CharSetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>charSetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>`
- 邮箱:ithelei@sina.cn
- 技术讨论群:687856230
- GoodLuck