struts2 中英文切換
struts.xml文件:
<struts>
<include file="struts-default.xml" />
<constant name="struts.custom.i18n.resources" value="mess"></constant> //value值爲數據轉換文件mess開頭 的
<package name="com.action" extends="struts-default">
<action name="input" class="com.action.ChangeLocale" >
// <result type="redirect">${href}</result>//返回原來的路徑 實際使用中不能使用重定向
<result >/index.jsp</result>
<interceptor-ref name="i18n" />
</action>
</package>
</struts>
mess.properties 默認數據轉換 比如 hello=你好
mess_en.properties 另外的數據轉換 hello=hello //英文
<body>
<a href="input?request_locale=zh_CN&href=<%=basePath%>">中文 </a><br/> //超鏈接同時傳遞路徑參數
<a href="input?request_locale=en_US&href=<%=basePath%>">英文</a>
<s:text name="hello"></s:text>
</body> 超鏈接傳遞參數切換
ChangeLocale.java
public class ChangeLocale extends ActionSupport {
private String href;
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public String execute(){
HttpServletRequest request = ServletActionContext.getRequest();
this.setHref(request.getParameter("href")); //獲取傳遞過來的參數
return SUCCESS;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.