这是在之前创建的人事管理系统中继续增加的内容,可参考之前发布的博文看,主要目录结构如下:
package com.controller;//package com.controller;
/*public class RestController {
}*/
import com.dao.UserDao;
import java.util.ArrayList;
import java.util.List;
import com.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping(value="/user")
public class UserController {
private static List userList;
/* @Autowired
private static User user;*/
public UserController() {
this.userList = new ArrayList<>();//初始化userlist
}
@RequestMapping(value = "/register",method = RequestMethod.GET)
public String registerForm() {
return "registerForm";
}
@RequestMapping(value = "/register",method = RequestMethod.POST)
public String register(@RequestParam("loginname") String loginname,
@RequestParam("username") String username,
@RequestParam("password") String password) {
User user = new User();
user.setLoginname(loginname);
user.setUsername(username);
user.setPassword(password);
//存储user信息
userList.add(user);
//跳转页面到登录页面
return "/user/loginForm";
}
@RequestMapping("/tologin")
public String tologin()
{
return "loginForm";
}
public String login(Model model,@RequestParam(value = "loginname",required = false) String loginname,
@RequestParam(value ="password",required = false) String password
) {
//到集合中查询用户是否存在
for (User user : userList){
if (user.getLoginname().equals(loginname)&& user.getPassword().equals(password)){
model.addAttribute("user",user);
return "/user/sayhello";
}
}
return "/user/loginForm";
}
}//end
package com.pojo;
public class User {
private String loginname;
private String password;
private String username;
public String getLoginname() {
return loginname;
}
public String getPassword() {
return password;
}
public String getUsername(){return username; }
public void setLoginname(String loginname)
{
this.loginname=loginname;
}
public void setPassword(String password)
{this.password=password;}
public void setUsername(String username)
{
this.username=username;
}
}
loginForm:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
登录界面
registerForm:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
注册页面
sayhello:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
欢迎界面
欢迎${requestScope.user.username}
点击进入登录界面
编码过程中,出现了几个问题:
1、,这个首先按照网上的方法,选择File——>invalildate Cashes/restart,然后重启了IDEA。但是没有用,于是采取第二个办法,在一段代码中进行修改:
原代码:
public String login(Model model,@RequestParam(value = "loginname") String loginname,
@RequestParam(value ="password") String password
) {
public String login(Model model,@RequestParam(value = "loginname",required = false) String loginname,
@RequestParam(value ="password",required = false) String password
) {
这里切记要使required=false,确实有用,这样问题就解决了
2、
发现程序到不了login那里去,于是在usercontroller中加了如下代码:
@RequestMapping("/tologin")
public String tologin()
{
return "loginForm";
}
并且在index中也有相应修改:login——>tologin
3、
发现这个程序是不连接数据库的,采用List直接写值方式,所以之前列表一直是空的,登录的话根本就没有结果。于是修改了usercontroller部分代码:
修改后:
@RequestMapping("/tologin")
public String tologin()
{
return "loginForm";
}
@RequestMapping("/login")
public String login(Model model, String password,String loginname ){
//到集合中查询用户是否存在
System.out.println("here");
User MM=new User();
MM.setLoginname("美丽");
MM.setPassword("789");
userList.add(MM);
for (User user : userList){
if (user.getLoginname().equals(loginname)&& user.getPassword().equals(password)){
model.addAttribute("user",user);
return "sayhello";
}
}
return "loginForm";
但是不用数据库不是长久之计,所以进一步修改还在路上。