问题描述
- 简单用户登录和数据增删改查
- <%@page pageEncoding=""utf-8"" isELIgnored=""false""%>
<%@taglib uri=""http://java.sun.com/jsp/jstl/core"" prefix=""c""%>
<%@taglib uri=""http://java.sun.com/jsp/jstl/fmt"" prefix=""fmt""%>
<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">body{ text-align:center;}#all{margin:auto: text-align:left; width:1000px;}}
<br> function check(){</p><pre><code> var reg = /^w{630}$/; var name=$("#username").val(); var pwd=$("#password").val(); var flag = true; if(!reg.test(name)){ $("#username_msg").text("用户名为字母数字和下划线"); flag=false; } if(!reg.test(pwd)){ $("#password_msg").text("密码为字母数字和下划线"); flag=false; } } </script></head><body> <form action="CheckLogin.do" method="post"> <div id="all"> 用户名:<input type="text" name="username" id="username" onfocus="check();" /><span id="username_msg"></span><br/> 密码:&nbsp;&nbsp;<input type="password" name="password" id="password" onfocus="check();"/><span id="password_msg"></span><br/> <input type="submit" value="登陆"/><br/> </div> </form></body></code></pre><p></html></p>
解决方案
package com.tarena.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.tarena.dao.UserDao;
import com.tarena.entity.User;
@Controller
@RequestMapping(""/login"")
public class LoginController {
@Resourceprivate UserDao userDao;@RequestMapping(""/toLogin.do"")public String tologin(){ return ""Login/login"";}@RequestMapping(""/CheckLogin.do"")public String checkname(String usernameString passwordHttpSession session){ User user = userDao.findByName(username); if(user==null){ System.out.println(user); return ""redirect:toLogin.do""; } if(user.getPassword().equals(password)){ session.setAttribute(""User"" user); return ""redirect:list.do""; } return ""redirect:toLogin.do"";}@RequestMapping(""/list.do"")public String list(Model model){ List<User> lists = userDao.findAll(); model.addAttribute(""users""lists); return ""Login/list"";}@RequestMapping(""/toUpdateUser.do"")public String updateUser(String usernameModel model){ User user = userDao.findByName(username); model.addAttribute(""user"" user); return ""Login/update_user"";}@RequestMapping(""/updateUser.do"")public String update(User user){ userDao.updateUser(user); return ""redirect:list.do"";}@RequestMapping(""/deleteUser.do"")public String deleteUser(String username){ userDao.deleteUser(username); return ""redirect:list.do"";}@RequestMapping(""/toaddUser.do"")public String toAddUser(){ return ""Login/add_user"";}@RequestMapping(""/addUser.do"")public String addUser(User user){ userDao.addUser(user); return ""redirect:list.do"";}
}