首页 > 代码库 > Struts2 maven项目简单案例
Struts2 maven项目简单案例
-
案例--步骤
- 创建Maven web项目
- 在pom.xml导入相关jar包
- 配置web.xml
- 编写action代码
- 配置struts.xml
- 访问方式
-
案例步骤图解
-
详细代码及配置
- 导入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="http://www.mamicode.com/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项目简单案例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。