2 zdb814390564 zdb814390564 于 2014.12.02 19:59 提问

简单用户登录和数据增删改查

<%@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{6,30}$/; var name=$(&quot;#username&quot;).val(); var pwd=$(&quot;#password&quot;).val(); var flag = true; if(!reg.test(name)){ $(&quot;#username_msg&quot;).text(&quot;用户名为字母数字和下划线&quot;); flag=false; } if(!reg.test(pwd)){ $(&quot;#password_msg&quot;).text(&quot;密码为字母数字和下划线&quot;); flag=false; } } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form action=&quot;CheckLogin.do&quot; method=&quot;post&quot;&gt; &lt;div id=&quot;all&quot;&gt; 用户名:&lt;input type=&quot;text&quot; name=&quot;username&quot; id=&quot;username&quot; onfocus=&quot;check();&quot; /&gt;&lt;span id=&quot;username_msg&quot;&gt;&lt;/span&gt;&lt;br/&gt; 密码:&amp;nbsp;&amp;nbsp;&lt;input type=&quot;password&quot; name=&quot;password&quot; id=&quot;password&quot; onfocus=&quot;check();&quot;/&gt;&lt;span id=&quot;password_msg&quot;&gt;&lt;/span&gt;&lt;br/&gt; &lt;input type=&quot;submit&quot; value=&quot;登陆&quot;/&gt;&lt;br/&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; </code></pre> <p></html></p>

1个回答

zdb814390564
zdb814390564   2014.12.02 20:00

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 {

@Resource
private UserDao userDao;

@RequestMapping("/toLogin.do")
public String tologin(){
    return "Login/login";
}


@RequestMapping("/CheckLogin.do")
public String checkname(String username,String password,HttpSession 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 username,Model 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";
}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!