zoukankan      html  css  js  c++  java
  • Maven的学习资料收集--(五)使用Maven构建Struts2项目

    在前两篇博客中,使用Maven构建了Web项目,在这篇博客中写一下,怎样构建一个简单的Struts2项目。

    在准备过程中发现,要使用好Maven,个人觉得要好好利用这两个网站:

    http://mvnrepository.com/

    http://search.maven.org/

    由于自己对Maven的理解不是非常深,所以学习的时候遇到很多简单的问题都没法解决委屈,走了很多弯路

    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.   <!-- 属性配置 -->  
    12.   <properties>  
    13.       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    14.   </properties>  
    15.     
    16.   <dependencies>    
    17.       
    18.     <!-- 添加JUnit -->  
    19.     <dependency>  
    20.       <groupId>junit</groupId>  
    21.       <artifactId>junit</artifactId>  
    22.       <version>3.8.1</version>  
    23.       <scope>test</scope>  
    24.     </dependency>  
    25.       
    26.     <!-- Struts2 依赖 -->  
    27.     <dependency>  
    28.         <groupId>org.apache.struts</groupId>  
    29.         <artifactId>struts2-core</artifactId>  
    30.         <version>2.3.1</version>  
    31.     </dependency>  
    32.       
    33.   </dependencies>  
    34.   <build>  
    35.     <finalName>test02</finalName>  
    36.   </build>  
    37. </project>  

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

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

    3. 新建一个Action

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

    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.   
    6. <struts>  
    7.     <constant name="struts.i18n.encoding" value="utf-8"></constant>  
    8.     <constant name="struts.multipart.maxSize" value="20971520"/>  
    9.     <constant name="struts.devMode" value="true" />  
    10.       
    11.     <package name="p_user" namespace="/" extends="struts-default">  
    12.         <action name="login_input" class="com.deppon.test02.action.UserAction" method="login_input">  
    13.             <result name="success">  
    14.                 /login.jsp  
    15.             </result>  
    16.         </action>  
    17.           
    18.         <action name="login" class="com.deppon.test02.action.UserAction" method="login">  
    19.             <result name="success">  
    20.                 /login_success.jsp  
    21.             </result>  
    22.         </action>  
    23.     </package>  
    24.       
    25. </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.   
    5. <web-app>  
    6.   
    7.     <filter>  
    8.         <filter-name>struts2</filter-name>  
    9.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
    10.     </filter>  
    11.       
    12.     <filter-mapping>  
    13.         <filter-name>struts2</filter-name>  
    14.         <url-pattern>/*</url-pattern>  
    15.     </filter-mapping>  
    16.    
    17. </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.       
    10.     <body>  
    11.         <a href="login_input">去登陆</a>  
    12.     </body>  
    13. </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.       
    10.     <body>  
    11.         <form action="login" method="post">  
    12.             name:<input type="text" name="name" />  
    13.             password:<input type="password" name="password" />  
    14.               
    15.             <input type="submit" value="登录" />  
    16.         </form>  
    17.     </body>  
    18. </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.       
    11.     <body>  
    12.         <s:form action="login" namespace="/" method="post">  
    13.             <s:textfield name="name" label="name"></s:textfield>  
    14.             <s:password name="password" label="password"></s:password>  
    15.               
    16.             <s:submit value="Login"></s:submit>  
    17.         </s:form>  
    18.     </body>  
    19. </html>  
    项目结构如下图所示:

  • 相关阅读:
    php环境配置中各个模块在网站建设中的功能
    PHP+Apache+MySQL+phpMyAdmin在win7系统下的环境配置
    August 17th 2017 Week 33rd Thursday
    August 16th 2017 Week 33rd Wednesday
    August 15th 2017 Week 33rd Tuesday
    August 14th 2017 Week 33rd Monday
    August 13th 2017 Week 33rd Sunday
    August 12th 2017 Week 32nd Saturday
    August 11th 2017 Week 32nd Friday
    August 10th 2017 Week 32nd Thursday
  • 原文地址:https://www.cnblogs.com/gxbk629/p/3891364.html
Copyright © 2011-2022 走看看