struts2 實現簡單的登錄實例


平台環境:win7、MyEclipse 10

1         struts2 框架搭建

在MyEclipse新建web project 取名為Struts2_Test,復制jar包到webroot/web-inf/lib.整體的結構如下

 

注意:不同版本的struts 所必需的包不同,可根據報錯加入缺少的包。

在project上右鍵->選擇myeclipse->add struts capabilities

 

選擇struts 2.1

 

點擊next,這兒取消MyEclipse Libraries,因為我們已經把包放上去了。如果沒有包也可以用MyEclipse自帶的包。

 

點擊finish.這時會在src下自動添加struts.xml文件。並且web.xml也會自動修改。

struts.xml如下:空的struts配置

1 <?xml version="1.0" encoding="UTF-8" ?>
2 
3 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
4 
5 <struts>
6 
7  
8 
9 </struts>   

web.xml 如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
 4 
 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6 
 7     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 8 
 9     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
10 
11     <display-name></display-name>
12 
13     <!-- 起始頁面 -->
14 
15     <welcome-file-list>
16 
17         <welcome-file>index.jsp</welcome-file>
18 
19     </welcome-file-list>
20 
21     <!-- 過濾器 用於初始化struts2 -->
22 
23     <filter>
24 
25         <filter-name>struts2</filter-name>
26 
27         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
28 
29     </filter>
30 
31     <!-- 用於struts2 的過濾器映射 -->
32 
33     <filter-mapping>
34 
35         <filter-name>struts2</filter-name>
36 
37         <url-pattern>/*</url-pattern>
38 
39     </filter-mapping>
40 
41 </web-app>

部署project:點擊工具欄上的部署按鈕

 選擇要部署的project點擊add

 

在Server下拉列表選擇MyEclipse Tomcat

 

 部署成功,無報錯。選中project點擊run

選擇run as ->MyEclipse Server Application運行后結果如下

 

到此struts2 框架搭建成功。可以進行下一步:struts2 登錄實例的實現。

2        struts2 登錄實例的實現

  • 在scr下創建名為org.struts.useraction的包(package) 再創建名為UserAction的類

 

UserAction.java的內容:

 1 package org.struts.useraction;
 2 
 3  
 4 
 5 import com.opensymphony.xwork2.ActionSupport;
 6 
 7  
 8 
 9 public class UserAction extends ActionSupport {
10 
11  
12 
13     private String username;
14 
15     private String password;
16 
17  
18 
19     public String getUsername() {
20 
21         return username;
22 
23     }
24 
25  
26 
27     public void setUsername(String username) {
28 
29         this.username = username;
30 
31     }
32 
33  
34 
35     public String getPassword() {
36 
37         return password;
38 
39     }
40 
41  
42 
43     public void setPassword(String password) {
44 
45         this.password = password;
46 
47     }
48 
49  
50 
51     public String execute() throws Exception {
52 
53         System.out.println("Login.action");
54 
55         if ("scott".equals(username) && "tiger".equals(password))//scott是用戶名,tiger是密碼
56 
57             return "success";
58 
59         else
60 
61             return "error";
62 
63     }
64 
65 }
  • 然后在struts.xml配置login.action 代碼如下:
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 
 3 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
 4 
 5 <struts>
 6 
 7     <!--struts2.0默認的配置文件 -->
 8 
 9     <include file="struts-default.xml"></include>
10 
11     <!-- 也可以加載其他的配置文件 -->
12 
13     <!-- <include file="mystrutsconfig.xml"></include> -->
14 
15     <!-- 添加package -->
16 
17     <package name="useraction" extends="struts-default">
18 
19         <!-- 配置login.action -->
20 
21         <action name="login" class="org.struts.useraction.UserAction">
22 
23             <result name="success">success.jsp</result>
24 
25             <result name="error">error.jsp</result>
26 
27         </action>
28 
29     </package>
30 
31 </struts>   
  • 修改index.jsp內容如下:
 1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=GBK"%>
 2 
 3 <%
 4 
 5 String path = request.getContextPath();
 6 
 7 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 8 
 9 %>
10 
11 <%@taglib uri="/struts-tags" prefix="s"%>
12 
13  
14 
15 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
16 
17 <html>
18 
19   <head>
20 
21     <base href="<%=basePath%>">
22 
23    
24 
25     <title>登陸界面</title>
26 
27     <meta http-equiv="pragma" content="no-cache">
28 
29     <meta http-equiv="cache-control" content="no-cache">
30 
31     <meta http-equiv="expires" content="0">   
32 
33     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
34 
35     <meta http-equiv="description" content="This is my page">
36 
37     <!--
38 
39     <link rel="stylesheet" type="text/css" href="styles.css">
40 
41     -->
42 
43   </head>
44 
45  
46 
47   <body>
48 
49     <!-- 提交請求參數的表單 -->
50 
51     <form action="login" method="post">
52 
53         <table align="center">
54 
55             <caption>
56 
57                 <h3>用戶登錄</h3>
58 
59             </caption>
60 
61             <tr>
62 
63                 <!-- 用戶名的表單域 -->
64 
65                 <td>用戶名:<input type="text" name="username" /></td>
66 
67             </tr>
68 
69             <tr>
70 
71                 <!-- 密碼的表單域 -->
72 
73                 <td>&nbsp;&nbsp;碼:<input type="password" name="password" /></td>
74 
75             </tr>
76 
77             <tr align="center">
78 
79                 <td colspan="2"><input type="submit" value="登錄"/></td>
80 
81             </tr>
82 
83         </table>
84 
85     </form>
86 
87   </body>
88 
89 </html>

 

  • 在webroot下創建success.jsp 和error.jsp。 success.jsp 內容如下:
 1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
 2 
 3 <%
 4 
 5 String path = request.getContextPath();
 6 
 7 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 8 
 9 %>
10 
11  
12 
13 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
14 
15 <html>
16 
17   <head>
18 
19     <base href="<%=basePath%>">
20 
21    
22 
23     <title>My JSP 'success.jsp' starting page</title>
24 
25  
26 
27   </head>
28 
29  
30 
31   <body>
32 
33     This is success page. <br>
34 
35   </body>
36 
37 </html>

 

error.jsp 類似。

最后是部署運行,結果如下:

 

輸入正確用戶名(scott)和密碼(tiger)登錄成功!

 

輸入錯誤的則會顯示登錄失敗

 

這樣登錄實例就完成了~~~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM