Struts2 maven项目简单案例

Posted fengqixueluo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2 maven项目简单案例相关的知识,希望对你有一定的参考价值。

  • 案例--步骤

  1. 创建Maven web项目
  2. 在pom.xml导入相关jar包
  3. 配置web.xml
  4. 编写action代码
  5. 配置struts.xml
  6. 访问方式
  • 案例步骤图解

 

  • 详细代码及配置

  • 导入jar包:pom.xml
 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <groupId>com.xxxx</groupId>
 5     <artifactId>Struts2Test0809</artifactId>
 6     <version>0.0.1-SNAPSHOT</version>
 7     <packaging>war</packaging>
 8 
 9     <dependencies>
10         <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
11         <dependency>
12             <groupId>org.apache.struts</groupId>
13             <artifactId>struts2-core</artifactId>
14             <version>2.2.3</version>
15         </dependency>
16         <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
17         <dependency>
18             <groupId>javax.servlet</groupId>
19             <artifactId>javax.servlet-api</artifactId>
20             <version>3.1.0</version>
21             <scope>provided</scope>
22         </dependency>
23 
24 
25     </dependencies>
26     <build>
27         <plugins>
28             <plugin>
29                 <groupId>org.apache.maven.plugins</groupId>
30                 <artifactId>maven-compiler-plugin</artifactId>
31                 <configuration>
32                     <source>1.8</source>
33                     <target>1.8</target>
34                 </configuration>
35             </plugin>
36         </plugins>
37     </build>
38 </project>
  • 配置web.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     version="2.5">
 6     <display-name>Struts2Test0809</display-name>
 7     <welcome-file-list>
 8         <welcome-file>index.html</welcome-file>
 9         <welcome-file>index.htm</welcome-file>
10         <welcome-file>index.jsp</welcome-file>
11         <welcome-file>default.html</welcome-file>
12         <welcome-file>default.htm</welcome-file>
13         <welcome-file>default.jsp</welcome-file>
14     </welcome-file-list>
15 
16     <filter>
17         <filter-name>struts2</filter-name>
18         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
19     </filter>
20     <filter-mapping>
21         <filter-name>struts2</filter-name>
22         <url-pattern>/*</url-pattern>
23     </filter-mapping>
24 </web-app>
  • 编写action代码
 1 package com.xxxx.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class TestAction extends ActionSupport {
 6 
 7     public String sayHello(){
 8         
 9         System.out.println(">>>>test Ok");
10         
11         return SUCCESS;
12     }
13 }
  • 配置struts.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2         <!DOCTYPE struts PUBLIC
 3             "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
 4             "http://struts.apache.org/dtds/struts-2.5.dtd">
 5 <struts>
 6     <!-- 启用动态方法加载 -->
 7     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
8 <package name="hell" namespace ="/hello" extends="struts-default"> 9 <action name="test" class="com.xxxx.action.TestAction"> 10 <result name="success">/Hello.jsp</result> 11 </action> 12 </package> 13 </struts>
  • 在URL访问
http://localhost:8080/Struts2Test0809/hello/test!sayHello
/项目名称/package_namespace/action_name!action_functionName

 

以上是关于Struts2 maven项目简单案例的主要内容,如果未能解决你的问题,请参考以下文章

Spring+Mybatis+MySql+Maven 简单的事务管理案例

Maven构建Struts2项目

构建 struts2 spring3 mybatis 的maven项目 构建 pom.xml

struts2之day01——02入门案例

使用Maven搭建Struts2框架的开发环境

SSH项目总结 Maven构建SSH项目遇到的问题