- 浏览: 3012212 次
- 性别:
- 来自: 河南
文章分类
- 全部博客 (340)
- Java综合 (26)
- 程序人生 (53)
- RIA-ExtJS专栏 (18)
- RIA-mxGraph专栏 (4)
- RIA-Flex4专栏 (43)
- 框架-Spring专栏 (16)
- 框架-持久化专栏 (22)
- 框架-Struts2专栏 (11)
- 框架-Struts专栏 (12)
- SQL/NOSQL (12)
- 报表/图表 (2)
- 工作流 (5)
- XML专栏 (4)
- 日常报错解决方案 (5)
- Web前端-综合 (12)
- Web/JSP (14)
- Web前端-ajax专栏 (14)
- Web前端-JQuery专栏 (9)
- IDE技巧 (6)
- FILE/IO (14)
- 远程服务调用 (2)
- SSO单点登录 (2)
- 资源分享 (22)
- 云计算 (1)
- 项目管理 (3)
- php专栏 (1)
- Python专栏 (2)
- Linux (1)
- 缓存系统 (1)
- 队列服务器 (1)
- 网络编程 (0)
- Node.js (1)
最新评论
-
hui1989106a:
我的也不能解压,360和好压都试了,都不行
《Spring in Action》完整中文版分享下载 -
temotemo:
这些example有些过时了,官方建议使用HBase-1.0 ...
Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询 -
zy8102:
非常感谢~
HeadFirst系列之七:《深入浅出SQL》原版高清PDF电子书分享下载 -
zy8102:
重命名了一下搞定了
HeadFirst系列之七:《深入浅出SQL》原版高清PDF电子书分享下载 -
zy8102:
为什么下载以后老解压不了呢?
HeadFirst系列之七:《深入浅出SQL》原版高清PDF电子书分享下载
有关Struts1的知识大部分都已经温习完毕,今天开始转向Struts2的温习工作了
首先解压struts-2.1.8.1的ZIP包,复制struts-2.1.8.1\apps\struts2-blank-2.1.8.1.war中lib下的jar包到项目中去,那么这几个包就是必须包
复制struts-2.1.8.1\src\core\src\test\resources\struts.xml到项目的src下,然后进行细致的修改
具体示例【还是经典的登录示例】
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>登录示例</title>
</head>
<body>
<form action="login.action" method="post">
<table>
<tr><td>用户名:</td><td><input type="text" name="loginname"/></td></tr>
<tr><td>密码:</td><td><input type="password" name="pwd"/></td></tr>
<tr><td colspan="2"><input type="submit" value="登录" /></td></tr>
</table>
</form>
</body>
</html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>主页面</title>
</head>
<body>
登录成功,欢迎${param.loginname}
</body>
</html>
LoginAction.java
package com.javacrazyer.action;
public class LoginAction {
private String loginname;
private String pwd;
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String execute() throws Exception {
if (loginname.equals("test") && pwd.equals("test")) {
return "success";
} else {
return "index";
}
}
}
src/struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- package的属性
name: 指定包名。要唯一。方便于其它包的继承。
extends: 指定要继承的包名。一般是struts-default
namespace: 指定命名空间。不指定时,使用默认命名空间,默认命名空间为""
abstract: 指定是否为抽象包。抽象包中不能有Action的配置。
-->
<package name="mystruts" extends="struts-default">
<!-- action标签用来注册一个Action.常用属性
name: action的名字,用于匹配请求URI。必须的。不能出现"/"
class: 指定Action处理类的全限定名。未指定时,默认是ActionSupport。而ActionSupport的execute() 方法默认处理就是返回一个success字符串。
method: 指定要调用的处理方法.不指定时,默认调用execute方法
converter: 指定本Action类要使用到的转换器的全限定名
-->
<action name="login" class="com.javacrazyer.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="index">/index.jsp</result>
</action>
</package>
</struts>
总结,本示例中的Action没有继承任何第三方与Struts2有关的类,但在struts2中只要求其提供一个execute方法,并且返回个字符串的值即可。
对于比较复杂的实际项目来说,可能struts.xml中需要写非常多的类似代码,那么为了方便管理,可以将相关的写在别的xml文件中,然后再引入到struts.xml中来,效果也是一样
比如,在src/META-INF/struts2文件下有这么几个xml:struts2-login.xml、struts2-deliver.xml、struts2-order.xml、
struts2-member.xml、struts2-system.xml等,这几个xml分别对应为
struts2-login.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="logon" namespace="/" extends="default">
<action name="logon_*" method="{1}" class="com.javacrazyer.action.LoginAction">
<result name="input">index.jsp</result>
<result name="success" type="redirect">html/main.jsp</result>
</action>
</package>
</struts>
struts-deliver.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 卡配送增删改 -->
<package name="deliver2" namespace="/deliver2" extends="default">
<action name="*" class="com.javacrazyer.action.DeliverAction" method="{1}">
<result name="deliverUI">/html/deliver2/productDeliver.jsp</result>
<result name="list" type="redirect">/deliverQuery/queryDeliverInfo.action</result>
<result name="queryValidProductInfo" type="json">
<param name="excludeProperties">
productInfo\.cardDeliverDetails, productInfo\.product\.cardOrderDetails
</param>
<param name="includeProperties">
success, tipMsg, productInfo.*, productInfo\.product.*
</param>
</result>
<result name="queryCustInfoByCardID" type="json">
<param name="includeProperties">
order.orderId, order\.cust.*
</param>
</result>
</action>
</package>
<!-- 产品发货 -->
<package name="orderDeliver" namespace="/orderDeliver" extends="default">
<action name="*" class="com.javacrazyer.action.OrderDeliverAction" method="{1}">
<result name="deliverUI">/html/deliver2/orderDeliver.jsp</result>
<result name="list">/deliverQuery/queryDeliverInfo.action</result>
</action>
</package>
<!-- 配送查询 -->
<package name="deliverQuery" namespace="/deliverQuery" extends="default">
<action name="*" class="com.javacrazyer.action.DeliverQueryAction" method="{1}">
<result name="deliverList">/html/deliver2/deliverList.jsp</result>
</action>
</package>
<!-- 物流配送增删改查 -->
<package name="/logistics2" namespace="/logistics2" extends="default">
<action name="*" class="com.javacrazyer.action.DeliverLogisticsAction" method="{1}">
<result name="addResult" type="json">
<param name="includeProperties">
success, tipMsg
</param>
</result>
<result name="logisticsList">/html/deliver2/logistics.jsp</result>
<result name="logisticsToExcel">/html/deliver2/deliverExcel.jsp</result>
</action>
</package>
<!-- 配送回访 -->
<package name="deliverVisit2" namespace="/deliverVisit2" extends="default">
<action name="*" class="com.javacrazyer.action.DeliverVisit2Action" method="{1}">
<result name="UI">/html/deliver2/deliverVisit.jsp</result>
<result name="backList">
/deliverQuery/queryDeliverInfo.action
</result>
<result name="logisticsList">/logistics2/queryLogistics.action</result>
<result name="save" type="json">
<param name="includeProperties">
success, tipMsg
</param>
</result>
</action>
</package>
</struts>
struts2-order.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 订单添加、修改 -->
<package name="orderMgr" namespace="/order" extends="default">
<action name="*" class="com.javacrazyer.action.OrderAction" method="{1}">
<result name="orderUI">/html/order/OrderAdd.jsp</result>
<result name="saveResult" type="json">
<param name="includeProperties">
order\.orderId, order\.orderNo, order.orderStatus, detailId, success, tipMsg
</param>
</result>
<result name="cardOrder">/html/order/orderPrint.jsp</result>
<result name="receiveDetail">/html/order/receiveDetail.jsp</result>
<result name="orderList">/cardOrders/list.action</result>
<result name="check">/html/order/checkDetail.jsp</result>
<result name="orderAdapter">/html/order/orderAdapterCard.jsp</result>
<result name="info">/html/order/showWaterCardInfo.jsp</result>
<result name="activeDetail">/html/order/cardActiveDetail.jsp</result>
<result name="ifAgreeCheck" type="redirect">/cardOrders/list.action</result>
<!-- 导出excel -->
<result name="excel">/html/order/deliverExcel.jsp</result>
<result name="showOrder">/html/order/showOrder.jsp</result>
</action>
</package>
<package name="cardOrders" namespace="/cardOrders" extends="default">
<action name="*" class="com.javacrazyer.action.CardOrdersAction" method="{1}">
<result name="cardOrders_list">/html/order/orderMgn.jsp</result>
<result name="serch">/html/order/orderSerch.jsp</result>
<result name="check">/html/order/orderCheck.jsp</result>
<result name="listByType">/html/order/orderType.jsp</result>
<result name="toCheck" type="redirect">/cardOrders/list.action</result>
<result name="orderCollectList">/html/order/orderCollectList.jsp</result>
<result name="querySelfExamFlag" type="json">
<param name="includeProperties">selfExamFlag</param>
</result>
</action>
</package>
</struts>
struts2-member.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="member" namespace="/member" extends="default">
<global-results>
<result name="success">/html/member/MemberMgn.jsp</result>
</global-results>
<action name="member_*" method="{1}" class="com.javacrazyer.action.MemberAction">
<result name="list">/html/member/showMember.jsp</result>
<result name="del">/html/member/showMember.jsp</result>
<result name="show">/html/member/showSingleData.jsp</result>
<result name="update">/html/member/memberUpdate.jsp</result>
<result name="transaction">/html/member/showTransaction.jsp</result>
</action>
</package>
</struts>
struts2-system.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="system" namespace="/html/system" extends="default">
<!-- 清理业务数据 -->
<action name="clear_*" method="{1}"
class="com.javacrazyer.action.ClearDataAction">
<result name="message">/html/system/cleardata/clearData_do.jsp</result>
</action>
<!-- 配送单位 -->
<action name="system_*" method="{1}"
class="com.javacrazyer.action.DeliverCompanyAction">
<result name="save">/html/system/system_list.action</result>
<result name="list">/html/system/DeliverCompany.jsp</result>
<result name="delete">/html/system/system_list.action</result>
<result name="update">/html/system/DeliverCompanyDetail.jsp</result>
<result name="update2">/html/system/system_list.action</result>
<result name="findByShortName">/html/system/DeliverCompanyShow.jsp</result>
<!-- 根据配送信息,生成excel -->
<result name="excel">/html/order/deliverExcel.jsp</result>
</action>
<!-- 数据字典 -->
<action name="dictTables_*" method="{1}"
class="com.javacrazyer.action.DictTablesAction">
<result name="list">/html/system/DictsList.jsp</result>
<result name="list2">/html/system/DictsDetail.jsp</result>
<result name="add">/html/system/dictTables_list2.action</result>
<result name="delete">/html/system/dictTables_list2.action</result>
<result name="addDict">/html/system/dictTables_list.action</result>
<result name="update">/html/system/DictsList.jsp</result>
<result name="modDictName" type="json">
<param name="includeProperties">success</param>
</result>
</action>
<!-- 产品库 -->
<action name="cardProduct_*" method="{1}"
class="com.javacrazyer.action.CardProductAction">
<result name="list">/html/system/ProductMgn.jsp</result>
<result name="delete">/html/system/cardProduct_list.action</result>
<result name="serch">/html/system/ProductFind.jsp</result>
<result name="chgStatus" type="json">
<param name="includeProperties">productStatus</param>
</result>
<result name="save" type="json">
<param name="includeProperties">
success, tipMsg
</param>
</result>
<result name="detail">/html/system/ProductDetail.jsp</result>
<result name="update">/html/system/cardProduct_list.action</result>
<result name="productsJson" type="json">
<param name="includeProperties">
cardProductss\[\d+\]\.productCode, cardProductss\[\d+\]\.productName,
</param>
</result>
</action>
<!-- 积分规则 -->
<action name="levelQry_*" method="{1}"
class="com.javacrazyer.action.LevelQryAction">
<result name="list">/html/system/LevelQry.jsp</result>
<result name="save">/html/system/levelQry_list.action</result>
<result name="detail">/html/system/LevelQryDetail.jsp</result>
<result name="update">/html/system/levelQry_list.action</result>
</action>
<!-- 费用类别 -->
<action name="cost_*" method="{1}"
class="com.javacrazyer.action.SysCostAction">
<result name="serch">/html/system/costList.jsp</result>
<result name="program">/html/system/programList.jsp</result>
</action>
<!-- 部门管理 -->
<action name="dept_*" method="{1}"
class="com.javacrazyer.action.DeptinfoAction">
<result name="mgr" type="json">
<param name="includeProperties">
deptCode, success, tipMsg
</param>
</result>
<result name="del" type="json">
<param name="includeProperties">
deptCode, success, tipMsg
</param>
</result>
<result name="list">/html/system/dept/changeOrder.jsp</result>
<result name="order" type="json">
<param name="includeProperties">
success, tipMsg
</param>
</result>
<result name="loadDeptInfo" type="json">
<param name="includeProperties">
success, tipMsg, shortName, deptMemo
</param>
</result>
<result name="deptPage">/html/system/dept/deptList.jsp</result>
<result name="back">/html/system/dept/changeOrder.jsp</result>
</action>
<!-- 人员管理 -->
<action name="user_*" method="{1}" class="com.javacrazyer.action.UserAction">
<result name="userList">/html/system/user/userList.jsp</result>
<result name="mgr" type="json">
<param name="includeProperties">
userID, success, tipMsg
</param>
</result>
<result name="del" type="json">
<param name="includeProperties">
userID, success, tipMsg
</param>
</result>
<result name="initPass" type="json">
<param name="includeProperties">
userID, success, tipMsg
</param>
</result>
<result name="userRoleList">/html/system/user/userRoleList.jsp</result>
<result name="saveUserRole" type="json">
<param name="includeProperties">
userID, success, tipMsg
</param>
</result>
</action>
<!-- 用户可管理部门 -->
<action name="userDept_*" method="{1}" class="com.javacrazyer.action.UserDeptAction">
<result name="deptPage">/html/system/user/deptList.jsp</result>
<result name="saveUserDept" type="json">
<param name="includeProperties">
success, tipMsg
</param>
</result>
</action>
<!-- 用户信息修改 -->
<action name="userInfo_*" method="{1}"
class="com.javacrazyer.action.UserinfoAction">
<result name="detail">/html/system/PasswordMgn.jsp</result>
<result name="update">/html/system/PasswordMgn.jsp</result>
<result name="selfInfo">/html/system/SelfInfo.jsp</result>
<result name="modSelfInfo" type="json">
<param name="includeProperties">userName</param>
</result>
</action>
<!-- 角色管理 -->
<action name="role_*" method="{1}" class="com.javacrazyer.action.RoleAction">
<result name="roleList">/html/system/role/roleList.jsp</result>
<result name="ui">/html/system/role/roleMgr.jsp</result>
</action>
</package>
<package name="storeHouse" namespace="/storeHouse" extends="default">
<action name="*" method="{1}" class="com.javacrazyer.action.StoreHouseAction">
<result name="ui">/html/system/storehouse/storeHouseMgr.jsp</result>
<result name="list">/html/system/storehouse/storeHouseList.jsp</result>
</action>
</package>
</struts>
那么针对这么几个xml,在src/struts.xml中就应该像下面这么引用
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.objectFactory.spring.autoWire" value="name" />
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.action.extension" value="action"></constant>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<!-- 引入struts2其他类似配置文件 -->
<include file="META-INF/struts2/struts2-login.xml" />
<include file="META-INF/struts2/struts2-member.xml" />
<include file="META-INF/struts2/struts2-system.xml" />
<include file="META-INF/struts2/struts2-order.xml" />
<include file="META-INF/struts2/struts2-deliver.xml" />
<package name="default" namespace="/" extends="struts-default, json-default">
<!-- 定义拦截器 -->
<interceptors>
<interceptor name="appInterceptor"
class="com.javacrazyer.action.AppInterceptor" />
<interceptor-stack name="appInterceptorStack">
<interceptor-ref name="appInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="appInterceptorStack"/>
<default-action-ref name="index" />
<global-results>
<result name="login">/index.jsp</result>
<result name="postRequest">/postRequest.jsp</result>
<result name="errorPage">/errorPage.jsp</result>
</global-results>
<action name="index">
<result type="redirectAction">
<param name="actionName">Login_Action</param>
<param name="namespace">/</param>
</result>
</action>
</package>
<!-- Add packages here -->
</struts>
发表评论
-
struts2中关于ActionMessage在redirect传递时丢失问题的解决方案
2011-01-24 09:32 4301首先来看一段ACTION代码 @ParentPacka ... -
Struts2温习(10)--文件上传
2010-11-16 20:41 5609自接触这么多种技术的上传来看,还是Struts2的上传最好用, ... -
Struts2温习(9)--国际化的应用
2010-11-16 15:11 15141. Java对国际化的支持: Java内部使用unicode ... -
Struts2温习(8)--表单验证的两种方式
2010-11-16 10:32 48731. Struts2中的输入校验 2. 编码方式校验 1) ... -
Struts温习(7)--自定义类型转换器
2010-11-16 10:08 3236一、概述 在B/S应用中,将字符串请求参数转换 ... -
Struts2温习(6)--拦截器(Inteceptor)的使用
2010-11-15 21:36 2239Interceptor(以下译为拦截器)是Struts ... -
Struts2温习(5)--OGNL的使用
2010-11-15 20:18 4062要谈OGNL在Struts2中的应用,首先得明白OGNL到底是 ... -
Struts2温习(4)--基于注解方式Action配置
2010-11-15 19:37 22832还是已登录来说明下这个Action的配置,这里要说的Actio ... -
Struts2温习(3)--ActionSuppot的使用
2010-11-15 17:29 2318之前在第一个示例中,使用到的Acrtion是没有继承任何方法的 ... -
Struts2温习(2)--工作原理图解
2010-11-15 10:11 1967就上篇文章的第一个完整的登录示例,我们本节来讲解下Struts ...
相关推荐
赠送jar包:struts2-json-plugin-2.3.24.jar; 赠送原API文档:struts2-json-plugin-2.3.24-javadoc.jar; 赠送源代码:struts2-json-plugin-2.3.24-sources.jar; 赠送Maven依赖信息文件:struts2-json-plugin-...
struts2-core-2.0.1.jar, struts2-core-2.0.11.1.jar, struts2-core-2.0.11.2.jar, struts2-core-2.0.11.jar, struts2-core-2.0.12.jar, struts2-core-2.0.14.jar, struts2-core-2.0.5.jar, struts2-core-2.0.6.jar,...
struts2-spring-plugin-2.3.15.2.jar ; struts2-json-plugin-2.3.16.3.jarstruts2-spring-plugin-2.3.15.2.jar ; struts2-json-plugin-2.3.16.3.jar
struts2-ssl-plugin-1.2.1.jar
struts2-struts1-plugin-2.1.6.jar
`struts2-json-plugin-2.1.8.1.jar` 则是Struts 2框架的一个插件,主要用于增强Struts 2对JSON的支持。Struts 2是一款非常流行的MVC(Model-View-Controller)框架,用于构建企业级的Java Web应用程序。这个插件允许...
struts2-json-plugin,Struts JSON插件
-- 为修复struts2 s2-016、s2-017漏洞,重写DefaultActionMapper --> <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="myDefaultActionMapper" class=...
struts2-convention-plugin-2.3.15.1.jar
Struts2-Spring-Plugin-2.2.1.jar 是一个专门为 Struts2 框架与 Spring 框架集成而设计的插件。这个插件的主要目的是简化在基于Struts2的应用程序中整合Spring的功能,如依赖注入(DI)、AOP(面向切面编程)以及...
struts2-core-2.2.3.1-sources.jar struts2-core-2.2.3.1-sources.jar struts2-core-2.2.3.1-sources.jar
struts2-convention-plugin-2.3.24.1
Struts2 JSON Plugin是针对Apache Struts2框架的一个重要组件,版本为2.3.8。这个插件主要的功能是让Struts2应用程序能够轻松地处理JSON(JavaScript Object Notation)数据格式,使得Web应用可以方便地进行JSON序列...
struts2-plexus-plugin-2.1.6.jar
struts2-core-2.5.18.jar包下载,支持struts2的类库下载
struts2-jquery-plugin-3.1.0.jar
struts2-core-2.5.10.jar ,struts核心包,struts2-core-2.5.10.jar
struts2-jfreechart-plugin-2.1.8.1.jar
struts2-config-browser-plugin-2.3.24.jar, struts2-core-2.3.24.jar, struts2-jasperreports-plugin-2.3.24.jar, struts2-jfreechart-...struts2-struts1-plugin-2.3.24.jar, struts2-tiles-plugin-2.3.24.jar,
默认的struts2-config-browser-plugin包中的ftl文件include标签路径用的相对路径,会找到包内的include文件,将包内ftl里include的路径改成的/开头的全路径。