初学Struts2,就编写了一个简单的登录的例子,在运行中遇到当用户名为中文时出现中文乱码,故上网搜索解决之道,并撰此文以记之。
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="test1" method="post">
用户名:<input type="text" name="username"/><br/>
密 码:<input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
success.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
登录成功!欢迎:${username}
</body>
</html>
error.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'error.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
登录失败!
<a href="index.jsp">返回重新登录</a>
</body>
</html>
LoginAction.java:
package com.cz.action;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute(){
System.out.println("到达execute。。。。");
return null;
}
public String login(){
System.out.println("到达login。。。。用户名:"+username);
if("啊".equals(username) && "123".equals(password)){
return "success";
}
return "login";
}
}
过滤器FilterEncoding.java
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 FilterEncoding implements Filter {
protected String encoding; // 接收字符编码
protected FilterConfig filterConfig; // 初始化配置
public void init(FilterConfig filterConfig) throws ServletException {
// 从web.xml文件中读取encoding的值
encoding = filterConfig.getInitParameter("encoding");
}
// doFilter方法
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
public void destroy() {
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter>
<filter-name>encoding</filter-name>
<filter-class>FilterEncoding </filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="test1" class="com.cz.action.LoginAction" method="login">
<result name="success">/success.jsp</result>
<result name="login">/error.jsp</result>
</action>
</package>
</struts>
分享到:
相关推荐
关于StrutsPrepareAndExecuteFilter最新的过滤器,在Struts.xml定义常量<constant name="struts.i18n.encoding" value="UTF-8"/>...关于Struts2各个版本、各种过滤器,用get方式提交都方法都无法解决的中文乱码问题。
乱码解决方案和过滤器的解决与用户的登陆error解决 乱码解决方案和过滤器的解决与用户的登陆error解决
struts2技巧,笔记.zip 指定404,500页面 utf-8过滤器 防SQL注入 解决乱码
中文乱码问题很头疼,但一个好的过滤器就可以解决问题啦
页面开发时经常会出现中文乱码问题,需要我们对编码进行转换。手工转换很麻烦,写个过滤器就好多了。
NULL 博文链接:https://alexfc.iteye.com/blog/326497
这是一个不错的例子,使用过滤器解决了字符乱码的问题
一个集成了分页和过滤器的struts小项目(学生成绩管理) ,方便的解决了分页和中文乱码的问题!!! 适合初学者!
STRUTS ActionForm乱码,servlet全局过滤器转义编码
character-encoding-filter 是 Java Web 乱码过滤器。 web.xml <filter-name>character-encoding-filter <filter-class>com.github.zhanhb.filter.CharacterEncodingFilter</filter-class> ...
struts+eclipse+tomcate实现上传使用了过滤器解决了乱码问题。
JSP struts2 url传参中文乱码解决办法 1.设置struts.xml <constant name=struts.i18n.encoding value=UTF-8> 或是设置struts.properties(我自己没试过) struts.i18n.encoding=UTF-8 2.在web.xml添加编码过滤器 ...
按照文件可以快速处理struts乱码问题,不须要写过滤器,非常好用
struts文件的上传下载功能实现,并利用过滤器解决了相应的中文乱码问题
能够快速处理struts乱码问题,很使用,不要写过滤器和转码类
★ EncodingFilter 处理中文乱码的编码过滤器 ★ Action、Form、struts-config.xml web.xml struts的处理核心类及配置文件 ★ JSP调用页面(全面支持struts1.2)(增、删、改、查),分页功能自动实现 (如果数据库...
ssh框架整合实例 采用在web.xml中加载spring配置文件的方法降低struts和spring的耦合度,使用自定义的MyDelegatingRequestProcessor类来代替spring的...该实例还通过过滤器解决了中文乱码的问题。
Struts过滤器可以解决页面之间的乱码问题 一个类继承struts的RequestProcessor(分request 和response处理) 然后在struts-config.xml配置即可!
(2)项目是否为utf-8格式(同时看看的源代码文件中,中文是否乱码)。 (3)JSP页面是否是utf-8 格式。 (4)在web.xml 是否配置了编码过滤器。 (5)数据源配置的url(?useUnicode=true&characterEncoding=UTF-8),...
jsp开发常见的问题,比如乱码问题、数据库连接池问题、servlet过滤器问题等等。