整合 struts2.3.4

来源:互联网 发布:知乎 民贷天下 编辑:程序博客网 时间:2024/06/05 17:30

1.到官网下载 struts2.3.4 

2.把lib下的(1)commons-fileupload-1.2.2(2)commons-io-2.0.1(3)commons-lang-2.4(4)commons-lang3-3.1(5)commons-logging-1.1.1(6)commons-logging-api-1.1(7)freemarker-2.3.19(8)javassist-3.11.0.GA

(9)ognl-3.0.5(10)struts2-core-2.3.4.1(11)xwork-core-2.3.4.1

这11jar拷贝到项目lib下

3.配置web.xml

 web.xml添加

<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

4.配置struts.xml

  1)struts.xml方在src下

  2)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>     <package name="main" extends="struts-default" namespace="">         <action name="login" class="com.test.LoginAction" method="execute">             <result name="loginSuccess">/success.jsp</result>            <result name="loginFailure">/failure.jsp</result>        </action>         </package> </struts>

  3)编写LoginAction类

package com.web;import com.opensymphony.xwork2.ActionSupport;public class LoginAction  extends  ActionSupport{    public String message ="Hello Word";    private String username;    private String passowrd;    public String execute() throws Exception {        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxx");        username="1";        if (username.equals("struts2")) {             System.out.println("aaaaaaaaaa");            return "success";        } else {            System.out.println("dddddddddd");            return "error";        }    }     
  

  public String abc(){
    System.out.println("abc");
    return "success";
  }

  public String cba(){
    System.out.println("cba");
    return "error";
  }

    }

这样 你可以可以通过 hppt://pc-xxx:8080/xxx/login.action来访问了

 

5、如何让struts2 实现零配置

在导入之前jar的基础上,再增加3个jar,它们分别为

  1)struts2-convention-plugin-2.3.4.1

  2)asm-3.3

  3)asm-commons-3.3

 

  4)web.xml在之前的配置基础上不变,改变struts.xml的配置,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.convention.action.disableScanning" value="false"/>     <constant name="struts.convention.classes.reload" value="true" />    <constant name="struts.convention.package.locators" value="web" />    <constant name="struts.convention.result.path" value="/" />  </struts>

说明:

 <constant name="struts.convention.package.locators" value="web" /> 为action包
如果你action类在 com.web下,那么value="http://www.cnblogs.com/sunshin2012/archive/2012/09/19/web"
如图:

 

 <constant name="struts.convention.result.path" value="/" /> 为你的jsp模板放在WebRoot下的哪个位置,如果为WebRoot的根目录下,value='http://www.cnblogs.com/',如果为WebRoot的xxx文件夹下,则value='http://www.cnblogs.com/xxx'

4)此时 模板的命名要以action的类名来命名相对于来命名,如LoginAction,则对于的user-返回的string来命名,如:
login-success.jsp
login-error.jsp

5)此时 你就可以通过 hppt://pc-xxx:8080/xxx/login.action、
hppt://pc-xxx:8080/xxx/login!abc.action、
hppt://pc-xxx:8080/xxx/login!cba.action
等来访问了
该配置经本人测试成功,如您出现不成功的情况,请检查是否出现包冲突。