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>  
項目結構如下圖所示:


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章