搭建strus2框架

1.新建一个web工程; 2.把struts2核心包拷到工程的lib下; 3.在web.xml中配置Struts2的前端控制器; 4.在src目录下新建struts.xml文件;struts-2.0.dtd 建立第一个struts例子 1.新建一个Action类,完成从action到jsp功能; 2.在struts.xml里进行配置; 3.编写jsp页面,接收action中传的值; 4.测试效果。 ------------------------------------------------ 配置web.xml <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> src下 struts.xml struts-2.0.dtd 配置struts.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "struts-2.0.dtd"> <struts> <package name="main" extends="struts-default"> <action name="hello" class="com.bw.action.HelloAction"> <result name="ha">/hello.jsp</result> </action> </package> </struts> <package name="main主包名" extends="struts-default  struts2自带的默认拦截器"> <action name="hello(请求路径.action)" class="com.bw.action.HelloAction(完整的action类)"> <result name="ha(转发名与action里的转发相对应)">/hello.jsp(转发地址)</result> </action> </package> 第一个struts2例子 hello struts-default.xml 17个默认拦截器  <interceptor-stack name="defaultStack">                 <interceptor-ref name="exception"/>                 <interceptor-ref name="alias"/>                 <interceptor-ref name="servletConfig"/>                 <interceptor-ref name="prepare"/>                 <interceptor-ref name="i18n"/>                 <interceptor-ref name="chain"/>                 <interceptor-ref name="debugging"/>                 <interceptor-ref name="profiling"/>                 <interceptor-ref name="scopedModelDriven"/>                 <interceptor-ref name="modelDriven"/>                 <interceptor-ref name="fileUpload"/>                 <interceptor-ref name="checkbox"/>                 <interceptor-ref name="staticParams"/>                 <interceptor-ref name="params">                   <param name="excludeParams">dojo\..*</param>                 </interceptor-ref>                 <interceptor-ref name="conversionError"/>                 <interceptor-ref name="validation">                     <param name="excludeMethods">input,back,cancel,browse</param>                 </interceptor-ref>                 <interceptor-ref name="workflow">                     <param name="excludeMethods">input,back,cancel,browse</param>                 </interceptor-ref>    </interceptor-stack> ----------------------------编写Action类三种方式----------------------------------------------------- 编写Action类三种方式 第一种:直接写action 第二种:实现Action接口 第三种:继承ActionSupport ------------------------------------------------------- 编写Action类三种方式 一、编写普通action类,定义名称为execute()的方法,在访问action时,会默认访问此方法。 二、编写action类,实现Action接口,重写execute()方法,在访问时,默认访问此方法。 三、编写action类,继承ActionSupport父类,重写execute()方法,在访问时,默认访问此方法。 注意:在导入Action接口的时候,有好多同名不一样包的类都叫Action,好比javax.swing.Action, 必定要注意,咱们须要使用Xwork2中的Action接口。 Action接口:五个常量,一个执行方法 public static final String SUCCESS = "success";//成功  public static final String NONE = "none";      //null public static final String ERROR = "error";    //错误 public static final String INPUT = "input";    //错误页面 public static final String LOGIN = "login";    //登陆 public String execute() throws Exception;      //执行方法 ---------Action中基本的数据对应方式(二种方式)-------------------------------------------------------------------- 属性驱动(FieldDriven)和模型驱动(ModelDriven)。 属性驱动又有二种 1.简单数据类型属性驱动 2.域对象的属性驱动 ---------------------------------------------- 1、属性驱动(对应的是基本数据类型) 1.属性驱动 2.域对象(javaBean风格) 2、模型驱动(ModelDriven) --------------------------------------------- 属性驱动(FieldDriven)(对应的是基本数据类型):属性驱动有两种 第一种属性驱动--简单数据类型 action类中写 private String name; 设置setter getter方法 index.jsp 页面上取值${name} 第二种属性驱动--域对象(javaBean) 1.编写Model类-用于存储对象 2.action类中写 private LoginModel loginModel = new LoginModel(); 设置setter getter方法 //收集数据须要用setter getter方法 3.表单页面 域对象.属性 <form action="login2.action" method="post"> 登陆名2:<input type="text" name="loginModel.login_name"><br> 密码2:<input type="password" name="loginModel.pwd"><br> <input type="submit" value=" 登陆 "> </form> 4.页面上取值${loginModel.name} --------------------- 模型驱动(ModelDriven) 1.编写Model类-用于存储对象 2.action类implements ModelDriven //模型驱动  private LoginModel loginModel = new LoginModel(); //实现模型驱动的方法,返回Model对象 //只要实现模型驱动ModelDriven中 getModel()方法 ,就能收集表单数据 public Object getModel() { return loginModel; } 3.表单页面  <!-- 使用模型驱动取值   由于模型就是loginModel因此就不须要加loginModel了 直接用属性就行 --> <form action="login3.action" method="post"> 登陆名3:<input type="text" name="login_name"><br> 密码3:<input type="password" name="pwd"><br> <input type="submit" value=" 登陆 "> </form> 4.页面上取值 ${login_name }---${pwd } ----------------------------------------------------------- 总结: 1.Action中基本的数据对应方式,通常都使用域对象。 2.少用模型驱动。由于模型对象只能实现一个getModel()方法 3.模型驱动不须要用setter getter方法 4.做用域对象的好处:对象.属性能区分是哪个模块的 <form action="student.action" method="post"> 姓名:<input type="text" name="studentModel.name"><br> 密码:<input type="password" name="studentModel.pwd"><br> <input type="submit" value=" 登陆 "> </form> <form action="teacher.action" method="post"> 姓名:<input type="text" name="teacherModel.name"><br> 密码:<input type="password" name="teacherModel.pwd"><br> <input type="submit" value=" 登陆 "> </form> -------------struts.xml-------------------------------------------------------------------------------- struts.xml <package name="main(包的名称)" namespace="/main(包的命名空间)" extends="struts-default(要继承的包-继承的是拦截器)"> </package> 包与包之间能够继承 <package name="main1"  extends="main"> </package> 引入其它配置文件:用于分模块 <include file="struts-login.xml" /> <include file="struts-user.xml" /> <include file="struts-book.xml" /> ---------Action中的方法使用及调用---------------------------------------------------- Action的默认方法是execute(); http://localhost:8080/struts2/main/hello.action http://localhost:8080/struts2/main/hello!add.action    对应HelloAction类中的add方法 public String add() { System.out.println("-------helloaction的add方法------"); return "add"; } http://localhost:8080/struts2/main/hello!del.action    对应HelloAction类中的del方法 public String del() { System.out.println("-------helloaction的del方法------"); return "del"; }  总结:xxx!方法名.action -------------通配符的使用------------------------------------------------------------ xml中name路径的通配符的使用 <!-- 通配符的使用 --> <action name="hello_*" class="com.bw.action.HelloAction" method="{1}"> <result name="ha">/hello.jsp</result> </action> http://localhost:8080/struts2/main/hello_add.action  对应HelloAction类中的add方法 http://localhost:8080/struts2/main/hello_del.action  对应HelloAction类中的del方法 -------------- <action name="*_*" class="com.bw.action.{1}Action" method="{2}"> <result name="ha">/hello.jsp</result> </action> http://localhost:8080/struts2/main/User_add.action      经过配置文件能够对应 UserActioin类 http://localhost:8080/struts2/main/Book_add.action      经过配置文件能够对应 BookActioin类 -----------ActionContext和ServletActionContext-能够取到request session application------------------------ ----------如何取到requst session application(掌握)--------------------------------------------------------- //第一组(解耦合) ActionContext ac = ActionContext.getContext(); Map request = (Map) ac.get("request"); Map session = ac.getSession(); Map application = ac.getApplication(); //第二组 PageContext pageContext = ServletActionContext.getPageContext(); HttpServletRequest request2 = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); HttpSession session2 = request2.getSession(); ServletContext application2 = ServletActionContext.getServletContext(); ActionContext ac2 = ServletActionContext.getContext(); -------------------------------------Result基础--------------------------------------------------------- Reult能干什么和作什么? 简单的说,Result是Action执行完后返回的一个字符串,它指示了Action执行完成后,下一个页面在哪里 具体页面在哪里,是在struts.xml里面配置的。就是每一个<action>元素里面,配置的<result>子元素,例如: <action name="hello" class="com.bw.action.HelloAction"> <result name="add" type="dispatcher">/hello.jsp</result> <result name="del">/hello.jsp</result> </action> --------------------------------------------------------------------------- 在Struts2中,预约义了一些Result的字符串常量,以下: SUCCESS:表示Action执行成功,显示结果视图给用户,值为字符串"success"。 NONE:表示Action执行成功,不须要显示视图给用户,值为字符串"none"。 ERROR:表示Action执行失败,显示错误页面给用户,值为字符串"error"。 INPUT:表示执行Action须要更多的输入信息,回到input对应的页面,值为字符串"input"。 LOGIN:表示因用户没有登录而没有正确执行,将返回该登录视图,值为字符串"login"。 固然,你能够不使用这些字符串常量,而是使用本身定义的字符串,这样作是没有问题的,只要你在Action里面返回的字符串, 跟在struts.xml里面配置的result的name属性值同样就能够了。 -------预约义的ResultType----------------------------------------------------- 在Struts2中,预约义了不少ResultType,其实就是定义了不少展现结果的技术。 Struts2把内置的<result-type>都放在struts-default包中。 Struts2预约义以下: <result-types>             <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>             <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>             <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>             <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>             <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>             <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>             <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>             <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>             <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>             <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />             <!-- Deprecated name form scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707 -->             <result-type name="redirect-action" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>             <result-type name="plaintext" class="org.apache.struts2.dispatcher.PlainTextResult" /> </result-types> ---------------------------------------------------------- <result>中type若是没有配置,默认就是“dispatcher”。转发到jsp页面上 <action name="hello" class="com.bw.action.HelloAction"> <result name="add" type="dispatcher">/hello.jsp</result> <result name="del">/hello.jsp</result> </action> ----------------------------------------------------------- 预约义的ResultType 常见的结果类型 1.chain---Action处理完用户请求以后,转发到下一个Action继续处理。造成“链”式处理。 (一个Action里的做用域的值到下一个Action还能用) 重定向也是从一个Action到另外一个Action可是不能传值 2.redirect---重定向到新的URL。会生成一个新的请求。原有的请求参数,请求属性都会丢失。 3.disptcher---默认值。转发。转发的请求参数,请求属性不会丢失。 4.plainText---直接显示视图页面的源代码。 5.stream---直接生成“二进制”流做为向应。 6.redirectAction 说明: 全局result与局部result: 1.result元素放在action,就是局部result。 2.result元素放在global-results,就是全局result。 总结:尽可能少用全局result---只有在多个Action都具备某个通用性质的result时,才考虑使用全局result。