使用Maven构建Struts2项目

来源:互联网 发布:网络打鸡血是什么意思 编辑:程序博客网 时间:2024/05/18 04:01

1. 新建一个基本的Web项目

这和前面讲的是一样的,可以参考前面的博客

2. 添加Struts2依赖

这里主需要在pom.xml中添加一个struts-core的依赖即可:

[html] view plaincopy
  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/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.deppon.demo</groupId>
  5. <artifactId>test02</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>test02 Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <!-- 属性配置 -->
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. </properties>
  14. <dependencies>
  15. <!-- 添加JUnit -->
  16. <dependency>
  17. <groupId>junit</groupId>
  18. <artifactId>junit</artifactId>
  19. <version>3.8.1</version>
  20. <scope>test</scope>
  21. </dependency>
  22. <!-- Struts2 依赖 -->
  23. <dependency>
  24. <groupId>org.apache.struts</groupId>
  25. <artifactId>struts2-core</artifactId>
  26. <version>2.3.1</version>
  27. </dependency>
  28. </dependencies>
  29. <build>
  30. <finalName>test02</finalName>
  31. </build>
  32. </project>

之后,Maven会自动从网上下载struts2需要的其他依赖包,可以看一下这里:

是不是都有了,有的时候Maven可能会报错,由于网络的原因(个人认为大多是网络问题,导致下载依赖包出错难过),可以从上面提到的两个网站手动下载

3. 新建一个Action

[java] view plaincopy
  1. package com.deppon.test02.action;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class UserAction extends ActionSupport {
  4. private static final long serialVersionUID = -1417237614181805435L;
  5. private String name;
  6. private String password;
  7. public String getName() {
  8. return name;
  9. }
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13. public String getPassword() {
  14. return password;
  15. }
  16. public void setPassword(String password) {
  17. this.password = password;
  18. }
  19. /**
  20. * 跳转到登录界面
  21. * @return
  22. */
  23. public String login_input() {
  24. return SUCCESS;
  25. }
  26. /**
  27. * 登录
  28. * @return
  29. */
  30. public String login() {
  31. System.out.println("name->" + name);
  32. System.out.println("password->" + password);
  33. return SUCCESS;
  34. }
  35. }

struts.xml
[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <constant name="struts.i18n.encoding" value="utf-8"></constant>
  7. <constant name="struts.multipart.maxSize" value="20971520"/>
  8. <constant name="struts.devMode" value="true" />
  9. <package name="p_user" namespace="/" extends="struts-default">
  10. <action name="login_input" class="com.deppon.test02.action.UserAction"method="login_input">
  11. <result name="success">
  12. /login.jsp
  13. </result>
  14. </action>
  15. <action name="login" class="com.deppon.test02.action.UserAction"method="login">
  16. <result name="success">
  17. /login_success.jsp
  18. </result>
  19. </action>
  20. </package>
  21. </struts>

web.xml
[html] view plaincopy
  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <filter>
  6. <filter-name>struts2</filter-name>
  7. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>struts2</filter-name>
  11. <url-pattern>/*</url-pattern>
  12. </filter-mapping>
  13. </web-app>

index.jsp

[java] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>主页</title>
  8. </head>
  9. <body>
  10. <a href="login_input">去登陆</a>
  11. </body>
  12. </html>

login.jsp
[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>登录界面</title>
  8. </head>
  9. <body>
  10. <form action="login" method="post">
  11. name:<input type="text" name="name" />
  12. password:<input type="password" name="password" />
  13. <input type="submit" value="登录" />
  14. </form>
  15. </body>
  16. </html>

login_success.jsp
[java] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib prefix="s" uri="/struts-tags" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>登录成功</title>
  9. </head>
  10. <body>
  11. <s:form action="login" namespace="/" method="post">
  12. <s:textfield name="name" label="name"></s:textfield>
  13. <s:password name="password" label="password"></s:password>
  14. <s:submit value="Login"></s:submit>
  15. </s:form>
  16. </body>
  17. </html>
项目结构如下图所示:


0 0
原创粉丝点击