1. 程式人生 > >專案程式碼3

專案程式碼3

一.使用者登陸(校驗驗證碼:錯誤的驗證碼)

 1 package com.itheima.bos.web.action;
 2 
 3 import org.apache.commons.lang3.StringUtils;
 4 import org.apache.struts2.ServletActionContext;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.context.annotation.Scope;
 7 import org.springframework.stereotype.Controller;
8 9 import com.itheima.bos.domain.User; 10 import com.itheima.bos.service.IUserService; 11 import com.itheima.bos.web.action.base.BaseAction; 12 13 @Controller 14 @Scope("prototype") 15 public class UserAction extends BaseAction<User>{ 16 //屬性驅動,接受頁面輸入的驗證碼 17 private String checkcode;
18 public void setCheckcode(String checkcode) { 19 this.checkcode = checkcode; 20 } 21 22 @Autowired 23 private IUserService userService; 24 /* 25 * 使用者登入 26 */ 27 public String login() { 28 //從Session中獲取生成的驗證碼 29 String validatecode = (String) ServletActionContext.getRequest().getAttribute("key");
30 //教研驗證碼 31 if(StringUtils.isNoneBlank(checkcode) && checkcode.equals(validatecode)) { 32 //輸入的驗證碼正確 33 User user = userService.login(model); 34 if(user != null) { 35 //登陸成功,將user物件存放入session 36 37 } 38 }else { 39 //輸入的驗證碼錯誤,設定提示資訊,跳轉到登陸頁面 40 this.addActionError("輸入的驗證碼錯誤"); 41 return LOGIN; 42 } 43 return ""; 44 } 45 }

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     <constant name="struts.devMode" value="false" />
 7     <!-- 
 8         <constant name="struts.objectFactory" value="spring"/>
 9      -->
10     <package name="basicstruts2" extends="struts-default">
11         <!-- 需要進行許可權控制的頁面訪問 -->
12         <action name="page_*_*">
13             <result type="dispatcher">/WEB-INF/pages/{1}/{2}.jsp</result>
14         </action>
15         <!-- 使用者管理 -->
16         <action name="userAction_*" class="userAction" method="{1}">
17             <result name="login">/login.jsp</result>
18         </action>
19     </package>
20 </struts>

login.jsp頁面

  1 <%@ page language="java" contentType="text/html; charset=UTF-8"
  2     pageEncoding="UTF-8"%>
  3 <%@ taglib uri="/struts-tags" prefix="s" %>
  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 <script src="${pageContext.request.contextPath }/js/jquery-1.8.3.js" type="text/javascript"></script>
 10 <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/css/style.css" />
 11 <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath }/css/style_grey.css" />
 12 <style>
 13 input[type=text] {
 14     width: 80%;
 15     height: 25px;
 16     font-size: 12pt;
 17     font-weight: bold;
 18     margin-left: 45px;
 19     padding: 3px;
 20     border-width: 0;
 21 }
 22 
 23 input[type=password] {
 24     width: 80%;
 25     height: 25px;
 26     font-size: 12pt;
 27     font-weight: bold;
 28     margin-left: 45px;
 29     padding: 3px;
 30     border-width: 0;
 31 }
 32 
 33 #loginform\:codeInput {
 34     margin-left: 1px;
 35     margin-top: 1px;
 36 }
 37 
 38 #loginform\:vCode {
 39     margin: 0px 0 0 60px;
 40     height: 34px;
 41 }
 42 </style>
 43 <script type="text/javascript">
 44     if(window.self != window.top){
 45         window.top.location = window.location;
 46     }
 47 </script>
 48 </head>
 49 <body>
 50     <div
 51         style="width: 900px; height: 50px; position: absolute; text-align: left; left: 50%; top: 50%; margin-left: -450px;; margin-top: -280px;">
 52         <span style="float: right; margin-top: 35px; color: #488ED5;">新BOS系統,致力於便捷、安全、穩定等方面的客戶體驗</span>
 53     </div>
 54     <div class="main-inner" id="mainCnt"
 55         style="width: 900px; height: 440px; overflow: hidden; position: absolute; left: 50%; top: 50%; margin-left: -450px; margin-top: -220px; background-image: url('${pageContext.request.contextPath }/images/bg_login.jpg')">
 56         <div id="loginBlock" class="login"
 57             style="margin-top: 80px; height: 255px;">
 58             <div class="loginFunc">
 59                 <div id="lbNormal" class="loginFuncMobile">員工登入</div>
 60             </div>
 61             <div class="loginForm">
 62                 <form id="loginform" name="loginform" method="post" class="niceform"
 63                     action="userAction_login.action">
 64                     <div id="idInputLine" class="loginFormIpt showPlaceholder"
 65                         style="margin-top: 5px;">
 66                         <input id="loginform:idInput" type="text" name="username"
 67                             class="loginFormTdIpt" maxlength="50" />
 68                         <label for="idInput" class="placeholder" id="idPlaceholder">帳號:</label>
 69                     </div>
 70                     <div class="forgetPwdLine"></div>
 71                     <div id="pwdInputLine" class="loginFormIpt showPlaceholder">
 72                         <input id="loginform:pwdInput" class="loginFormTdIpt" type="password"
 73                             name="password" value="" />
 74                         <label for="pwdInput" class="placeholder" id="pwdPlaceholder">密碼:</label>
 75                     </div>
 76                     <div class="loginFormIpt loginFormIptWiotTh"
 77                         style="margin-top:58px;">
 78                         <div id="codeInputLine" class="loginFormIpt showPlaceholder"
 79                             style="margin-left:0px;margin-top:-40px;width:50px;">
 80                             <input id="loginform:codeInput" class="loginFormTdIpt" type="text"
 81                                 name="checkcode" title="請輸入驗證碼" />
 82                             <img id="loginform:vCode" src="${pageContext.request.contextPath }/validatecode.jsp"
 83                                 onclick="javascript:document.getElementById('loginform:vCode').src='${pageContext.request.contextPath }/validatecode.jsp?'+Math.random();" />
 84                         </div>
 85                         <a onclick="document.getElementById('loginform').submit();" href="#" id="loginform:j_id19" name="loginform:j_id19">
 86                         <span
 87                             id="loginform:loginBtn" class="btn btn-login"
 88                             style="margin-top:-36px;">登入</span>
 89                         </a>
 90                     </div>
 91                     <div align="center">
 92                         <br/>
 93                         <font color="red">
 94                             <s:actionerror/>
 95                         </font>
 96                     </div>
 97                 </form>
 98             </div>
 99         </div>
