(一)struts2之基础

有关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


Html代码   收藏代码
  1. <span><span style="font-size: medium;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <html>  
  3.   <head>  
  4.     <title>登录示例</title>  
  5.   </head>  
  6.     
  7.   <body>  
  8.    <form action="login.action" method="post">  
  9.    <table>  
  10.    <tr><td>用户名:</td><td><input type="text" name="loginname"/></td></tr>  
  11.     <tr><td>密码:</td><td><input type="password" name="pwd"/></td></tr>  
  12.      <tr><td colspan="2"><input type="submit" value="登录" /></td></tr>  
  13.    </table>  
  14.    </form>  
  15.   </body>  
  16. </html></span></span>  

 success.jsp


Html代码   收藏代码
  1. <span><span style="font-size: medium;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <html>  
  3.   <head>  
  4.     <title>主页面</title>  
  5.   </head>  
  6.   <body>  
  7.    登录成功,欢迎${param.loginname}  
  8.   </body>  
  9. </html></span></span>  

LoginAction.java


Java代码   收藏代码
  1. <span><span style="font-size: medium;">package com.javacrazyer.action;  
  2.   
  3.   
  4. public class LoginAction {  
  5.     private String loginname;  
  6.     private String pwd;  
  7.   
  8.     public String getLoginname() {  
  9.         return loginname;  
  10.     }  
  11.   
  12.     public void setLoginname(String loginname) {  
  13.         this.loginname = loginname;  
  14.     }  
  15.   
  16.     public String getPwd() {  
  17.         return pwd;  
  18.     }  
  19.   
  20.     public void setPwd(String pwd) {  
  21.         this.pwd = pwd;  
  22.     }  
  23.   
  24.     public String execute() throws Exception {  
  25.         if (loginname.equals("test") && pwd.equals("test")) {  
  26.             return "success";  
  27.         } else {  
  28.             return "index";  
  29.         }  
  30.     }  
  31.   
  32. }</span></span>  


 


 src/struts.xml


Xml代码   收藏代码
  1. <span><span style="font-size: medium;"><!DOCTYPE struts PUBLIC  
  2.           "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.           "http://struts.apache.org/dtds/struts-2.0.dtd">  
  4. <struts>  
  5.   <!-- package的属性  
  6.        name:  指定包名。要唯一。方便于其它包的继承。  
  7.        extends: 指定要继承的包名。一般是struts-default  
  8.        namespace: 指定命名空间。不指定时,使用默认命名空间,默认命名空间为""  
  9.        abstract: 指定是否为抽象包。抽象包中不能有Action的配置。  
  10.      -->  
  11.     <package name="mystruts"  extends="struts-default">  
  12.       
  13.         <!-- action标签用来注册一个Action.常用属性   
  14.           name: action的名字,用于匹配请求URI。必须的。不能出现"/"  
  15.           class: 指定Action处理类的全限定名。未指定时,默认是ActionSupport。而ActionSupport的execute() 方法默认处理就是返回一个success字符串。  
  16.           method: 指定要调用的处理方法.不指定时,默认调用execute方法  
  17.           converter: 指定本Action类要使用到的转换器的全限定名  
  18.         -->  
  19.         <action name="login" class="com.javacrazyer.action.LoginAction">  
  20.             <result name="success">/success.jsp</result>  
  21.             <result name="index">/index.jsp</result>  
  22.         </action>  
  23.     </package>  
  24.      
  25. </struts></span></span>  

 

总结,本示例中的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代码   收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.         "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.     <package name="logon" namespace="/" extends="default">  
  7.   
  8.         <action name="logon_*" method="{1}" class="com.javacrazyer.action.LoginAction">  
  9.             <result name="input">index.jsp</result>  
  10.             <result name="success" type="redirect">html/main.jsp</result>  
  11.         </action>  
  12.           
  13.     </package>  
  14.       
  15. </struts>  
  16. </span>  

 

struts-deliver.xml

