首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >struts2开发_userlogin_模拟用户登录

struts2开发_userlogin_模拟用户登录

作者头像
Hongten
发布2018-09-13 16:53:14
7430
发布2018-09-13 16:53:14
举报
文章被收录于专栏:HongtenHongten

项目结构:

http://www.cnblogs.com/hongten/gallery/image/112981.html

登录界面:

http://www.cnblogs.com/hongten/gallery/image/112983.html

登录失败:

http://www.cnblogs.com/hongten/gallery/image/112982.html

登录成功:

http://www.cnblogs.com/hongten/gallery/image/112984.html

/struts2_0200_userlogin/src/com/b510/entity/User.java

User中多余的字段,是为了为以后项目中所创建的……

  1 /**
  2  * 
  3  */
  4 package com.b510.entity;
  5 
  6 /**
  7  *User实体类
  8  * 
  9  * @author hongten</br>
 10  * @date 2012-3-17</br>
 11  * 
 12  */
 13 public class User {
 14 
 15     /**
 16      * id号
 17 */
 18     private int id;
 19     /**
 20      * 姓名
 21 */
 22     private String name;
 23 
 24     /**
 25      * 密码
 26 */
 27     private String password;
 28     /**
 29      * 性别
 30 */
 31     private String sex;
 32     /**
 33      * 邮箱
 34 */
 35     private String mail;
 36 
 37     public User() {
 38     }
 39 
 40     /**
 41      * @param name
 42      * @param password
 43 */
 44     public User(String name, String password) {
 45         super();
 46         this.name = name;
 47         this.password = password;
 48     }
 49 
 50     /**
 51      * @return the id
 52 */
 53     public int getId() {
 54         return id;
 55     }
 56 
 57     /**
 58      * @return the mail
 59 */
 60     public String getMail() {
 61         return mail;
 62     }
 63 
 64     /**
 65      * @return the name
 66 */
 67     public String getName() {
 68         return name;
 69     }
 70 
 71     /**
 72      * @return the password
 73 */
 74     public String getPassword() {
 75         return password;
 76     }
 77 
 78     /**
 79      * @return the sex
 80 */
 81     public String getSex() {
 82         return sex;
 83     }
 84 
 85     /**
 86      * @param id
 87      *            the id to set
 88 */
 89     public void setId(int id) {
 90         this.id = id;
 91     }
 92 
 93     /**
 94      * @param mail
 95      *            the mail to set
 96 */
 97     public void setMail(String mail) {
 98         this.mail = mail;
 99     }
100 
101     /**
102      * @param name
103      *            the name to set
104 */
105     public void setName(String name) {
106         this.name = name;
107     }
108 
109     /**
110      * @param password
111      *            the password to set
112 */
113     public void setPassword(String password) {
114         this.password = password;
115     }
116 
117     /**
118      * @param sex
119      *            the sex to set
120 */
121     public void setSex(String sex) {
122         this.sex = sex;
123     }
124 }

/struts2_0200_userlogin/src/com/b510/user/action/UserAction.java

 1 /**
 2  * 
 3  */
 4 package com.b510.user.action;
 5 
 6 import com.b510.entity.User;
 7 import com.b510.user.service.UserService;
 8 import com.b510.user.service.impl.UserServiceBean;
 9 import com.opensymphony.xwork2.ActionContext;
10 import com.opensymphony.xwork2.ActionSupport;
11 
12 /**
13  * 
14  * @author hongten</br>
15  * @date 2012-3-17</br>
16  * 
17  */
18 public class UserAction extends ActionSupport {
19 
20     /**
21      * id号
22 */
23     private static final long serialVersionUID = -4186853910477035740L;
24 
25     /**
26      * UserService实例
27 */
28     private UserService userService=new UserServiceBean();
29     /**
30      * 用户User的一个实例
31 */
32     private User user;
33 
34     public String execute() throws Exception {
35         ActionContext act=ActionContext.getContext();
36         boolean falg=getUserService()
37         .login(getUser().getName(), getUser().getPassword());
38         if (falg) {
39             act.put("congratulation", getUser().getName());
40             return SUCCESS;
41         } else {
42             return "FAIL";
43         }
44     }
45 
46     /**
47      * @return the user
48 */
49     public User getUser() {
50         return user;
51     }
52 
53     /**
54      * @return the userService
55 */
56     public UserService getUserService() {
57         return userService;
58     }
59 
60     /**
61      * @param user
62      *            the user to set
63 */
64     public void setUser(User user) {
65         this.user = user;
66     }
67 
68     /**
69      * @param userService
70      *            the userService to set
71 */
72     public void setUserService(UserServiceBean userServiceBean) {
73         this.userService = userServiceBean;
74     }
75 
76 }

/struts2_0200_userlogin/src/com/b510/user/dao/UserDAO.java

 1 /**
 2  * 
 3  */
 4 package com.b510.user.dao;
 5 
 6 import java.util.List;
 7 
 8 import com.b510.entity.User;
 9 
10 /**
11  * 
12  * @author hongten</br>
13  * @date 2012-3-17</br>
14  * 
15  */
16 public interface UserDAO {
17 
18     /**
19      * 加载User实例
20      * 
21      * @param id
22      *            需要加载的User实例的标识属性值
23      * @return 指定id对应的User实例
24 */
25     public abstract User get(int id);
26 
27     /**
28      * 保存User实例
29      * 
30      * @param user
31      *            需要保存的User实例
32      * @return 刚刚保存的User实例的标识属性值
33 */
34     public abstract Integer save(User user);
35 
36     /**
37      * 更改User实例
38      * 
39      * @param user
40      *            需要更改的User实例
41 */
42     public abstract void update(User user);
43 
44     /**
45      * 删除User实例
46      * 
47      * @param id
48      *            需要删除的User实例的标识属性值
49 */
50     public abstract void delete(int id);
51 
52     /**
53      * 删除User实例
54      * 
55      * @param user
56      *            需要删除的User实例
57 */
58     public abstract void delete(User user);
59 
60     /**
61      * 根据姓名查找User
62      * 
63      * @param name
64      *            查询的人名
65      * @return 指定用户对应的全部User
66 */
67     public abstract List findByName(String name);
68 
69     /**
70      * 查询全部User实例
71      * 
72      * @return 全部User实例
73 */
74     public abstract List findAllUser();
75 
76     /**
77      * 根据姓名,密码查找User
78      * 
79      * @param name
80      *            姓名
81      * @param password
82      *            密码
83      * @return 指定姓名和密码存在,返回true,否则,返回false
84 */
85     public abstract boolean findByNameAndPassword(String name, String password);
86 
87 }

/struts2_0200_userlogin/src/com/b510/user/dao/impl/UserDAOImpl.java

项目中只是实现了一个方法findByNameAndPassword(),其余方法,会再后面的项目中实现……

 1 /**
 2  * 
 3  */
 4 package com.b510.user.dao.impl;
 5 
 6 import java.util.List;
 7 
 8 import com.b510.entity.User;
 9 import com.b510.user.dao.UserDAO;
10 import com.sun.org.apache.bcel.internal.generic.NEW;
11 
12 /**
13  *UserDAO的实现类
14  * 
15  * @author hongten</br>
16  * @date 2012-3-17</br>
17  * 
18  */
19 public class UserDAOImpl implements UserDAO {
20 
21     public void delete(int id) {
22 
23     }
24 
25     public void delete(User user) {
26 
27     }
28 
29     public List findAllUser() {
30         List list = null;
31         return list;
32     }
33 
34     public List findByName(String name) {
35         List list = null;
36         return list;
37     }
38 
39     public boolean findByNameAndPassword(String name, String password) {
40         System.out.println(name);
41         if (name.equals("hongten")&&password.equals("510")) {
42             return true;
43         } else {
44             return false;
45         }
46     }
47 
48     public User get(int id) {
49         User user = null;
50         return user;
51     }
52 
53     public Integer save(User user) {
54         return Integer.valueOf(0);
55     }
56 
57     public void update(User user) {
58 
59     }
60 }

/struts2_0200_userlogin/src/com/b510/user/service/UserService.java

 1 /**
 2  * 
 3  */
 4 package com.b510.user.service;
 5 
 6 /**
 7  * 
 8  * @author hongten</br>
 9  * @date 2012-3-17</br>
10  * 
11  */
12 public interface UserService {
13 
14     /**
15      * 根据姓名,密码进行登录
16      * 
17      * @param name
18      *            姓名
19      * @param password
20      *            密码
21      * @return 如果存在姓名和密码,返回true,否则,返回false
22 */
23     public abstract boolean login(String name, String password);
24 }

/struts2_0200_userlogin/src/com/b510/user/service/impl/UserServiceBean.java

在项目中可以不用getter,setter方法,在以后通spring整合的时候,我们就可以用到这个方法……

 1 /**
 2  * 
 3  */
 4 package com.b510.user.service.impl;
 5 
 6 import java.util.List;
 7 
 8 import com.b510.user.dao.UserDAO;
 9 import com.b510.user.dao.impl.UserDAOImpl;
10 import com.b510.user.service.UserService;
11 
12 /**
13  * 
14  * @author hongten</br>
15  * @date 2012-3-17</br>
16  * 
17  */
18 public class UserServiceBean implements UserService {
19 
20     private UserDAO userDAO = new UserDAOImpl();
21 
22     public boolean login(String name, String password) {
23         return getUserDAO().findByNameAndPassword(name, password);
24     }
25 
26     /**
27      * @return the userDAO
28 */
29     public UserDAO getUserDAO() {
30         return userDAO;
31     }
32 
33     /**
34      * @param userDAO
35      *            the userDAO to set
36 */
37     public void setUserDAO(UserDAOImpl userDAO) {
38         this.userDAO = userDAO;
39     }
40 }

/struts2_0200_userlogin/src/com/b510/utils/CharacterEncodingFilter.java

 1 /**
 2  * 
 3  */
 4 package com.b510.utils;
 5 
 6 import java.io.IOException;
 7 
 8 import javax.servlet.Filter;
 9 import javax.servlet.FilterChain;
10 import javax.servlet.FilterConfig;
11 import javax.servlet.ServletException;
12 import javax.servlet.ServletRequest;
13 import javax.servlet.ServletResponse;
14 
15 /**
16  * 自定义Filter,过滤所有请求的编码,全部转换为UTF-8
17  * 
18  * @author hongten</br>
19  * @date 2012-3-17</br>
20  * 
21  */
22 public class CharacterEncodingFilter implements Filter {
23 
24     public void destroy() {
25     }
26 
27     /**
28      * 把request请求的内容转换为"UTF-8"
29 */
30     public void doFilter(ServletRequest request, ServletResponse response,
31             FilterChain chain) throws IOException, ServletException {
32         request.setCharacterEncoding("utf-8");
33         chain.doFilter(request, response);
34     }
35 
36     public void init(FilterConfig arg0) throws ServletException {
37     }
38 
39 }

/struts2_0200_userlogin/src/mess.properties

1 name=\u7528\u6237\u540D
2 password=\u5BC6  \u7801
3 failUI=\u767B\u5F55\u5931\u8D25\u9875\u9762
4 successUI=\u767B\u5F55\u6210\u529F\u9875\u9762
5 loginUI=\u767B\u5F55\u9875\u9762
6 login=\u767B\u5F55
7 reset=\u91CD\u7F6E
8 fail_message=\u4F60\u7684\u7528\u6237\u540D\u6216\u8005\u5BC6\u7801\u9519\u8BEF\uFF0C\u8BF7\u91CD\u65B0
9 congratulation=\u606D\u559C\uFF0C\u4F60\u6210\u529F\u767B\u5F55\u5566\uFF1A

/struts2_0200_userlogin/src/struts-login.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
 4     "http://struts.apache.org/dtds/struts-2.1.7.dtd">
 5     <!-- 指定Struts 2配置文件的根元素 -->
 6 <struts>
 7     <!-- 所有的Action定义都应该放在package下 -->
 8     <package name="hr" extends="struts-default">
 9         <action name="*">
10             <result>/index.jsp</result>
11         </action>
12     </package>
13     <!--所有的Action定义都应该放在package下 -->
14     <package name="loginui" namespace="/loginUI" extends="struts-default">
15         <action name="login" class="com.b510.user.action.UserAction">
16             <result>/WEB-INF/loginUI/login_successUI.jsp</result>
17             <result name="FAIL">/WEB-INF/loginUI/login_failUI.jsp</result>
18         </action>
19     </package>
20 </struts>

