`
JavaCrazyer
  • 浏览: 3012258 次
  • 性别: Icon_minigender_1
  • 来自: 河南
社区版块
存档分类

Struts2温习(1)--最基本的示例

阅读更多

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

 

 

 


 

  • 大小: 3.3 KB
分享到:
评论
1 楼 chenjian1567 2012-04-26  
谢谢lz,学习了。。

相关推荐

Global site tag (gtag.js) - Google Analytics