Struts2 学习笔记 —— 10 —— Action接收参数时的中文乱码问题

 

在Struts接收参数时,如果输入中文,那么可能就会出现乱码问题

 

 

首先写一个index.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<% String context = request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>

	使用DomainModel接收参数,测试中文问题<br><br>
	<form action="<%= context%>/user/user!add">
	姓名:<input type="text" name="user.name"></input><br><br>
	密码:<input type="password" name="user.password"></input><br><br>
	<input type="submit" value="submit"> 
	</form>
	
</body>
</html>


 

 

我们发现使用Action传参数的时候使用POST方式也是可以的(之前用的都是GET方式传的参数)

 

结果发现输入中文后Action接收到的中文都是乱码

 

 

接收结果:

 

 

处理的方式应当是在struts.xml中加入

<constant name="struts.i18n.encoding" value="GBK" />


 

这句话会指定action的编码方式,将覆盖默认的struts的设置

 

 

PS:默认的struts的设置在这里

 

 

 

 

但是!!貌似加了这句话以后不管用,不知道是struts的bug还是出于什么考虑

 

解决办法:

1、可以再struts的filter之前配一个filter,更改request的值

2、不再使用struts2.1的配置,使用2.0的配置,见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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	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>Struts2_0600_ActionWildcard</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>
	
	<filter>
		<filter-name>struts2</filter-name>
		<!-- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> -->
		<filter-class>org.apache.struts2.dispatcher.FilterDispacher</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>


 

不知道有没有什么后遗症(我使用的2.3.16版本实验失败)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值