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" name="code"><?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}" 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="*" 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="*" 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="*" method="{1}"><result name="deliverList">/html/deliver2/deliverList.jsp</result></action> </package><!-- 物流配送增删改查 --> <package name="/logistics2" namespace="/logistics2" extends="default"><action name="*" 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="*" 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="*" 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="*" 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}" name="code"><?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}"method="{1}"method="{1}"type="json"><param name="includeProperties">success</param></result></action><!-- 产品库 --><action name="cardProduct_*" method="{1}"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}"method="{1}"method="{1}"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}" 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}" type="json"><param name="includeProperties">success, tipMsg </param></result></action><!-- 用户信息修改 --><action name="userInfo_*" method="{1}"type="json"><param name="includeProperties">userName</param></result></action><!-- 角色管理 --><action name="role_*" method="{1}" namespace="/storeHouse" extends="default"><action name="*" method="{1}" name="code"><?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"/><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>
?
?
?
?
1 楼 chenjian1567 2012-04-26 谢谢lz,学习了。。