增删改查,login
1. login.jsp,写在外面,及跟WEB-INF同一级目录,如:ls Webcontent; >> META-INF WEB-INF login.jsp
表单:<form>中包含两个参数,action(目标) & method (方法)。使用了<input>标签,type(text,password,submit), name,两个参数。
<form action = "user/login" method = "post"> <span style="white-space:pre"> </span>username:<input type="text" name="username"/><br/> <span style="white-space:pre"> </span>password:<input type="password" name="password"/><br/> <span style="white-space:pre"> </span> <input type="submit"/> </form>
2. userController.java
login.jsp点击“submit”以后,将访问userController里的login方法:Public String login(){ }
· 指定进入method的条件
· 指定进入method的参数
· 异常处理1:用户名不存在, 异常处理2:密码不正确
· 把User存到Session中 【?】
·
<span style="white-space:pre"> </span>@RequestMapping(value = "/login", method = RequestMethod.POST)//RequestMapping就是指定怎么进入这个方法来的 <span style="white-space:pre"> </span>public String login(String username, String password, HttpSession session){<span style="white-space:pre"> </span>//传2个参数进来, 1st:username 2nd:password//第3个参数是指定Session注入的 <span style="white-space:pre"> </span>if (!users.containsKey(username)){<span style="white-space:pre"> </span>//异常处理1.如果不包含,则用户名不存在 <span style="white-space:pre"> </span>//创建一个异常处理.java,见下“3. 需要一个处理异常的class” <span style="white-space:pre"> </span>throw new UserException("Username does not exist! "); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>User u = users.get(username);<span style="white-space:pre"> </span>//得到username <span style="white-space:pre"> </span>if (!u.getPassword().equals(password) )<span style="white-space:pre"> </span>{//if 不相等, 说明密码不正确: <span style="white-space:pre"> </span>throw new UserException("Password does not match!"); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>//把user 存到Session中: <span style="white-space:pre"> </span>session.setAttribute("loginUser",u); <span style="white-space:pre"> </span>return "redirect:/user/users";<span style="white-space:pre"> </span>//login success, jump to user/users <span style="white-space:pre"> </span>}
· 新建一个class:
Name: UserException.class
Superclass: java.lang.RuntimExcption
· Add Default Version ID: 代码: private static final long serialVersionUID = 1L;
· 创建一个Constructors构造器 from Superclass: 右键-Resources-Generate Constructors from Superclass
package edu.bit.model; <span style="white-space: pre;"> </span>public class UserException extends RuntimeException { <span style="white-space: pre;"> </span>public UserException() { super(); // TODO Auto-generated constructor stub } public UserException(String arg0, Throwable arg1, boolean arg2, boolean arg3) { super(arg0, arg1, arg2, arg3); // TODO Auto-generated constructor stub } public UserException(String arg0, Throwable arg1) { super(arg0, arg1); // TODO Auto-generated constructor stub } public UserException(String arg0) { super(arg0); // TODO Auto-generated constructor stub } public UserException(Throwable arg0) { super(arg0); // TODO Auto-generated constructor stub }
==========至此,基本完成一个登陆操作的编写,但如果用户名不存在或密码不正确,将返回500错误,并按UserException("错误提示")提示错误 ========
测试链接: http://localhost:8080/myhello/login.jsp