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请求


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZZ2713634772/article/details/75994747
个人分类: struts2
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