第一個Struts2項目-登陸Struts

內容提要:本文經過「用戶登陸」這個簡單功能,介紹struts2的標誌(Tag)、Action、輸入校驗(Input Validation)以及本地化輸出(Localizing Output)。html

開發環境:J2EE+jdk7+tomcat7.0+struts2+junit3.8java

項目目錄結構:web

 

項目工做流程:apache

1. 顯示登陸頁面等待用戶輸入tomcat

2. 用戶輸入「用戶名」和「密碼」點擊「登陸」app

3.Action類中用戶校驗獲得了執行,若是用戶在name/password字段輸入admin/admin,jsp

   那麼將會顯示成功頁面.不然頁面顯示錯誤信息.ide

 

開發步驟:ui

1.建立登錄頁面(index.jsp)this

代碼實現:

 1  <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 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=GB18030">
 8 <title>Struts2登陸界面</title>
 9 </head>
10 <body>
11  <s:form action="LoginAction" namespace="/">
12  <tr>  
13 <td colspan="2" align="center">  Login  </td>  
14  </tr>  
15  <tr>  
16    <td colspan="2">  
17          <s:actionerror />  
18          <s:fielderror />  
19    </td>  
20   </tr>  
21      <s:password name="name" label="用戶名"></s:password>
22       <s:textfield name="password" label="密碼"></s:textfield>
23       <s:submit  name="submit"  value="登陸" ></s:submit> 
24 </s:form>
25 </body>
26 </html>
View Code

 登錄成功頁面(main.jsp)

代碼實現:

 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html> 
 5 <head>
 6 <title>Login Success</title>
 7 </head>
 8 <body>
 9 <p align="center"><font color="#000080" size="5">Login Successful</font></p>
10 </body>
11 </html>
View Code

其中代碼 <s:actionerror />和<s:fielderror />是Struts2標籤庫裏的通用標籤。用來顯示Action和字段校驗的錯誤,先不用管。

2.配置web.xml

代碼實現:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>Struts</display-name>
 4   
 5   
 6   <filter>
 7         <filter-name>struts2</filter-name>
 8         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 9  </filter>
10  <filter-mapping>
11         <filter-name>struts2</filter-name>
12         <url-pattern>/*</url-pattern>
13  </filter-mapping>
14     
15  <welcome-file-list>
16     <welcome-file>index.jsp</welcome-file>
17    
18   </welcome-file-list>
19 </web-app>
View Code

 

3.建立LoginAction類

如今讓咱們建立LoginAction類來處理登陸請求.Struts2中不必定非要實現Action接口,任何含有execute()方法的POJO均可以看成Action。

使用.Struts2提供了一個基礎的ActionSupport類來實現經常使用的接口.在咱們的Action類中(LoginAction.java)咱們實現了ActionSupport接口.

咱們的"LoginAction.java"存放在「package com.lizanhong.action」 

代碼實現:

 1 package com.lizanhong.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class LoginAction extends ActionSupport {
 6     private static final long serialVersionUID = 1L;
 7     
 8     private String name;
 9     private String password;
10     
11     public String getName() {
12         return name;
13     }
14     public void setName(String name) {
15         this.name = name;
16     }
17     public String getPassword() {
18         return password;
19     }
20     public void setPassword(String password) {
21         this.password = password;
22     }
23     
24     
25     public String execute(){
26         System.out.println("Validating login");
27 //        獲取表單中的姓名
28         String name = this.getName();
29 //        獲取表單中的密碼
30         String password = this.getPassword();
31 //        判斷用戶名及密碼是否正確
32         if(!name.equals("admin") || !password.equals("admin")){  
33             addActionError("Invalid user name or password! Please try again!");  
34             return ERROR;  
35     }else{  
36       return SUCCESS;  
37     }  
38     }
39     
40 }
View Code

 

4.配置LoginAction映射(在struts.xml)

代碼實現:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <package name="default" namespace="/" extends="struts-default">
 7         <action name="LoginAction" class="com.lizanhong.action.LoginAction">
 8             <!-- 定義邏輯視圖和物理資源之間的映射 -->
 9             <result name="error">index.jsp</result>
10             <result >/WEB-INF/main.jsp</result>
11         </action>
12     </package>
13 </struts>
View Code

 

  這個項目是本身親自實踐的,有問題歡迎你們討論。下一篇博文我會添加校驗功能。

相關文章
相關標籤/搜索