Struts2下载、安装及开发流程



二、Struts2安装

三、开发流程

<?xml version="1.0" encoding="GBK"?><web-app xmlns:xsi=""xmlns="" xmlns:web=""xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"><!– 定义Struts2的核心Filter –><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><!– 让Struts2的核心Filter拦截所有请求 –><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.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为crazyit.org,password为leegang时即登录成功if (getUsername().equals("struts")&& getPassword().equals("struts") ){ActionContext.getContext().getSession().put("user" , getUsername());return SUCCESS;}else{return ERROR;}}}

<struts><!– 指定全局国际化资源文件 –><constant name="struts.custom.i18n.resources" value="mess"/><!– 指定国际化编码所使用的字符集 –><constant name="struts.i18n.encoding" value="GBK"/><!– 所有的Action定义都应该放在package下 –><package name="lee" extends="struts-default"><action name="login" class="org.crazyit.app.action.LoginAction"><!– 定义三个逻辑视图和物理资源之间的映射 –><result name="input">/login.jsp</result><result name="error">/error.jsp</result><result name="success">/welcome.jsp</result></action></package></struts>5.编写视图资源

login.jsp

<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%><%@taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""><html><head><meta http-equiv="Content-Type" content="text/html; charset=GBK"><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>

error.jsp

<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%><%@taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""><html><head><title><s:text name="errorPage"/></title><meta http-equiv="Content-Type" content="text/html; charset=GBK"></head><body><s:text name="failTip"/></body></html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%><%@taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""><html><head><title><s:text name="succPage"/></title><meta http-equiv="Content-Type" content="text/html; charset=GBK"></head><body><s:text name="succTip"><s:param>${sessionScope.user}</s:param></s:text><br/></body></html>

6.编写国际化资源文件

loginPage=\u767b\u5f55\u9875\u9762

errorPage=\u9519\u8bef\u9875\u9762

succPage=\u6210\u529f\u9875\u9762

failTip=\u5bf9\u4e0d\u8d77\uff0c\u60a8\u4e0d\u80fd\u767b\u5f55\uff01

succTip=\u6b22\u8fce\uff0c{0},\u60a8\u5df2\u7ecf\u767b\u5f55\uff01

user=\u7528\u6237\u540d

pass=\u5bc6\u7801

login=\u767b\u5f55

loginPage=登录页面

errorPage=错误页面

succPage=成功页面

failTip=对不起,,您不能登录!

user=用户名

pass=密码

login=登录

四、运行结果

美好的生命应该充满期待惊喜和感激

Struts2下载、安装及开发流程

相关文章:

你感兴趣的文章:

标签云: