2、使用Struts2实现登录功能(没有连接数据)

1.创建如下的目录结构

2.在com.entity包下创建

 1 package com.entity;
 2 /**
 3  * 用户类
 4  * @author Holly老师
 5  *
 6  */
 7 public class User {
 8     private String username; //用户名
 9     private String password; //密码
10     public String getUsername() {
11         return username;
12     }
13     public void setUsername(String username) {
14         this.username = username;
15     }
16     public String getPassword() {
17         return password;
18     }
19     public void setPassword(String password) {
20         this.password = password;
21     }
22     
23 
24 }
User.java

3.在com.action包下创建LoginAction.java

 1 package com.action;
 2 
 3 import com.entity.User;
 4 import com.opensymphony.xwork2.Action;
 5 /**
 6  * 登陆控制类
 7  * @author Holly老师
 8  *
 9  */
10 public class LoginAction implements Action {
11     /**
12      * action注入实体类对象
13      * 该对象名和表单里的name属性值的前缀保持一致
14      * 否则注入不了
15      */
16     private User user; 
17     public User getUser() {
18         return user;
19     }
20     public void setUser(User user) {
21         this.user = user;
22     }
23     
24     public String execute() throws Exception {
25         if(user.getUsername().equals("holly") && user.getPassword().equals("123")){
26             return "success";
27         }else{
28             return "error";
29         }
30         
31     }
32     
33 //    private String user;
34 //    private String pwd;
35 //    public String getUser() {
36 //        return user;
37 //    }
38 //
39 //    public void setUser(String user) {
40 //        this.user = user;
41 //    }
42 //
43 //    public String getPwd() {
44 //        return pwd;
45 //    }
46 //
47 //    public void setPwd(String pwd) {
48 //        this.pwd = pwd;
49 //    }
50 //    public String execute() throws Exception {
51 //        if(user.equals("holly") && pwd.equals("123")){
52 //            return "success";
53 //        }else{
54 //            return "error";
55 //        }
56 //        
57 //    }
58     
59 
60     
61 
62 }
LoginAction.java

4.在src跟目录下创建struts.xml文件

 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     <package name="default" namespace="/" extends="struts-default">
 7         <action name="login" class="com.action.LoginAction">
 8             <result name="success">success.jsp</result>
 9             <result name="error">fail.jsp</result>
10         </action>
11     </package>
12 </struts>
struts.xml

5.在WebRoot下编辑index.jsp页面

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@taglib uri="/struts-tags" prefix="s"%>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10     <head>
11         <base href="<%=basePath%>">
12 
13         <title>My JSP 'index.jsp' starting page</title>
14         <meta http-equiv="pragma" content="no-cache">
15         <meta http-equiv="cache-control" content="no-cache">
16         <meta http-equiv="expires" content="0">
17         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18         <meta http-equiv="description" content="This is my page">
19         <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22     </head>
23 
24     <body>
25 
26         <form action="login.action" method="post">
27             <table>
28                 <tr>
29 
30                     <td>
31                         用户名:
32                     </td>
33                     <td>
34                         <input type="text" name="user.username" />
35                     </td>
36                 </tr>
37                 <tr>
38 
39                     <td>
40                         密码:
41                     </td>
42                     <td>
43                         <input type="password" name="user.password" />
44                     </td>
45                 </tr>
46                 <tr>
47                     <td colspan="2">
48                         <input type="submit" value="提交" />
49                     </td>
50                 </tr>
51             </table>
52         </form>
53     </body>
54 </html>
index.jsp

 

6.在WebRoot下创建success.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@taglib uri="/struts-tags" prefix="s" %>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP 'index.jsp' starting page</title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23   
24   <body>
25     <h1>登录成功</h1>
26      欢迎您,<s:property value="user.username"/>
27   </body>
28 </html>
success.jsp

7.在webRoot下创建fail.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@taglib uri="/struts-tags" prefix="s" %>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP 'index.jsp' starting page</title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23   
24   <body>
25      <h1>登录失败</h1>
26      用户名或密码错误!
27   </body>
28 </html>
fail.jsp

 

posted @ 2016-05-17 17:48  红酒人生  阅读(480)  评论(1)    收藏  举报