使用Maven搭建Struts2项目

大家好 今天我们来使用maven构建Struts2项目,关于maven的环境搭建请参考  Maven环境搭建和介绍

1. Struts2框架的环境配置

1.1下载

进入apache的官方网:https://dist.apache.org/repos/dist/release/struts/      

这里为大家提供了struts的各种版本,供大家使用。

下载完后,解压到本地磁盘,该文件夹包含如下文件结构:
      apps:该文件夹下包含了struts 2 的示例应用。
      docs:struts2的相关文档,包含struts2的快速入门、struts2的帮助文档及API文档等内容。
      lib:该文件夹下包含了struts2框架的核心类库,以及struts2的第三方插件类库。
      src:该文件下包含了struts2框架的全部源代码。

1.2添加Struts2依赖

这里主需要在pom.xml中添加一个struts-core的依赖即可(这里所使用的是 struts 2.3.14版本):
<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.demo</groupId>
  <artifactId>struts2_project</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>struts2 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <!-- 属性配置 -->  
  <properties>  
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  </properties>
  
  <dependencies>
    <!-- junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.9</version>
      <scope>test</scope>
    </dependency>
    
    <!-- struts2依赖包 -->
    <dependency>
    	<groupId>org.apache.struts</groupId>
    	<artifactId>struts2-core</artifactId>
    	<version>2.3.14</version>
    </dependency>
    
  </dependencies>
  <build>
    <finalName>struts2</finalName>
  </build>
</project>

1.3新建一个Action

在src/main/Java目录下新建一个UserAction.java

package com.demo.action;

import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	public String login() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpServletResponse response = ServletActionContext.getResponse();
			request.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=utf-8");
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			System.out.println("name->" + username + ",password->" + password);
			if ("admin".equals(username) && "123456".equals(password)) {
				return SUCCESS;
			} else {
				return "login";
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
}

1.4配置Struts.xml

在下载的struts2包里面,解后会有一个apps文件夹,打开之后里面有一些官方给你的参考示例。随便打开一个,里面就会有struts.xml文件,直接把这个文件复制过来再修改就可以了,如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <package name="default" extends="struts-default" namespace="/">

        <action name="login" class="com.demo.action.UserAction" method="login">
            <result name="success">index.jsp</result>
            <result name="login">login.jsp</result>
        </action>

    </package>

</struts>

1.5配置web.xml

1编辑web应用的web.xml配置文件,配置struts2的核心Filter,这个可以从Struts2官网(http://struts.apache.org/development/2.x/docs/webxml.html)找到示例。
2前面我们提到Maven构建web项目时资源文件存放在src/main/resources目录下,但是Struts2的配置文件struts.xml默认要放在类路径下,也就是src下。
这个位置可以修改,就是在web.xml中加入以下内容:
<init-param>   
   <param-name>config</param-name>   
   <param-value>../../resources/struts.xml</param-value>   
</init-param> 
配置完成后web.xml内容如下:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
	<init-param>
		<param-name>config</param-name>
		<param-value>../../resources/struts.xml</param-value>
	</init-param>
	
	<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>

	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
</web-app>

2.测试

新建两个页面login.jsp,index.jsp,内容如下:
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
<!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>登录界面</title>  
    </head>  
      
    <body> 
<form action="login" method="post"> 
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" /> </td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password" /> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登录" />
<input type="reset" value="重置" /></td>
</tr>
</table>
</form>
    </body>  
</html>  

index.jsp 
<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
<!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 Maven</title>  
    </head>  
      
    <body>  
        <p>大家好,欢迎进入Maven Struts2应用!</p>  
    </body>  
</html>  


注意点:在struts 2.5.2版本之后 ,struts的结构发生了改变。具体请参考  Struts2 2.5.2的套路

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
搭建Struts2项目需要按照以下步骤进行: 1. 创建Maven项目 在命令行中执行以下命令,创建一个Maven项目: ``` mvn archetype:generate -DgroupId=com.example -DartifactId=my-struts2-app -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false ``` 其中,`-DgroupId`表示项目的groupId,`-DartifactId`表示项目的artifactId,`-DarchetypeArtifactId`表示使用的模板,这里使用的是maven-archetype-webapp。 2. 添加Struts2依赖 在`pom.xml`文件中添加Struts2依赖: ```xml <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.26</version> </dependency> ``` 3. 创建Struts2配置文件 在`src/main/resources`目录下创建`struts.xml`文件,配置Struts2的基本信息,例如: ```xml <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" extends="struts-default"> <action name="hello" class="com.example.MyAction"> <result>/index.jsp</result> </action> </package> </struts> ``` 4. 创建Struts2 Action 在`src/main/java`目录下创建一个Java类,例如`MyAction.java`,作为Struts2的Action,例如: ```java package com.example; import com.opensymphony.xwork2.ActionSupport; public class MyAction extends ActionSupport { private String message; public String execute() { message = "Hello, Struts2!"; return SUCCESS; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } ``` 5. 创建JSP页面 在`src/main/webapp`目录下创建一个JSP页面,例如`index.jsp`,用于显示Action的返回结果: ```html <html> <head> <title>Struts2 Example</title> </head> <body> <h1>${message}</h1> </body> </html> ``` 6. 运行项目 使用以下命令在Tomcat中运行项目: ``` mvn tomcat7:run ``` 然后在浏览器中访问`http://localhost:8080/my-struts2-app/hello`即可看到页面上显示的内容"Hello, Struts2!"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值