创建项目大家都懂,如有不明白的地方请参照前几篇博客
首先,我们在src下编写一个messages_zh_CN.properties和messages_en_US.properties
message_zh_CN.properties(中文)
message_en_US.properties(英文)
代码如下:
英文:
login.username=username
login.password=password
login=login
login.success=login.success
user.login=user.login
login.error=login.error
中文:
login.username=\u7528\u6237\u540D
login.password=\u5BC6\u7801
login=\u767B\u5F55
login.success=\u767B\u9646\u6210\u529F
user.login=\u7528\u6237\u767B\u5F55
login.error=\u767B\u5F55\u5931\u8D25
提示:等号后面的可以直接打汉字,会自动转换
接下来,在src下建一个包,新建3个class
取名为LoginAction.java,SwitchAction.java,User.java
代码如下:
LoginAction.java
package com.hngy;
import com.hngy.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;
}
}
SwitchAction.java
package com.hngy;
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;
}
}
User.java
package com.hngy;
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;
}
}
然后在WebRoot里面的WEB-INF中找到web.xml配置文件
代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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-app>
接下来在项目的src里配置struts.xml
代码如下:
<?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.hngy.LoginAction" method="login">
<result name="error">/error.jsp</result>
</action>
<action name="switchAction" class="com.hngy.SwitchAction">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
最后,在WebRoot里面修改index.jsp并新建success.jsp(登陆成功)和error.jsp(登录失败)
index.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登录页面</title>
</head>
<body>
<center>
<s:form action="loginAction" 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>
</html>
success.jsp和error.jsp把body里面的内容写成
<s:text name="login.success" />和<s:text name="login.error" />
此博客是看别人的代码并结合自己的思路重新创作出的,如有侵权请及时联系,谢谢!!