Struts2下载、安装及开发流程
来源:互联网 发布:淘宝客不计入销量 编辑:程序博客网 时间:2024/06/05 18:37
一、Struts2下载
登录http://struts.apache.org/download.cgi 下载Struts2最新版
建议下载Full Distribution完整版,该选项包括Struts2的示例应用(apps文件夹)、空示例应用、核心库(lib文件夹)、源代码和文档(docs文件夹)等,src文件夹包含Struts2框架的全部源代码。
二、Struts2安装
将这几个必须类库复制到Web应用的WEB-INF/lib路径下。如果需要在Web应用中使用Struts2的更多特性,则需要将相应的JAR文件复制到Web应用的WEB-INF/lib路径下。
三、开发流程
1.在web.xml文件中定义核心Filter来拦截用户请求。
<?xml version="1.0" encoding="GBK"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 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>
2.如果需要以POST方式提交请求,则定义包含表单数据的JSP页面。如果仅仅只是以GET方式发送请求,则无须经过这一步。
3.定义处理用户请求的Action类。比如我们做一个登录demo需要一个LoginAction
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;}}}4.配置Action以及处理结果与物理视图资源之间的对应关系。
<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" "http://www.w3.org/TR/html4/loose.dtd"><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" "http://www.w3.org/TR/html4/loose.dtd"><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" "http://www.w3.org/TR/html4/loose.dtd"><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.编写国际化资源文件
使用native2ascii转换编码
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=对不起,您不能登录!
succTip=欢迎,{0},您已经登录!
user=用户名
pass=密 码
login=登录
四、运行结果
- Struts2下载、安装及开发流程
- PHP开发环境&MySQL下载安装及配置流程
- 二、Struts2开发流程
- Struts2 开发流程
- Struts2 开发流程
- Struts2框架开发流程
- struts2 一般开发流程
- struts2项目开发流程
- struts2开发一般流程
- struts2简单开发流程
- struts2开发流程
- struts2的开发流程
- struts2开发流程
- Struts2开发流程分析
- Struts2简单开发流程
- 【Struts2】开发流程
- Struts2框架开发介绍及重要jar文件下载
- VISUALSVN SERVER下载安装流程 及ca证书制作教程
- const的用法,特别是用在函数后面
- lib 合并 打包 遇到 .o 重名的解决办法
- zoj_2876 Phone List
- 轻松搞定面试中的链表的题
- Android布局中使用weight的技巧(点名小软件的实现)
- Struts2下载、安装及开发流程
- Java UDP Socket
- android 中px dp sp 及资源目录drawable-hdpi drawable-ldpi drawable-mdpi等的分析
- nodejs之处理POST请求
- 大雨后的蓝天
- 最常用的 Eclipse 快捷键总结
- hihocoder#1082之单词替换
- ACM学习感悟——weekly training
- Github 教程