100     </div>
101     <div
102         style="width: 900px; height: 50px; position: absolute; text-align: left; left: 50%; top: 50%; margin-left: -450px;; margin-top: 220px;">
103         <span style="color: #488ED5;">Powered By www.itcast.cn</span><span
104             style="color: #488ED5;margin-left:10px;">推薦瀏覽器(右鍵連結-目標另存為):<a
105             href="http://download.firefox.com.cn/releases/full/23.0/zh-CN/Firefox-full-latest.exe">Firefox</a>
106         </span>
107     </div>
108 </body>
109 </html>
 1 package com.itheima.bos.web.action;
 2 
 3 import org.apache.commons.lang3.StringUtils;
 4 import org.apache.struts2.ServletActionContext;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.context.annotation.Scope;
 7 import org.springframework.stereotype.Controller;
 8 
 9 import com.itheima.bos.domain.User;
10 import com.itheima.bos.service.IUserService;
11 import com.itheima.bos.web.action.base.BaseAction;
12 
13 @Controller
14 @Scope("prototype")
15 public class UserAction extends BaseAction<User>{
16     //屬性驅動,接受頁面輸入的驗證碼
17     private String checkcode;
18     public void setCheckcode(String checkcode) {
19         this.checkcode = checkcode;
20     }
21     
22     @Autowired
23     private IUserService userService;
24     /*
25      * 使用者登入
26      */
27     public String login() {
28         //從Session中獲取生成的驗證碼
29         String validatecode = (String) ServletActionContext.getRequest().getSession().getAttribute("key");
30         //教研驗證碼
31         if(StringUtils.isNotBlank(checkcode) && checkcode.equals(validatecode)) {
32             //輸入的驗證碼正確
33             User user = userService.login(model);
34             if(user != null) {
35                 //登陸成功,將user物件存放入session,跳轉到首頁
36                 ServletActionContext.getRequest().getSession().setAttribute("loginUser", user);
37                 return HOME;
38             }else {  
39                 //登陸失敗
40                 this.addActionError("使用者名稱或者密碼輸入錯誤");
41                 return LOGIN;
42             }
43         }else {
44             //輸入的驗證碼錯誤,設定提示資訊,跳轉到登陸頁面
45             this.addActionError("輸入的驗證碼錯誤");
46             return LOGIN;
47         }
48     }
49 }
1 package com.itheima.bos.service;
2 
3 import com.itheima.bos.domain.User;
4 
5 public interface IUserService {
6 
7     public User login(User model);
8 
9 }
 1 package com.itheima.bos.service.impl;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 import org.springframework.transaction.annotation.Transactional;
 6 
 7 import com.itheima.bos.dao.IUserDao;
 8 import com.itheima.bos.domain.User;
 9 import com.itheima.bos.service.IUserService;
10 import com.itheima.bos.utils.MD5Utils;
11 
12 @Service
13 @Transactional
14 public class UserServiceImpl implements IUserService{
15     @Autowired
16     private IUserDao userDao;
17     /*使用者登陸
18      * (non-Javadoc)
19      * @see com.itheima.bos.service.IUserService#login(com.itheima.bos.domain.User)
20      */
21     public User login(User user) {
22         //使用MD5加密密碼
23         String password = MD5Utils.md5(user.getPassword());
24         return userDao.findUserByUsernameAndPassword(user.getUsername(),password);
25     }
26 
27 }
package com.itheima.bos.dao;

import com.itheima.bos.dao.base.IBaseDao;
import com.itheima.bos.domain.User;

public interface IUserDao extends IBaseDao<User>  {

    User findUserByUsernameAndPassword(String username, String password);

}
package com.itheima.bos.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.itheima.bos.dao.IUserDao;
import com.itheima.bos.dao.base.impl.BaseDaoImpl;
import com.itheima.bos.domain.User;

@Repository
public class UserDaoImpl extends BaseDaoImpl<User> implements IUserDao  {
    /*
     * 根據使用者明和密碼查詢使用者(non-Javadoc)
     * @see com.itheima.bos.dao.IUserDao#findUserByUsernameAndPassword(java.lang.String, java.lang.String)
     */
    @Override
    public User findUserByUsernameAndPassword(String username, String password) {
        String hql = "FROM User u WHERE u.username = ? AND u.password = ?";
        List<User> list = (List<User>) this.getHibernateTemplate().find(hql, username,password);
        if(list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

}