Maven 实现Struts2注解配置步骤详解
1,pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wiker</groupId> <artifactId>struts2</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>struts2 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.3.14</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.14</version> </dependency> </dependencies> <build> <finalName>struts2</finalName> </build> </project>
2, struts2.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.i18n.encoding" value="UTF-8"/> <!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 --> <constant name="struts.action.extension" value="action,do,htm"/> <!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 --> <constant name="struts.configuration.xml.reload" value="true"/> <!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 --> <constant name="struts.devMode" value="false"/> <!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭 --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 指定由spring负责action对象的创建 <constant name="struts.objectFactory" value="spring" /> --> <!-- 是否开启动态方法调用 --> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> </struts>
3,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>
4,TestAction
package com.wiker.struts2; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; @ParentPackage("struts-default") @Results( { @Result(name = "success", location = "/index.jsp"), @Result(name = "error", location = "/index.jsp") }) public class TestAction extends ActionSupport { private static final long serialVersionUID = 1L; @Action(value="test") public String mainDataSouse1Search() { System.out.println(333); return "success"; } }
配置好启动,在浏览器上输入http://localhsot:8080/struts2/test/test.action便能打印333了。
附源码免积分下载:http://download.csdn.net/detail/yangwei19680827/6239247