mybatis的查询方式(map,user,一个参数id)
validator-api jar包 用来验证注解的(例如邮箱的格式或者非空校验)
代码实现:
控制层:
package com.easymall.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
//针对jsp里边的私密文件访问
@Controller
@RequestMapping("/forward")
public class ForwardController {
@RequestMapping("/{path}.action")
public String forward(@PathVariable String path){
return "forward:/WEB-INF/jsp/"+path+".jsp";
}
@RequestMapping("aa.action")
public void a(){
System.out.println(123);
}
}
package com.easymall.controller;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.easymall.domain.User;
import com.easymall.exception.MsgException;
import com.easymall.service.UserService;
import com.easymall.utils.MD5Utils;
import com.easymall.utils.VerifyCode;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService=null;
//乱码处理
// 获取参数
// 获取是否记住用户名选项
// 判断是否需要自动登录,如果需要则创建一个自动登录的cookie,保存用户信息30天
// 传输数据到UserService
/**
* 登录
* @throws UnsupportedEncodingException
*/
@RequestMapping("/loginUser.action")
public String loginUser(String autologin,HttpServletResponse response,HttpServletRequest request,String username,String password,HttpSession session,Model model,String remname) throws UnsupportedEncodingException{
/**
* 记住用户名
*/
if("true".equals(remname)){
Cookie remnamec=new Cookie("remname", URLEncoder.encode(username, "utf-8"));
remnamec.setPath(request.getContextPath()+"/");
remnamec.setMaxAge(60*60*24*30);
response.addCookie(remnamec);
}else{//清除用户
Cookie remnamec=new Cookie("remname", "");
remnamec.setPath(request.getContextPath()+"/");
remnamec.setMaxAge(0);
response.addCookie(remnamec);
}
//保存用户的登录状态
if("true".equals(autologin)){
Cookie autologinc=new Cookie("autologin", URLEncoder.encode(username,"utf-8")+"#"+MD5Utils.md5(password));
autologinc.setPath(request.getContextPath()+"/");
autologinc.setMaxAge(60*60*24*30);
response.addCookie(autologinc);
}
else{
Cookie autologinc=new Cookie("autologin","");
autologinc.setPath(request.getContextPath()+"/");
autologinc.setMaxAge(0);
response.addCookie(autologinc);
}
try {
if(username==null||password==null){
throw new MsgException("用户名或密码错误");
}
User user = userService.loginUser(username,MD5Utils.md5(password));
// 得到user对象,应该放入session域,保留登录状态
session.setAttribute("user", user);
} catch (MsgException e) {
// 发生异常则证明向前台页面输出错误提示信息
model.addAttribute("msg", e.getMessage());
return "login";
}
// 跳转回首页
return "redirect:/index.jsp";
}
/**
*验证码刷新
* @param response
* @param session
* @throws IOException
*/
@RequestMapping("/valiImg.action")
public void valiImg(HttpServletResponse response,HttpSession session) throws IOException{
// 控制浏览器不使用缓存
response.setDateHeader("Expires", -1);
response.setHeader("Cache-Control", "no-cache");
// response.setContentType("text/html;charset=utf-8");
// PrintWriter out = response.getWriter();
VerifyCode vc = new VerifyCode();
// 当前servlet只有img标签调用,所以将图片放入缓存区中,
// 最终会在其调用的位置输出在浏览器中
vc.drawImage(response.getOutputStream());
String code = vc.getCode();
// 添加验证码到session
session.setAttribute("valistr", code);
System.out.println(code);
System.out.println("执行成功!");
}
/**
* 注册用户
*/
@RequestMapping("/regist.action")
public String registUser(@Valid User user,Errors errors,String password2,String valistr,Model model,HttpSession session,HttpServletResponse response){
// 请求乱码处理
// 响应的乱码处理
// 请求参数
// 验证码校验
// session
// 获取域中的验证码用于用户输入的验证码比较
String valistr2 = (String) session.getAttribute("valistr");
if (valistr==null||valistr2==null||!valistr2.equalsIgnoreCase(valistr)) {
model.addAttribute("msg", "验证码错误");
return "regist";
}
//其它校验
if(errors.hasErrors()){
List<FieldError> list=errors.getFieldErrors();
for (FieldError fe : list) {
String field=fe.getField();
String msg=fe.getDefaultMessage();
System.out.println("后台校验数据出错:"+field+"~~~"+msg);
}
model.addAttribute("msg","后台数据校验出错!");
return "regist";
}
//完成注册
user.setPassword(MD5Utils.md5(user.getPassword()));
try {
userService.registUser(user);
} catch (MsgException e) {
model.addAttribute("msg",e.getMessage());
return "regist";
}
return "redirect:/index.jsp";
}
//Ajax验证用户名是否存在
@ResponseBody
@RequestMapping(value="/ajaxCheckUserName.action",produces="application/json;charset=utf-8")
public String ajaxCheckUserName(String username){
if(userService.hasUserName(username)){
return "用户名已存在";
}else{
return "用户名可以使用";
}
}
/**
* 注销
*/
@RequestMapping("/logoutUser.action")
public String logoutUser(HttpSession session,HttpServletRequest request,HttpServletResponse response){
session.invalidate();
Cookie autologinc=new Cookie("autologin","");
autologinc.setPath(request.getContextPath()+"/");
autologinc.setMaxAge(0);
response.addCookie(autologinc);
return "redirect:/index.jsp";
}
}
`
bean层:
package com.easymall.domain;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.Email;
//封装用户信息的Javabean
public class User implements HttpSessionBindingListener{
private int id;
@NotNull(message="用户名不能为空")
private String username;
@NotNull(message="密码不能为空")
@Size(min=6,max=16,message="密码长度必须是6~16位")
@Pattern(regexp="^(\\w||[~@#$%.]){6,16}$",message="密码格式不正确")
private String password;
@NotNull(message="昵称不能为空")
private String nickname;
@NotNull(message="邮箱不能为空")
@Email(message="邮箱格式不正确")
private String email;
public static Logger logger=Logger.getLogger("User.class");
public User(){}
public User(int id, String username, String password, String nickname,
String email) {
super();
this.id = id;
this.username = username;
this.password = password;
this.nickname = nickname;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void valueBound(HttpSessionBindingEvent arg0) {
//System.out.println("用户【"+username+"】登录EasyMall");
logger.warn("用户【"+username+"】登录EasyMall");
}
public void valueUnbound(HttpSessionBindingEvent arg0) {
//System.out.println("用户【"+username+"】退出EasyMall");
logger.warn("用户【"+username+"】退出EasyMall");
}
}
异常层:
package com.easymall.exception;
//
public class MsgException extends RuntimeException {
public MsgException(String msg){
super(msg);
}
}
过滤层:
package com.easymall.exception;
//
public class MsgException extends RuntimeException {
public MsgException(String msg){
super(msg);
}
}
数据层:
package com.easymall.exception;
//
public class MsgException extends RuntimeException {
public MsgException(String msg){
super(msg);
}
}
服务层:
package com.easymall.service;
import com.easymall.domain.User;
public interface UserService {
void registUser(User user);
boolean hasUserName(String username);
/**
* 根据用户名和密码查找用户
* @param username
* @param md5
* @return
*/
User loginUser(String username,String md5psw);
}
package com.easymall.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.easymall.domain.User;
import com.easymall.exception.MsgException;
import com.easymall.mapper.UserMapper;
import com.easymall.utils.MD5Utils;
@Service
public class UserServiceImp implements UserService {
@Autowired
UserMapper userMapper=null;
@Transactional
@Override
public void registUser(User user) {
//判断用户名是否存在
User findUser=userMapper.findUserByUsername(user.getUsername());
if(findUser!=null){
//用户名存在,
//创建自定义异常,作为通知,提示用户名已存在
throw new MsgException("用户名已存在");
}else{
//用户名不存在,添加一条数据
userMapper.addUser(user);
}
//int i=1/0;
}
//Ajax检测用户名是否存在
@Transactional
@Override
public boolean hasUserName(String username) {
User user=userMapper.findUserByUsername(username);
return user !=null;
}
@Transactional
@Override
public User loginUser(String username,String md5psw) {
if(username==null||md5psw==null){
throw new MsgException("用户名或密码错误");
}else{
User user=userMapper.findUserByUsernameAndPsw(new User(0,username,md5psw,null,null));
if(user!=null){
return user;
}else{
throw new MsgException("用户名或密码错误");
}
}
}
}
工具包:
package com.easymall.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.easymall.domain.User;
import com.easymall.exception.MsgException;
import com.easymall.mapper.UserMapper;
import com.easymall.utils.MD5Utils;
@Service
public class UserServiceImp implements UserService {
@Autowired
UserMapper userMapper=null;
@Transactional
@Override
public void registUser(User user) {
//判断用户名是否存在
User findUser=userMapper.findUserByUsername(user.getUsername());
if(findUser!=null){
//用户名存在,
//创建自定义异常,作为通知,提示用户名已存在
throw new MsgException("用户名已存在");
}else{
//用户名不存在,添加一条数据
userMapper.addUser(user);
}
//int i=1/0;
}
//Ajax检测用户名是否存在
@Transactional
@Override
public boolean hasUserName(String username) {
User user=userMapper.findUserByUsername(username);
return user !=null;
}
@Transactional
@Override
public User loginUser(String username,String md5psw) {
if(username==null||md5psw==null){
throw new MsgException("用户名或密码错误");
}else{
User user=userMapper.findUserByUsernameAndPsw(new User(0,username,md5psw,null,null));
if(user!=null){
return user;
}else{
throw new MsgException("用户名或密码错误");
}
}
}
}
package com.easymall.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.easymall.domain.User;
import com.easymall.exception.MsgException;
import com.easymall.mapper.UserMapper;
import com.easymall.utils.MD5Utils;
@Service
public class UserServiceImp implements UserService {
@Autowired
UserMapper userMapper=null;
@Transactional
@Override
public void registUser(User user) {
//判断用户名是否存在
User findUser=userMapper.findUserByUsername(user.getUsername());
if(findUser!=null){
//用户名存在,
//创建自定义异常,作为通知,提示用户名已存在
throw new MsgException("用户名已存在");
}else{
//用户名不存在,添加一条数据
userMapper.addUser(user);
}
//int i=1/0;
}
//Ajax检测用户名是否存在
@Transactional
@Override
public boolean hasUserName(String username) {
User user=userMapper.findUserByUsername(username);
return user !=null;
}
@Transactional
@Override
public User loginUser(String username,String md5psw) {
if(username==null||md5psw==null){
throw new MsgException("用户名或密码错误");
}else{
User user=userMapper.findUserByUsernameAndPsw(new User(0,username,md5psw,null,null));
if(user!=null){
return user;
}else{
throw new MsgException("用户名或密码错误");
}
}
}
}
配置文件:
mapper:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.easymall.mapper.UserMapper">
<select id="findUserByUsername" resultType="com.easymall.domain.User">
select * from user where username=#{username};
</select>
<insert id="addUser">
insert into user values(null,#{username},#{password},#{nickname},#{email});
</insert>
<select id="findUserByUsernameAndPsw" resultType="com.easymall.domain.User">
select * from user where username=#{username} and password=#{password};
</select>
</mapper>
applicationContext:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
" >
<!-- 配置ioc -->
<context:component-scan base-package="com.easymall.controller"></context:component-scan>
<context:component-scan base-package="com.easymall.service"></context:component-scan>
<!-- 配置di -->
<context:annotation-config/>
<!-- 配置aop -->
<aop:aspectj-autoproxy/>
<!-- 配置生命式事务处理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--用注解管理事务 -->
<tx:annotation-driven/>
<!-- 整合mybatis -->
<!--配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///easymall"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!--配置sqlsessionfactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:/sqlMapConfig.xml"></property>
<property name="mapperLocations" value="classpath:/mappers/*.xml"></property>
</bean>
<!--mybatis mapperbean扫描器 负责生成接口的实现类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.easymall.mapper"></property>
</bean>
</beans>
springmvc:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
" >
<!--配置处理器映射器 -->
<context:component-scan base-package="com.easymall.controller"></context:component-scan>
<mvc:annotation-driven/>
<!--配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
sqlmapconfig:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>SSM_EasyMall</display-name>
<!-- 配置spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>
<!--配置用户登录的过滤器 -->
<filter>
<filter-name>autologinfilter</filter-name>
<filter-class>com.easymall.fileter.AutologinFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>autologinfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 乱码过滤 --><!--只针对post -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--主页面配置 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
主页:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<!-- buffer="0kb" -->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/index.css"/>
<title>欢迎光临EasyMall</title>
</head>
<body>
<!-- 引入头部 -->
<%-- <%request.getRequestDispatcher("/_head.jsp").include(request, response); %> --%>
<!--私密地址,不可直接这样访问,可以请求转发 -->
<%@include file="/WEB-INF/jsp/_head.jsp" %>
<div id="index">
<div id="line1">
<img src="${pageContext.request.contextPath}/img/index/banner_big.jpg"/>
</div>
<div id="line2">
<img id="line2_1" src="${pageContext.request.contextPath}/img/index/adv1.jpg"/>
<img id="line2_2" src="${pageContext.request.contextPath}/img/index/adv2.jpg"/>
<img id="line2_3" src="${pageContext.request.contextPath}/img/index/adv_l1.jpg"/>
</div>
<div id="line3">
<img id="line3_1" src="${pageContext.request.contextPath}/img/index/adv3.jpg"/>
<img id="line3_2" src="${pageContext.request.contextPath}/img/index/adv4.jpg"/>
<div id="line3_right">
<img id="line3_3" src="${pageContext.request.contextPath}/img/index/adv_l2.jpg"/>
<img id="line3_4" src="${pageContext.request.contextPath}/img/index/adv_l3.jpg"/>
</div>
</div>
<div id="line4">
<img alt="" src="${pageContext.request.contextPath}/img/index/217.jpg"/>
</div>
<div id="line5">
<span id="line5_1">
<img src="${pageContext.request.contextPath}/img/index/icon_g1.png"/> 500强企业 品质保证
</span>
<span id="line5_2">
<img src="${pageContext.request.contextPath}/img/index/icon_g2.png"/> 7天退货 15天换货
</span>
<span id="line5_3">
<img src="${pageContext.request.contextPath}/img/index/icon_g3.png"/> 100元起免运费
</span>
<span id="line5_4">
<img src="${pageContext.request.contextPath}/img/index/icon_g4.png"/> 448家维修网点 全国联保
</span>
</div>
</div>
<!-- 引入尾部 -->
<%-- <%request.getRequestDispatcher("/_foot.jsp").include(request, response); %> --%>
<%@include file="/WEB-INF/jsp/_foot.jsp" %>
</body>
</html>
登录:
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/login.css"/>
<title>EasyMall欢迎您登录</title>
<!-- 引入jQuery函数库 -->
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.4.2.js"></script>
<script type="text/javascript">
//文档就绪事件
$(function(){
$("input[name='username']").val(decodeURI('${cookie.remname.value}'));
});
</script>
</head>
<body>
<h1><font color="red">欢迎登录EasyMall</font></h1>
<form action="${pageContext.request.contextPath}/user/loginUser.action" method="POST">
<table>
<%-- <%
//获取cookie中的remname 读取其中的用户名
Cookie[] cs=request.getCookies();
Cookie remnameC=null;
//初次获取cookie数组为null
if(null!=cs){
for(Cookie c:cs){
if("remname".equals(c.getName())){
remnameC=c;
}
}
}
String username="";
if(remnameC!=null){
username=URLDecoder.decode(remnameC.getValue(), "utf-8");
}
%> --%>
<tr> <%-- <%=request.getAttribute("msg")==null?"":request.getAttribute("msg") %> --%>
<td class="tdx"
colspan="2" style="color:red;text-align: center">${requestScope.msg}</td>
</tr>
<tr>
<td class="tdx">用户名:</td>
<%-- <td><input type="text" name="username" value="<%=username%>"/></td> --%>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td class="tdx">密 码:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2">
<%-- <%="".equals(username)?"":"checked='checked'" %> --%>
<input type="checkbox" name="remname" value="true"
${not empty cookie.remname.value?"checked='checked'":"" }
/>记住用户名
<input type="checkbox" name="autologin" value="true" />30天内自动登录
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<input type="submit" value="登录"/>
</td>
</tr>
</table>
</form>
</body>
</html>
注册
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/regist.css"/>
<!-- 引入jQuery函数库 -->
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.4.2.js"></script>
<!-- 前台校验 -->
<script type="text/javascript">
var formObj={
"checkForm":function(){
var canSub=true;
//1.获取参数
//2.做出校验
/* //非空校验
var username=$("input[name='username']").val();
//清空操作
$("input[name='username']").nextAll("span").text("");
if(username==""){
$("input[name='username']").nextAll("span").text("用户名不能为空").css("color","red");
} */
canSub=this.checkNull("username", "用户名不能为空")&&canSub;
canSub=this.checkNull("password", "密码不能为空")&&canSub;
canSub=this.checkNull("password2", "确认密码不能为空")&&canSub;
canSub=this.checkNull("nickname", "昵称不能为空")&&canSub;
canSub=this.checkNull("email", "邮箱不能为空")&&canSub;
canSub=this.checkNull("valistr", "验证码不能为空")&&canSub;
//密码一致性校验
canSub=this.checkPassword()&&canSub;
//邮箱格式校验
canSub=this.checkEmail()&&canSub;
return canSub;
},
//非空校验
"checkNull":function(name,msg){
var tag=$("input[name='"+name+"']").val();
//清空操作
//$("input[name='"+name+"']").nextAll("span").text("");
this.setMsg(name, "");
if(tag==""){
//$("input[name='"+name+"']").nextAll("span").text(msg).css("color","red");
this.setMsg(name, msg);
return false;
}
return true;
},
//密码一致性校验
"checkPassword":function(){
var password=$("input[name='password']").val();
var password2=$("input[name='password2']").val();
if(password!=""&&password2!=""&&password!=password2){
this.setMsg("password2", "两次密码不一致");
return false;
}
return true;
},
"checkEmail":function(){
//lishuai@tedu.cn
var reg=/\w+@\w+(\.\w+)+/;
var email=$("input[name='email']").val();
if(email!=""&&!reg.test(email)){
this.setMsg("email", "邮箱格式不正确");
return false;
}
return true;
},
//消息提示span
"setMsg":function(name,msg){
$("input[name='"+name+"']").nextAll("span").text(msg).css("color","red");
}
};
//文档就绪事件
$(function(){
//为username输入框添加鼠标离焦事件
$("input[name='username']").blur(function(){
formObj.checkNull("username", "用户名不能为空");
//获取输入框中的值
var username=$(this).val();
//ajax校验用户名是否存在 虚拟路径 动态资源
if(username!=""){
//$("#username_msg").load("${pageContext.request.contextPath}/AjaxCheckUsernameServlet",{"username":username});
$("#username_msg").load("${pageContext.request.contextPath}/user/ajaxCheckUserName.action",{"username":username});
}
});
$("input[name='password']").blur(function(){
formObj.checkNull("password", "密码不能为空");
});
$("input[name='password2']").blur(function(){
formObj.checkNull("password2", "确认密码不能为空");
formObj.checkPassword();
});
$("input[name='nickname']").blur(function(){
formObj.checkNull("nickname", "昵称不能为空");
});
$("input[name='email']").blur(function(){
formObj.checkNull("email", "邮箱不能为空");
formObj.checkEmail();
});
$("input[name='valistr']").blur(function(){
formObj.checkNull("valistr", "验证码不能为空");
});
//等待页面加载完成后,为验证码图片绑定单击事件,更新验证码
$("#img").click(function(){
//变化的时间让验证码变化,如果没变化就没有新的请求
var date=new Date();
var time=date.getTime();
//$(this).attr("src","${pageContext.request.contextPath}/ValidateServlet?time="+time);
$(this).attr("src","${pageContext.request.contextPath}/user/valiImg.action?time="+time);
});
});
</script>
<title>欢迎注册EasyMall</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/regist.action" method="post" onsubmit="return formObj.checkForm()">
<h1>欢迎注册EasyMall</h1>
<table>
<tr>
<td class="tds" colspan="2"
style="color:red;text-align:center"
><%=request.getAttribute("msg")==null?"":request.getAttribute("msg") %></td>
</tr>
<tr>
<td class="tds">用户名:</td>
<td><!-- 回显用户名 -->
<%-- <input type="text" name="username"
value="<%=request.getParameter("username")==null?"":request.getParameter("username")%>"/> --%>
<input type="text" name="username"
value="${param.username }"/>
<span id="username_msg"></span>
</td>
</tr>
<tr>
<td class="tds">密码:</td>
<td>
<input type="password" name="password"/>
<span></span>
</td>
</tr>
<tr>
<td class="tds">确认密码:</td>
<td>
<input type="password" name="password2"/>
<span></span>
</td>
</tr>
<tr>
<td class="tds">昵称:</td>
<td>
<%-- <input type="text" name="nickname" value="<%=request.getParameter("nickname")==null?"":request.getParameter("nickname")%>"/> --%>
<input type="text" name="nickname" value="${param.nickname }"/>
<span></span>
</td>
</tr>
<tr>
<td class="tds">邮箱:</td>
<td>
<%-- <input type="text" name="email" value="<%=request.getParameter("email")==null?"":request.getParameter("email")%>"/> --%>
<input type="text" name="email" value="${param.email}"/>
<span></span>
</td>
</tr>
<tr>
<td class="tds">验证码:</td>
<td>
<input type="text" name="valistr"/>
<%-- <img id="img" src="${pageContext.request.contextPath}/ValidateServlet" width="" height=""/> --%>
<img id="img" src="${pageContext.request.contextPath}/user/valiImg.action" width="" height=""/>
<span></span>
</td>
</tr>
<tr>
<td class="sub_td" colspan="2" class="tds">
<input type="submit" value="注册用户"/>
</td>
</tr>
</table>
</form>
</body>
</html>
头部:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/foot.css"/>
<div id="common_head">
<div id="line1">
<div id="content">
<%-- <%
//判断是否存在登录状态
if(request.getSession(false)!=null&&request.getSession().getAttribute("username")!=null){
//第一个判断时确定session对象是否存在,如果村子仍然不能确定是否包含username域属性是否存在(登陆状态)
//第二个判断是确定当前session对象是否包含username域属性,如果包含才可取出用户名,在页面中显示
%>
<a href="#">欢迎<%=request.getSession().getAttribute("username") %>,回来</a> |
<a href="${pageContext.request.contextPath}/LogOutServlet">注销</a>
<%
}else{
%>
<a href="${pageContext.request.contextPath}/login.jsp">登录</a> |
<a href="${pageContext.request.contextPath}/regist.jsp">注册</a>
<%
}
%> --%>
<c:if test="${sessionScope.user!=null }">
<a href="#">欢迎${sessionScope.user.username},回来</a> |
<a href="${pageContext.request.contextPath}/user/logoutUser.action">注销</a>
</c:if>
<c:if test="${sessionScope.user==null }">
<a href="${pageContext.request.contextPath}/forward/login.action">登录</a> |
<a href="${pageContext.request.contextPath}/forward/regist.action">注册</a>
</c:if>
</div>
</div>
<div id="line2">
<img id="logo" src="${pageContext.request.contextPath}/img/head/logo.jpg"/>
<input type="text" name=""/>
<input type="button" value="搜索"/>
<span id="goto">
<a id="goto_order" href="#">我的订单 </a>
<a id="goto_cart" href="#">我的购物车 </a>
</span>
<img id="erwm" src="${pageContext.request.contextPath}/img/head/qr.jpg"/>
</div>
<div id="line3">
<div id="content">
<ul>
<li><a href="#">首页</a></li>
<li><a href="#">全部商品</a></li>
<li><a href="#">手机数码</a></li>
<li><a href="#">电脑平板</a></li>
<li><a href="#">家用电器</a></li>
<li><a href="#">汽车用品</a></li>
<li><a href="#">食品饮料</a></li>
<li><a href="#">图书杂志</a></li>
<li><a href="#">服装服饰</a></li>
<li><a href="#">理财产品</a></li>
</ul>
</div>
</div>
</div>
尾部:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<%-- <link rel="stylesheet" href="<%=request.getContextPath() %>/css/foot.css"/> --%>
<link rel="stylesheet" href="${pageContext.request.contextPath }/css/foot.css"/>
<div id="common_foot">
<p>
Copyright @ 2011-2019达内软件技术有限公司 版权所有 保留一切权力 苏 B2-201
<br>
网络文化经营许可证苏网文[2012]0401-019号
</p>
</div>