项目代码3
Posted hellopaidaxing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了项目代码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; } }
以上是关于项目代码3的主要内容,如果未能解决你的问题,请参考以下文章
python 用于数据探索的Python代码片段(例如,在数据科学项目中)
Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段