使用Maven创建struts2工程(注解版)

来源:互联网 发布:英国留学女生知乎 编辑:程序博客网 时间:2024/04/23 17:45

1. 创建maven项目buding(具体步骤见上一节)

配置web.xml

<filter>

        <filter-name>struts2</filter-name>

        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

    </filter>

    <filter-mapping>

        <filter-name>struts2</filter-name>

        <url-pattern>/*</url-pattern>

 </filter-mapping>

1. 引入Convention插件的包struts2-convention-plugin-2.1.8.1.jar来实现struts2的零配置

pom.xml中加入以下代码

<dependency>

        <groupId>org.apache.struts</groupId>

        <artifactId>struts2-convention-plugin</artifactId>

        <version>2.1.8</version>

   </dependency>

在黑窗口中用命令的方式创建的项目好像发布的时候没有把包发布过去,暂时用了一种解决方法,就是在自己项目的WEB-INF目录下建立lib文件夹,手动把需要的包拷贝过去,然后在pom.xml中加入以下代码:

<build>

        <plugins>

            <plugin>

              <artifactId>maven-compiler-plugin</artifactId>

              <configuration>

                  <source>1.6</source>

                  <target>1.6</target>

                  <encoding>UTF-8</encoding>

                  <compilerArguments>

                   <extdirs>src\main\webapp\WEB-INF\lib</extdirs>

                 </compilerArguments>

              </configuration>

            </plugin>

        </plugins>

</build>

这样发布的时候就会把lib下的jar包发不过去了

3.验证Convention插件是否可以使用

Convention会默认所有的结果页面都存储在WEB-INF/content下,你也可以通过struts.xml

<constant name="struts.convention.result.path" value="/WEB-INF/page" />来修改为page文件夹,所以需要新建content文件夹,然后新建hello-world.jsp(具体一些struts的配置问题参考其他这里是做最简单的实现)

hello-world.jsp代码如下

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Hello World!</title>

</head>

<body>

<h2>

成功了没有啊?

</h2>

</body>

</html>

然后运行项目,在浏览器里输入:http://localhost:8080/buding/hello-world

hello-world页面成功显示,至此,Convention已经能正常运行,并找到了结果。即使在没有action存在情况下,convention也会根据URL规则来找到结果页面。

4.接下来,看一下通过action来返回jsp页面,首页先在java目录下新建一个名为HelloWord1Actionaction

HelloWorld1Action代码如下:

package com.test.action;

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWord1Action extends ActionSupport {

private static final long serialVersionUID = 1L;

public String execute() throws Exception {

return SUCCESS;

  }

}

content目录下新建hello-world1.jsp

在浏览器就可以访问http://localhost:8080/buding/hello-world1(这是约定大于配置,是根据action的名字来的去掉类名的Action前面一个单词就直接xxx,两个单词就用‘-’隔开)

5.只用注解

package com.test.action;

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWord1Action extends ActionSupport {

private static final long serialVersionUID = 1L;

  @Action(value = "struts-test3")  

public String execute() throws Exception {

return SUCCESS;

  }

}

这样就可以在浏览器就可以访问http://localhost:8080/buding/struts-test3

不过在content文件夹下要有struts-test3.jsp文件,execute()可以自己随便命名

用了@Action之后就不用考虑action的名称了。

 关于定义有返回页面的,返回到指定页面

public class HelloWord1Action extends ActionSupport {

private static final long serialVersionUID = 1L;

@Action(value = "struts-test", results = { @Result(name = "success"

type = "dispatcher", location = "struts-test3.jsp") })  

public String list() throws Exception {

return SUCCESS;

  }

}

http://localhost:8080/buding/struts-test    返回struts-test3.jsp前提是struts-test3.jspcontent目录下存在

返回到action

public class BuDingAction extends ActionSupport {

private static final long serialVersionUID = 1L;

 @Action(value = "struts-test", results = { @Result(name = "success"

type = "dispatcher", location = "struts-test3.jsp") })   

public String list() throws Exception {

return SUCCESS;

  }

}

//访问http://localhost:8080/buding/bu-ding2   执行完之后继续执行list方法

 @Action(value = "bu-ding2", results = { @Result(name = "success"

type="redirectAction",params={"actionName","struts-test"})}) 

public String list2() throws Exception {

return SUCCESS;

 }

}

6.多级的比如BuDingActioncom.test.action.app下,代码如下:  

package com.test.action.app;

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;

public class BuDingAction extends ActionSupport {

private static final long serialVersionUID = 1L;

 @Action(value = "bu-ding")  

public String execute() throws Exception {

return SUCCESS;

  }

}

content目录下要新建一个app文件夹,里面新建bu-ding.jsp文件浏览器中这样访问http://localhost:8080/buding/app/bu-ding 就可以显示bu-ding.jsp页面了。

package com.test.action.app;

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.Result;

import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

public class BuDingAction extends ActionSupport {

  @Action(value = "bu-ding", results = { @Result(name = "success"

 type = "dispatcher", location = "bu-ding2.jsp") })  

public String list() throws Exception {

return SUCCESS;

@Action(value = "bu-ding2", results = { @Result(name = "success"

 type="redirectAction",params={"actionName","bu-ding"})}) 

public String list2() throws Exception {

return SUCCESS;

  }

}

http://localhost:8080/buding/app/bu-ding  content/app下要有bu-ding2.jsp存在。

定义全局的返回页面

package com.test.action.app;

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.Result;

import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

@Results( {

@Result(name = "success", type = "dispatcher", location = "bu-ding2.jsp")

})

public class BuDingAction extends ActionSupport {

  @Action(value = "bu-ding", results = { @Result(name = "success"

 type = "dispatcher", location = "bu-ding.jsp") })  

public String list() throws Exception {

return SUCCESS;

@Action(value = "bu-ding2"

public String list2() throws Exception {

return SUCCESS;

  }

}

这样http://localhost:8080/buding/app/bu-ding  就会返回到bu-ding.jsp因为定义了自己的返回页面

http://localhost:8080/buding/app/bu-ding2就会返回到bu-ding2.jsp返回上面公共定义的。

前提是content/app下有这两个页面

以上项目没有在resources下存在struts.xml文件就可以直接运行,但是也可以创建struts.xml文件来配置一些其他的,比如

<struts>  

    <!-- 以下两项在生产模式中要去掉 -->  

        <constant name="struts.devMode" value="true"/>   

        <constant name="struts.convention.classes.reload" value="true" />  

        <!-- 定义默认的访问后缀 -->  

        <constant name="struts.action.extension" value="do,actio,," />  

        <constant name="struts.enable.SlashesInActionNames" value="true"/>

       

        <!-- 设定默认的字符编码 -->  

        <constant name="struts.i18n.encoding" value="UTF-8"/>  

          

        <!-- 可以自定义页面的存放路径 -->  

        <!-- <constant name="struts.convention.result.path" value="/WEB-INF/page" />  -->  

          

        <constant name="struts.convention.action.suffix" value="Action"/>

        <constant name="struts.convention.action.name.lowercase" value="true"/>

        <constant name="struts.convention.action.name.separator" value="_"/>  <!-- <constant name="struts.convention.action.name.separator" value="-"/> -->(默认是中间一横)

       

        <!--设置默认的父包,一般我们都设置一个default包继承自struts-default。  -->  

        <constant name="struts.convention.default.parent.package" value="struts-default"/>

       

        <!--    

        如果此值设为true,如果一个action的命名空间为/login,名称为HelloWorldActionresult返回值是success

        默认会找到/WEB-INF/pages/login/hello_world.jsp(如果有hello_world_success.jsp就找这个文件,连接符"_"是在上面配置的).

        如果有一个actionresult返回值是"error",就会找/WEB-INF/pages /login/hello_world_error.jsp

       如果此值设为false,如果一个action的命名空间为/login,名称为HelloWorldActionresult返回值是success

       默认会找到/WEB- INF/pages/login/hello_world/index.jsp(如果有success.jsp就找这个文件)。如果有一个actionresult返回值是"error"

       就会找/WEB-INF/pages /login/hello_world/error.jsp。 -->

       <constant name="struts.convention.result.flatLayout" value="true"/>

        <struts>