/struts2_0200_userlogin/src/struts.xml

 1 <?xml version="1.0" encoding="GBK"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
 4     "http://struts.apache.org/dtds/struts-2.1.7.dtd">
 5     <!-- 指定Struts 2配置文件的根元素 -->
 6 <struts>
 7     <!-- 指定全局国际化资源文件 -->
 8     <constant name="struts.custom.i18n.resources" value="mess" />
 9     <!-- 指定国际化编码所使用的字符集 -->
10     <constant name="struts.i18n.encoding" value="GBK" />
11     <!-- 设置为开发模式 -->
12     <constant name="struts.devMode" value="true" />
13     <include file="struts-login.xml"></include>
14 </struts>

/struts2_0200_userlogin/WebRoot/WEB-INF/web.xml

 1 <?xml version="1.0" encoding="GBK"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 5     id="WebApp_ID" version="3.0">
 6 
 7     <!-- 定义Struts2的核心Filter -->
 8     <filter>
 9         <filter-name>struts2</filter-name>
10         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
11     </filter>
12     <!-- 让Struts2的核心Filter拦截所有请求 -->
13     <filter-mapping>
14         <filter-name>struts2</filter-name>
15         <url-pattern>/*</url-pattern>
16     </filter-mapping>
17     <!-- 过滤的URL为“/*”,表示当前的request请求 -->
18     <filter>
19         <filter-name>characterEncoding</filter-name>
20         <filter-class>com.b510.utils.CharacterEncodingFilter</filter-class>
21     </filter>
22     <filter-mapping>
23         <filter-name>characterEncoding</filter-name>
24         <url-pattern>/*</url-pattern>
25     </filter-mapping>
26 </web-app>

/struts2_0200_userlogin/WebRoot/index.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme() + "://"
 5 + request.getServerName() + ":" + request.getServerPort()
 6 + path + "/";
 7 %>
 8 <%@ taglib uri="/struts-tags" prefix="s"%>
 9 
10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
11 <html>
12     <head>
13         <base href="<%=basePath%>">
14 
15         <title><s:text name="loginUI"></s:text></title>
16         <meta http-equiv="pragma" content="no-cache">
17         <meta http-equiv="cache-control" content="no-cache">
18         <meta http-equiv="expires" content="0">
19         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
20         <meta http-equiv="description" content="This is my page">
21         <!--
22     <link rel="stylesheet" type="text/css" href="styles.css">
23 -->
24     </head>
25 
26     <body>
27         <s:label value="用户登录:"></s:label>
28         <br />
29         <s:form action="loginUI/login" method="post">
30             <s:textfield name="user.name" key="name"/>
31             <br />
32             <s:password name="user.password" key="password" />
33             <br />
34             <s:submit key="login"></s:submit>
35             <s:reset key="reset"></s:reset>
36         </s:form>
37         <br>
38     </body>
39 </html>

/struts2_0200_userlogin/WebRoot/WEB-INF/loginUI/login_successUI.jsp

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

/struts2_0200_userlogin/WebRoot/WEB-INF/loginUI/login_failUI.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme() + "://"
 5 + request.getServerName() + ":" + request.getServerPort()
 6 + path + "/";
 7 %>
 8 <%@ taglib uri="/struts-tags" prefix="s"%>
 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10 <html>
11     <head>
12         <base href="<%=basePath%>">
13 
14         <title><s:text name="failUI"></s:text>
15         </title>
16 
17         <meta http-equiv="pragma" content="no-cache">
18         <meta http-equiv="cache-control" content="no-cache">
19         <meta http-equiv="expires" content="0">
20         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
21         <meta http-equiv="description" content="This is my page">
22         <!--
23     <link rel="stylesheet" type="text/css" href="styles.css">
24 -->
25 
26     </head>
27 
28     <body>
29         <s:text name="fail_message"></s:text>
30         <s:a href="ui">
31             <s:text name="login" />
32         </s:a>
33         <br>
34     </body>
35 </html>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-03-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档