基本配置和上一篇笔记一样。这里主要分析代码
项目结构如下
package com.cheese.pojo;
public class User {
private String userName;
private String password;
private String fullname;
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 getFullName() {
return fullname;
}
public void setFullName(String fullName) {
this.fullname=fullName;
}
}
2.index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
首页
${pageContext.request.contextPath}用于解决使用相对路径时出现的问题,它的作用是取出所部署项目的名字。
3.login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
登录页面
用户登录
regUser.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
用户注册
用户注册
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="utf-8"%>
登录成功
${requestScope.user.userName }登录成功!
4.UserController.java
package com.cheese.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.cheese.pojo.User;
import java.util.List;
import java.util.ArrayList;
@RequestMapping(value="/user")
@Controller
public class UserController {
private static List userList;
{
userList = new ArrayList();
}
/*注册页面*/
@RequestMapping(value="/showRegUser")
public String showRegUser() {
return "regUser";
}
/*新增用户*/
@RequestMapping(value="/addUser")
public String addUser(
@RequestParam("userName")String userName,
@RequestParam("password")String password,
@RequestParam("fullName")String fullName) {
User user = new User();
user.setUserName(userName);
user.setPassword(password);
user.setFullName(fullName);
userList.add(user);
return "login";//注册成功跳转到登录页
}
@RequestMapping(value="/showLogin")
public String showLogin() {//直接跳转登录页
return "login";
}
/*验证用户*/
@RequestMapping("/validateUser")
public String login(@RequestParam("userName")String userName,
@RequestParam("password")String password,Model model) {
//到集合中查找用户是否存在,此处用来模拟数据库验证
for(User user:userList) {
if(user.getUserName().equals(userName)
&& user.getPassword().equals(password)) {
model.addAttribute("user",user);//页面回显数据
return "welcome";//成功跳转欢迎页
}
}
return "login";//验证失败,跳转都到登录页
}
}
小知识
SpringMVC后台Controller中获取前段页面request中的参数 的方法主要是两种:
1.request.getParameter(“name”);
2.@RequestParam直接获取,如下:
public void getUser(
@RequestParam String userName,
@RequestParam String userPassword,
HttpServletRequest request)
{
String name= userName;
String password= userPassword;
}
其他特性:
1. @RequestParam也可以指定传入的参数名
public void getUser(
@RequestParam(value="user") String userName,
@RequestParam(value="pass") String userPassword,
HttpServletRequest request)
此时如果前端传的参数名不是user,就会报错
public void getUser(
@RequestParam(value="user",required = true) String userName,
@RequestParam(value="pass",required = false) String userPassword,
)
3.如果不用@RequestParam注解,也可以获得参数,如果没有传该参数不会报错。
public void getUser( String userName, String userPassword)
web.xml
springmvc
spring-mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
spring-mvc
/
EncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
EncodingFilter
/*
spring-mvc.xml