Xml代码   收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.         "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.     <!-- 卡配送增删改 -->  
  7.     <package name="deliver2" namespace="/deliver2" extends="default">   
  8.         <action name="*" class="com.javacrazyer.action.DeliverAction" method="{1}">  
  9.             <result name="deliverUI">/html/deliver2/productDeliver.jsp</result>  
  10.             <result name="list" type="redirect">/deliverQuery/queryDeliverInfo.action</result>  
  11.             <result name="queryValidProductInfo" type="json">  
  12.                 <param name="excludeProperties">  
  13.                     productInfo\.cardDeliverDetails, productInfo\.product\.cardOrderDetails  
  14.                 </param>  
  15.                 <param name="includeProperties">  
  16.                     success, tipMsg, productInfo.*, productInfo\.product.*  
  17.                 </param>  
  18.             </result>  
  19.             <result name="queryCustInfoByCardID" type="json">  
  20.                 <param name="includeProperties">  
  21.                     order.orderId, order\.cust.*  
  22.                 </param>  
  23.             </result>  
  24.         </action>  
  25.     </package>  
  26.       
  27.     <!-- 产品发货 -->  
  28.     <package name="orderDeliver" namespace="/orderDeliver" extends="default">   
  29.         <action name="*" class="com.javacrazyer.action.OrderDeliverAction" method="{1}">  
  30.             <result name="deliverUI">/html/deliver2/orderDeliver.jsp</result>  
  31.             <result name="list">/deliverQuery/queryDeliverInfo.action</result>  
  32.         </action>  
  33.     </package>  
  34.       
  35.     <!-- 配送查询 -->  
  36.     <package name="deliverQuery" namespace="/deliverQuery" extends="default">   
  37.         <action name="*" class="com.javacrazyer.action.DeliverQueryAction" method="{1}">  
  38.             <result name="deliverList">/html/deliver2/deliverList.jsp</result>  
  39.         </action>  
  40.      </package>     
  41.       
  42.     <!-- 物流配送增删改查 -->  
  43.      <package name="/logistics2" namespace="/logistics2" extends="default">     
  44.         <action name="*" class="com.javacrazyer.action.DeliverLogisticsAction" method="{1}">  
  45.             <result name="addResult" type="json">  
  46.                 <param name="includeProperties">  
  47.                     success, tipMsg  
  48.                 </param>  
  49.             </result>  
  50.             <result name="logisticsList">/html/deliver2/logistics.jsp</result>  
  51.             <result name="logisticsToExcel">/html/deliver2/deliverExcel.jsp</result>  
  52.         </action>  
  53.      </package>  
  54.   
  55.     <!-- 配送回访 -->  
  56.      <package name="deliverVisit2" namespace="/deliverVisit2" extends="default">    
  57.         <action name="*" class="com.javacrazyer.action.DeliverVisit2Action" method="{1}">  
  58.             <result name="UI">/html/deliver2/deliverVisit.jsp</result>  
  59.             <result name="backList">  
  60.                 /deliverQuery/queryDeliverInfo.action  
  61.             </result>  
  62.             <result name="logisticsList">/logistics2/queryLogistics.action</result>  
  63.             <result name="save" type="json">  
  64.                 <param name="includeProperties">  
  65.                     success, tipMsg  
  66.                 </param>  
  67.             </result>  
  68.         </action>  
  69.      </package>  
  70. </struts></span>  

 struts2-order.xml

