1 整体结构纵览
1.1 配置文件
Java Resources/src/struts.xml
WebContent/WEB-INF/web.xml
1.2 java文件
Java Resources/src/lee/LoginAction.java
1.3 jsp文件
WebContent/error.jsp
WebContent/index.jsp
WebContent/welcome.jsp
1.4 jar文件
WebContent/WEB-INF/lib/
包括
commons-fileupload.jar
commons-logging-api.jar
freemaker.jar
ognl.jar
struts-core.jar
xwork.jar
1.5 国际化资源文件
WebContent/WEB-INF/classes/lee/messageResource_zh_CN.properties
WebContent/WEB-INF/classed/lee/messageResource.properties
注: 国际化步骤
- 编写资源文件
- 在struts.xml文件中声明以便加载资源文件
- 使用bean标签显示国际化信息
2 具体实现
2.1 配置文件实现
web.xml
<?xml version=
"1.0"
encoding=
"gbk"
?>
<web-app 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" version= "2.5" > <filter> <filter-name>struts2 </filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2 </filter-name> <url-pattern>/* </url-pattern> </filter-mapping> </web-app> |
struts.xml
<?xml version=
"1.0"
encoding=
"gbk"
?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > <!-- 指定Struts 2配置文件的根元素 --> <struts> <!-- 指定全局国际化资源文件base名 --> <constant name= "struts.custom.i18n.resources" value= "messageResource" /> <!-- 指定国际化编码所使用的字符集 --> <constant name= "struts.i18n.encoding" value= "GBK" /> <!-- 所有的Action定义都应该放在package下 --> <package name= "lee" extends= "struts-default" > <action name= "login" class= "lee.LoginAction" > <!-- 定义三个逻辑视图和物理资源之间的映射 --> <result name= "input" >/login.jsp </result> <result name= "error" >/error.jsp </result> <result name= "success" >/welcome.jsp </result> </action> </package> </struts> |
2.2 java文件实现
LoginAction.java文件
package lee;
import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ActionContext; /** * Description: * <br/>Copyright (C), 2008-2010, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ //Struts2的Action继承了ActionSupport public class LoginAction extends ActionSupport { //定义封装请求参数的username和password属性 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; } //定义处理用户请求的execute方法 public String execute() throws Exception { //当username为scott,password为tiger即登录成功 if (getUsername().equals( "scott") && getPassword().equals( "tiger") ) { //将登录的用户名放入session范围内 ActionContext.getContext().getSession().put( "user" , getUsername()); return SUCCESS; } else { return ERROR; } } } |
2.3 jsp文件实现
error.jsp
<%@ page language=
"java" contentType=
"text/html; charset=UTF-8"%>
<%@taglib prefix= "s" uri= "/struts-tags"%> <html> <head> <title><s:text name= "errorPage"/></title> </head> <body> <s:text name= "failTip"/> </body> </html> |
index.jsp
<%@ page language=
"java" contentType=
"text/html; charset=UTF-8"%>
<%@taglib prefix= "s" uri= "/struts-tags"%> <html> <head> <title><s:text name= "loginPage"/></title> </head> <body> <s:form action= "login"> <s:textfield name= "username" key= "user"/> <s:textfield name= "password" key= "pass"/> <s:submit key= "login"/> </s:form> </body> </html> |
welcome.jsp
<%@ page language=
"java" contentType=
"text/html; charset=UTF-8"%>
<%@taglib prefix= "s" uri= "/struts-tags"%> <html> <head> <title><s:text name= "succPage"/></title> </head> <body> <s:text name= "succTip"> <s:param>${sessionScope.user}</s:param> </s:text><br> </body> </html> |