1. 程式人生 > >Struts2 中文字元編碼問題

Struts2 中文字元編碼問題

index.jsp

<% 
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<base href="<%=basePath %>"/>

<body>
使用action屬性接收引數,測試中文問題
<form action="user/user!add" method="post">
姓名:<input type="text" name="name"></input>
<input type="submit" value="submit"/>
</form>

	
</body>
</html>

user_add_success.jsp
<body>
	User Add Success!
</body>

struts.xml
<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.i18n.encoding" value="GBK" /> <!-- internationalization -->
    <package name="user" extends="struts-default" namespace="/user">
        <action name="userAdd" class="com.bjsxt.struts2.user.action.UserAction" method="add">
            <result>/user_add_success.jsp</result>
        </action>
        
        <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
            <result>/user_add_success.jsp</result>
        </action>
    </package>
</struts>

UserAction.java

package com.bjsxt.struts2.user.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	private String name;
	public String add() {
		System.out.println("name=" + name);
		return SUCCESS;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
}