Xml代码   收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.         "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.   
  7.     <!-- 订单添加、修改 -->  
  8.     <package name="orderMgr" namespace="/order" extends="default">      
  9.         <action name="*" class="com.javacrazyer.action.OrderAction" method="{1}">  
  10.             <result name="orderUI">/html/order/OrderAdd.jsp</result>  
  11.             <result name="saveResult" type="json">  
  12.                 <param name="includeProperties">  
  13.                     order\.orderId, order\.orderNo, order.orderStatus, detailId, success, tipMsg  
  14.                 </param>  
  15.             </result>  
  16.             <result name="cardOrder">/html/order/orderPrint.jsp</result>  
  17.             <result name="receiveDetail">/html/order/receiveDetail.jsp</result>  
  18.             <result name="orderList">/cardOrders/list.action</result>  
  19.             <result name="check">/html/order/checkDetail.jsp</result>  
  20.             <result name="orderAdapter">/html/order/orderAdapterCard.jsp</result>  
  21.             <result name="info">/html/order/showWaterCardInfo.jsp</result>  
  22.             <result name="activeDetail">/html/order/cardActiveDetail.jsp</result>  
  23.             <result name="ifAgreeCheck" type="redirect">/cardOrders/list.action</result>  
  24.             <!-- 导出excel -->  
  25.             <result name="excel">/html/order/deliverExcel.jsp</result>  
  26.             <result name="showOrder">/html/order/showOrder.jsp</result>  
  27.               
  28.         </action>  
  29.     </package>  
  30.       
  31.     <package name="cardOrders" namespace="/cardOrders" extends="default">   
  32.         <action name="*" class="com.javacrazyer.action.CardOrdersAction" method="{1}">  
  33.             <result name="cardOrders_list">/html/order/orderMgn.jsp</result>  
  34.             <result name="serch">/html/order/orderSerch.jsp</result>  
  35.             <result name="check">/html/order/orderCheck.jsp</result>  
  36.             <result name="listByType">/html/order/orderType.jsp</result>  
  37.             <result name="toCheck" type="redirect">/cardOrders/list.action</result>  
  38.             <result name="orderCollectList">/html/order/orderCollectList.jsp</result>  
  39.             <result name="querySelfExamFlag" type="json">  
  40.                 <param name="includeProperties">selfExamFlag</param>  
  41.             </result>  
  42.         </action>  
  43.     </package>  
  44. </struts>  
  45. </span>  

 

struts2-member.xml

Xml代码   收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.         "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.   
  7.     <package name="member" namespace="/member" extends="default">  
  8.         <global-results>  
  9.             <result name="success">/html/member/MemberMgn.jsp</result>  
  10.         </global-results>  
  11.   
  12.         <action name="member_*" method="{1}" class="com.javacrazyer.action.MemberAction">  
  13.             <result name="list">/html/member/showMember.jsp</result>  
  14.             <result name="del">/html/member/showMember.jsp</result>  
  15.             <result name="show">/html/member/showSingleData.jsp</result>  
  16.             <result name="update">/html/member/memberUpdate.jsp</result>  
  17.             <result name="transaction">/html/member/showTransaction.jsp</result>  
  18.         </action>  
  19.     </package>  
  20. </struts>  
  21. </span>  

 

struts2-system.xml

