struts国际化实例---登录例子(中英文切换)

struts国际化实例---登录例子(中英文切换)

1、新建一个web工程struts-i18n2,添加struts2支持
2、在index.jsp页面中写一个form表单:(超链接转换中英文时,需要在action之间进行切换)
  <body>
  	<center>
	  	<s:form action="login" method="post">
	  		<s:text name="user.login"></s:text>
	  		<s:textfield name="user.username" key="username"></s:textfield>
	  		<s:textfield name="user.password" key="password"></s:textfield>
	  		<s:submit key="login" align="center"></s:submit>
	  	</s:form>
	  	<a href="switch.action?request_locale=zh_CN">中文</a>
	  	<a href="switch.action?request_locale=en">English</a>
  	</center>
  </body>
3、配置struts.xml文件(需要配置i18n国际化的参数(struts.custom.i18n.resources)的值)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<constant name="struts.custom.i18n.resources" value="message"></constant>
	<package name="login" extends="struts-default" namespace="/">
		<action name="login" class="com.etc.action.LoginAction" method="login">
			<result>/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
		<action name="switch" class="com.etc.action.SwitchAction">
			<result>/index.jsp</result>
		</action>
	</package>
</struts>    
4、在src下新建com.etc.vo,新建User.java类:
package com.etc.vo;
public class User {
	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;
	}	
}
5、在src下新建com.etc.action,新建LoginAction.java类:
package com.etc.action;
import com.etc.vo.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
	private static final long serialVersionUID = 5282197184219501014L;
	private User user;
	public String login(){
		if(user.getUsername().equals("zoey")){
			System.out.println(getText("login.success"));
			return Action.SUCCESS;
		}
		System.out.println(getText("login.error"));
		return Action.ERROR;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
}
6、在com.etc.action下新建SwitchAction.java类:(什么都不做,只是返回success,跳转到index.jsp页面)
package com.etc.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class SwitchAction extends ActionSupport {
	private static final long serialVersionUID = 3791113563988999604L;
	@Override
	public String execute() throws Exception {
		return Action.SUCCESS;
	}
}
7、在src下新建message_zh_CN.properties和message_en.properties文件



8、编写success.jsp页面和error.jsp页面
  <body>
	<s:text name="login.success" />
  </body>
  <body>
	<s:text name="login.error" />
  </body>
9、运行:http://localhost:8080/struts-i18n2/index.jsp

点击English超链接进入如下页面:

登录成功(英文登录)或者登录失败(中文登录)之后进入:


注意:web.xml中默认配置的url-parttern是*.action,也就是捕捉所有的action的请求,如下:
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
但是,此处在index.jsp中就已经使用了struts标签,所以运行起来会报错如下:
	The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location] 
此时,需要将web.xml中中配置加上*.jsp,使得可以捕捉jsp页面的struts请求
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>
也可以直接配置/*,使得可以捕捉所有的struts请求
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
   <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

在web.xml中有三种配置:
(1) /* 捕捉所有请求
(2) / 捕捉所有除 jsp页面之外的请求
(3) *.action 捕捉所有的action请求


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值