org.springframework
spring-webmvc
5.2.7.RELEASE
Archetype Created Web Application
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc-config.xml
1
springmvc
/
@Controller
public class HelloController {
}
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(){
System.out.println("hello weiwei");
return "home";//返回后交给视图解析器 通过视图解析器进行匹配,返回页面
}
}
@RequestMapping("regist")
public String showRegist(){
return "regist";
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
@RequestMapping("/doReg")
public String doReg(HttpServletRequest request){
System.out.println("doReg");
String username=request.getParameter("username");
String password=request.getParameter("password");
String age=request.getParameter("age");
String phone=request.getParameter("phone");
String email=request.getParameter("email");
System.out.println(username+password+age+phone+email);
return "login";
}
@RequestMapping("/doReg")
public String doReg(HttpServletRequest request){
System.out.println("doReg");
String username=request.getParameter("username");
String password=request.getParameter("password");
String age=request.getParameter("age");
String phone=request.getParameter("phone");
String email=request.getParameter("email");
System.out.println(username+password+age+phone+email);
return "login";
}
@RequestMapping("/doReg")
public String doReg(String username,String password,Integer age,Integer phone,String email){
System.out.println("doReg");
System.out.println(username+password+age+phone+email);
return "login";
}
@RequestMapping("/doReg")
public String doReg(User user){
System.out.println(user);
return "login";
}
@Data //此注解的作用就是生成get set方法
@ToString //此注解的作用就是生成 toString方法
public class User {
private String username;
private String password;
private Integer age;
private String phone;
private String email;
}
@Controller
public class PageController {
@RequestMapping("login")
public String showLogin(){
return "login";
}
@RequestMapping("login2")
public ModelAndView showLogin2(){
ModelAndView mv=new ModelAndView();
mv.addObject("这是我的数据");//设置数据
mv.setViewName("login");
return mv;
}
@RequestMapping("showString")
@ResponseBody//此注解用来指定给浏览器返回的是文本数据
public String showString(){
return "showString";
}
}
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf8
encodingFilter
/*
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
Title
错误提示
<%=request.getAttribute("error")%>
@RequestMapping("/doLogin")
public String doLogin(String username,String password,HttpServletRequest request){
System.out.println("doLogin");
if("root".equals(username)&&"123".equals(password)){
return "home";
}else {
request.setAttribute("error","用户名或密码错误");
return "error";
}
}
@RequestMapping("/doLogin")
public String doLogin(String username, String password, ModelMap modelMap){
System.out.println("doLogin");
if("root".equals(username)&&"123".equals(password)){
return "home";
}else {
modelMap.addAttribute("error","用户名或密码错误");
return "error";
}
}
@Controller
@RequestMapping("user")
public class UserController {
@RequestMapping("/doLogin")
public String doLogin(String username, String password, ModelMap modelMap,
HttpSession session){
System.out.println("doLogin");
if("root".equals(username)&&"123".equals(password)){
session.setAttribute("username",username);
return "home";
}else {
modelMap.addAttribute("error","用户名或密码错误");
return "error";
}
}
@RequestMapping("/doLogin")
public String doLogin(@RequestParam("username") String name, String password, ModelMap modelMap,
HttpSession session){
System.out.println("doLogin");
if("root".equals(name)&&"123".equals(password)){
session.setAttribute("username",name);
return "home";
}else {
modelMap.addAttribute("error","用户名或密码错误");
return "error";
}
}
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//判断session中username是否为null
HttpSession session=request.getSession();
if(session.getAttribute("username")==null){
//重定向到登录页
String path=request.getServletContext().getContextPath();
response.sendRedirect(path+"/login");
return false;
}
return true;
}
}
在spring-config.xml
commons-io
commons-io
2.4
commons-fileupload
commons-fileupload
1.3.3
@RequestMapping("/upload")
public String uploadFile(){
return "uploadFile";
}
org.hibernate
hibernate-validator
5.4.1.Final
1.2在实体类属性上添加校验注解
public class User {
@NotEmpty(message = "用户名不能为空")
private String username;
@Length(max=10,min=5,message = "密码长度不正确")
private String password;
private Integer age;
private Integer phone;
@Email(message = "邮箱格式不正确")
private String email;
}
1.3在控制器层,开启校验
@RequestMapping("/doReg")
public String doReg(@Valid User user, BindingResult result){
System.out.println("doReg");
if (result.hasErrors()){
List errorList = result.getAllErrors();
for(ObjectError error : errorList){
System.out.println(error.getDefaultMessage());
}
return "regist";
}
System.out.println(user);
return "redirect:login";
}