Xml代码   收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.         "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.   
  7.     <package name="system" namespace="/html/system" extends="default">  
  8.       
  9.         <!-- 清理业务数据 -->  
  10.         <action name="clear_*" method="{1}"  
  11.             class="com.javacrazyer.action.ClearDataAction">  
  12.             <result name="message">/html/system/cleardata/clearData_do.jsp</result>  
  13.         </action>  
  14.           
  15.         <!-- 配送单位 -->  
  16.         <action name="system_*" method="{1}"  
  17.             class="com.javacrazyer.action.DeliverCompanyAction">  
  18.             <result name="save">/html/system/system_list.action</result>  
  19.             <result name="list">/html/system/DeliverCompany.jsp</result>  
  20.             <result name="delete">/html/system/system_list.action</result>  
  21.             <result name="update">/html/system/DeliverCompanyDetail.jsp</result>  
  22.             <result name="update2">/html/system/system_list.action</result>  
  23.             <result name="findByShortName">/html/system/DeliverCompanyShow.jsp</result>  
  24.             <!-- 根据配送信息,生成excel -->  
  25.             <result name="excel">/html/order/deliverExcel.jsp</result>  
  26.         </action>  
  27.           
  28.         <!-- 数据字典 -->  
  29.         <action name="dictTables_*" method="{1}"  
  30.             class="com.javacrazyer.action.DictTablesAction">  
  31.             <result name="list">/html/system/DictsList.jsp</result>  
  32.             <result name="list2">/html/system/DictsDetail.jsp</result>  
  33.             <result name="add">/html/system/dictTables_list2.action</result>  
  34.             <result name="delete">/html/system/dictTables_list2.action</result>  
  35.             <result name="addDict">/html/system/dictTables_list.action</result>  
  36.             <result name="update">/html/system/DictsList.jsp</result>  
  37.             <result name="modDictName" type="json">  
  38.                 <param name="includeProperties">success</param>  
  39.             </result>  
  40.         </action>  
  41.   
  42.         <!-- 产品库 -->  
  43.         <action name="cardProduct_*" method="{1}"  
  44.             class="com.javacrazyer.action.CardProductAction">  
  45.             <result name="list">/html/system/ProductMgn.jsp</result>  
  46.             <result name="delete">/html/system/cardProduct_list.action</result>  
  47.             <result name="serch">/html/system/ProductFind.jsp</result>  
  48.             <result name="chgStatus" type="json">  
  49.                 <param name="includeProperties">productStatus</param>  
  50.             </result>  
  51.             <result name="save" type="json">  
  52.                 <param name="includeProperties">  
  53.                     success, tipMsg  
  54.                 </param>  
  55.             </result>  
  56.             <result name="detail">/html/system/ProductDetail.jsp</result>  
  57.             <result name="update">/html/system/cardProduct_list.action</result>  
  58.             <result name="productsJson" type="json">  
  59.                 <param name="includeProperties">  
  60.                     cardProductss\[\d+\]\.productCode, cardProductss\[\d+\]\.productName,  
  61.                 </param>  
  62.             </result>  
  63.         </action>  
  64.   
  65.         <!-- 积分规则 -->  
  66.         <action name="levelQry_*" method="{1}"  
  67.             class="com.javacrazyer.action.LevelQryAction">  
  68.             <result name="list">/html/system/LevelQry.jsp</result>  
  69.             <result name="save">/html/system/levelQry_list.action</result>  
  70.             <result name="detail">/html/system/LevelQryDetail.jsp</result>  
  71.             <result name="update">/html/system/levelQry_list.action</result>  
  72.         </action>  
  73.   
  74.         <!-- 费用类别 -->  
  75.         <action name="cost_*" method="{1}"  
  76.             class="com.javacrazyer.action.SysCostAction">  
  77.             <result name="serch">/html/system/costList.jsp</result>  
  78.             <result name="program">/html/system/programList.jsp</result>  
  79.         </action>  
  80.                   
  81.         <!-- 部门管理 -->  
  82.         <action name="dept_*" method="{1}"  
  83.             class="com.javacrazyer.action.DeptinfoAction">  
  84.             <result name="mgr" type="json">  
  85.                 <param name="includeProperties">  
  86.                     deptCode, success, tipMsg  
  87.                 </param>  
  88.             </result>  
  89.             <result name="del" type="json">  
  90.                 <param name="includeProperties">  
  91.                     deptCode, success, tipMsg  
  92.                 </param>  
  93.             </result>  
  94.             <result name="list">/html/system/dept/changeOrder.jsp</result>  
  95.             <result name="order" type="json">  
  96.                 <param name="includeProperties">  
  97.                     success, tipMsg  
  98.                 </param>  
  99.             </result>  
  100.             <result name="loadDeptInfo" type="json">  
  101.                 <param name="includeProperties">  
  102.                     success, tipMsg, shortName, deptMemo  
  103.                 </param>  
  104.             </result>  
  105.             <result name="deptPage">/html/system/dept/deptList.jsp</result>  
  106.             <result name="back">/html/system/dept/changeOrder.jsp</result>  
  107.         </action>  
  108.           
  109.         <!-- 人员管理 -->  
  110.         <action name="user_*" method="{1}" class="com.javacrazyer.action.UserAction">  
  111.             <result name="userList">/html/system/user/userList.jsp</result>  
  112.             <result name="mgr" type="json">  
  113.                 <param name="includeProperties">  
  114.                     userID, success, tipMsg  
  115.                 </param>  
  116.             </result>  
  117.             <result name="del" type="json">  
  118.                 <param name="includeProperties">  
  119.                     userID, success, tipMsg  
  120.                 </param>  
  121.             </result>  
  122.             <result name="initPass" type="json">  
  123.                 <param name="includeProperties">  
  124.                     userID, success, tipMsg  
  125.                 </param>  
  126.             </result>  
  127.             <result name="userRoleList">/html/system/user/userRoleList.jsp</result>  
  128.             <result name="saveUserRole" type="json">  
  129.                 <param name="includeProperties">  
  130.                     userID, success, tipMsg  
  131.                         </param>  
  132.             </result>  
  133.         </action>  
  134.           
  135.         <!-- 用户可管理部门 -->  
  136.         <action name="userDept_*" method="{1}" class="com.javacrazyer.action.UserDeptAction">  
  137.             <result name="deptPage">/html/system/user/deptList.jsp</result>  
  138.             <result name="saveUserDept" type="json">  
  139.                 <param name="includeProperties">  
  140.                     success, tipMsg  
  141.                 </param>  
  142.             </result>  
  143.         </action>  
  144.           
  145.         <!-- 用户信息修改 -->  
  146.         <action name="userInfo_*" method="{1}"  
  147.             class="com.javacrazyer.action.UserinfoAction">  
  148.             <result name="detail">/html/system/PasswordMgn.jsp</result>  
  149.             <result name="update">/html/system/PasswordMgn.jsp</result>  
  150.             <result name="selfInfo">/html/system/SelfInfo.jsp</result>  
  151.             <result name="modSelfInfo" type="json">  
  152.                 <param name="includeProperties">userName</param>  
  153.             </result>  
  154.         </action>  
  155.   
  156.         <!-- 角色管理 -->  
  157.         <action name="role_*" method="{1}" class="com.javacrazyer.action.RoleAction">  
  158.             <result name="roleList">/html/system/role/roleList.jsp</result>  
  159.             <result name="ui">/html/system/role/roleMgr.jsp</result>  
  160.         </action>  
  161.           
  162.     </package>  
  163.   
  164.     <package name="storeHouse" namespace="/storeHouse" extends="default">  
  165.         <action name="*" method="{1}" class="com.javacrazyer.action.StoreHouseAction">  
  166.             <result name="ui">/html/system/storehouse/storeHouseMgr.jsp</result>  
  167.             <result name="list">/html/system/storehouse/storeHouseList.jsp</result>  
  168.         </action>  
  169.     </package>  
  170.       
  171. </struts>  
  172. </span>  

 

