🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
在前两篇博客中,使用Maven构建了Web项目,在这篇博客中写一下,怎样构建一个简单的Struts2项目。 在准备过程中发现,要使用好Maven,个人觉得要好好利用这两个网站: [http://mvnrepository.com/](http://mvnrepository.com/) [http://search.maven.org/](http://search.maven.org/) 由于自己对Maven的理解不是非常深,所以学习的时候遇到很多简单的问题都没法解决![委屈](https://box.kancloud.cn/2016-01-25_56a5a367bba9a.gif) ,走了很多弯路 ### 1. 新建一个基本的Web项目 这和前面讲的是一样的,可以参考前面的博客 ### 2. 添加Struts2依赖 这里主需要在pom.xml中添加一个struts-core的依赖即可: ~~~ <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.deppon.demo</groupId> <artifactId>test02</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>test02 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>3.8.1</version> <scope>test</scope> </dependency> <!-- Struts2 依赖 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.1</version> </dependency> </dependencies> <build> <finalName>test02</finalName> </build> </project> ~~~ 之后,Maven会自动从网上下载struts2需要的其他依赖包,可以看一下这里: ![](https://box.kancloud.cn/2016-09-06_57ce64fbb0294.png) 是不是都有了,有的时候Maven可能会报错,由于网络的原因(个人认为大多是网络问题,导致下载依赖包出错![难过](https://box.kancloud.cn/2016-01-18_569ca449b6adc.gif) ),可以从上面提到的两个网站手动下载 ### 3. 新建一个Action ~~~ package com.deppon.test02.action; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private static final long serialVersionUID = -1417237614181805435L; private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } /** * 跳转到登录界面 * @return */ public String login_input() { return SUCCESS; } /** * 登录 * @return */ public String login() { System.out.println("name->" + name); System.out.println("password->" + password); return SUCCESS; } } ~~~ struts.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"></constant> <constant name="struts.multipart.maxSize" value="20971520"/> <constant name="struts.devMode" value="true" /> <package name="p_user" namespace="/" extends="struts-default"> <action name="login_input" class="com.deppon.test02.action.UserAction" method="login_input"> <result name="success"> /login.jsp </result> </action> <action name="login" class="com.deppon.test02.action.UserAction" method="login"> <result name="success"> /login_success.jsp </result> </action> </package> </struts> ~~~ 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> <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> </web-app> ~~~ 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>主页</title> </head> <body> <a href="login_input">去登陆</a> </body> </html> ~~~ 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"> name:<input type="text" name="name" /> password:<input type="password" name="password" /> <input type="submit" value="登录" /> </form> </body> </html> ~~~ login_success.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>登录成功</title> </head> <body> <s:form action="login" namespace="/" method="post"> <s:textfield name="name" label="name"></s:textfield> <s:password name="password" label="password"></s:password> <s:submit value="Login"></s:submit> </s:form> </body> </html> ~~~ 项目结构如下图所示: ![](https://box.kancloud.cn/2016-09-06_57ce64fbcefb4.png) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - [Maven学习(一)- 环境搭建](http://blog.csdn.net/jolingogo/article/details/8775046) - [Maven学习(二)- 安装m2eclipse插件 ](http://blog.csdn.net/jolingogo/article/details/8796410) - [Maven学习(三)- 使用Maven构建Web项目](http://blog.csdn.net/jolingogo/article/details/8796726) - [Maven学习(四)- 使用Maven构建Web项目-测试](http://blog.csdn.net/jolingogo/article/details/8797153) - [Maven学习(五)- 使用Maven构建Struts2项目](http://blog.csdn.net/jolingogo/article/details/8798052) - [Maven学习(六)- 构建Hibernate项目](http://blog.csdn.net/jolingogo/article/details/8798684) - [Maven学习(七)- 构建Spring项目](http://blog.csdn.net/jolingogo/article/details/8799307) - [Maven学习(八)- 构建MyBatis项目](http://blog.csdn.net/jolingogo/article/details/8801158) - [Maven学习(九)- 构建SSH项目](http://blog.csdn.net/jolingogo/article/details/8811817) - [Maven学习(十) - 阶段小结 ](http://blog.csdn.net/jolingogo/article/details/8821375) - [专栏:Maven学习之旅](http://blog.csdn.net/column/details/yuguiyang-maven.html)