那么针对这么几个xml,在src/struts.xml中就应该像下面这么引用

Xml代码   收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.   
  8.     <constant name="struts.objectFactory" value="spring" />  
  9.     <constant name="struts.objectFactory.spring.autoWire" value="name" />  
  10.     <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
  11.     <constant name="struts.devMode" value="true" />  
  12.     <constant name="struts.configuration.xml.reload" value="true" />  
  13.     <constant name="struts.action.extension" value="action"></constant>  
  14.     <constant name="struts.i18n.encoding" value="UTF-8"></constant>    
  15.     <constant name="struts.ognl.allowStaticMethodAccess" value="true" />   
  16.       
  17.   
  18.         <!-- 引入struts2其他类似配置文件 -->  
  19.     <include file="META-INF/struts2/struts2-login.xml" />  
  20.     <include file="META-INF/struts2/struts2-member.xml" />  
  21.     <include file="META-INF/struts2/struts2-system.xml" />      
  22.     <include file="META-INF/struts2/struts2-order.xml" />  
  23.     <include file="META-INF/struts2/struts2-deliver.xml" />  
  24.   
  25.       
  26.       
  27.     <package name="default" namespace="/" extends="struts-default, json-default">  
  28.         <!-- 定义拦截器 -->  
  29.         <interceptors>  
  30.             <interceptor name="appInterceptor"  
  31.                 class="com.javacrazyer.action.AppInterceptor" />  
  32.             <interceptor-stack name="appInterceptorStack">  
  33.                 <interceptor-ref name="appInterceptor" />  
  34.                 <interceptor-ref name="defaultStack" />  
  35.             </interceptor-stack>  
  36.         </interceptors>  
  37.         <default-interceptor-ref name="appInterceptorStack"/>  
  38.           
  39.         <default-action-ref name="index" />  
  40.   
  41.         <global-results>  
  42.             <result name="login">/index.jsp</result>  
  43.             <result name="postRequest">/postRequest.jsp</result>  
  44.             <result name="errorPage">/errorPage.jsp</result>  
  45.         </global-results>  
  46.   
  47.         <action name="index">  
  48.             <result type="redirectAction">  
  49.                 <param name="actionName">Login_Action</param>  
  50.                 <param name="namespace">/</param>  
  51.             </result>  
  52.         </action>  
  53.           
  54.     </package>  
  55.   
  56.     <!-- Add packages here -->  
  57.   
  58. </struts>  
  59. </span> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值