zoukankan      html  css  js  c++  java
  • 简单后台登录逻辑实现Controller

    package com.fei.controller.admin;
    
    import javax.servlet.http.HttpSession;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.mvc.support.RedirectAttributes;
    
    import com.fei.po.User;
    import com.fei.service.UserService;
    
    /**
     * Created by zxf on 2019年9月30日
     */
    @Controller
    @RequestMapping("/admin")
    public class LoginController {
    
    	@Autowired
    	private UserService userService;
    
    	/**
    	 * 登录方法
    	 * 
    	 * @param username
    	 * @param password
    	 * @param session
    	 * @param attributes
    	 * @return
    	 */
    	@PostMapping("/login")
    	public String login(@RequestParam String username, @RequestParam String password, HttpSession session,
    			RedirectAttributes attributes) {
    
    		User user = userService.login(username, password);
    
    		if (user != null) {
    			user.setPassword(null);
    			session.setAttribute("user", user);
    
    			return "redirect:/admin/index";
    		} else {
    			attributes.addFlashAttribute("message", "用户名或密码错误!");
    			return "redirect:/admin";
    		}
    	}
    
    	/**
    	 * 注销方法
    	 * 
    	 * @param session
    	 * @return
    	 */
    	@PostMapping("/logout")
    	public String logout(HttpSession session) {
    		session.removeAttribute("user");
    		return "redirect:/admin";
    	}
    
    	/**
    	 * 去登录页
    	 * 
    	 * @return
    	 */
    	@GetMapping
    	public String toLogin() {
    		return "admin/login";
    	}
    
    	/**
    	 * 去后台首页
    	 * 
    	 * @return
    	 */
    	@GetMapping("/index")
    	public String toIndex() {
    		return "admin/index";
    	}
    
    }
    

    错误描述

    org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
    	at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:200) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    

    错误分析

    错误原因:可能是表单的提交方式为默认的get请求,而后台处理该请求的Controller处理的是PostMapping,两者不一致就会报该错误。

  • 相关阅读:
    用继承和组合的知识构造一辆汽车,功能需求见注释
    应用组合的方式实现继承关系
    PL/SQL 07 触发器 trigger
    PL/SQL 05 存储过程 procedure
    PL/SQL 04 游标 cursor
    PL/SQL 03 流程控制
    PL/SQL 02 声明变量 declare
    PL/SQL 01 代码编写规则
    Oracle基础 12 对象 objects 同义词/序列/试图/索引
    Oracle基础 11 约束 constraints
  • 原文地址:https://www.cnblogs.com/zxfei/p/11614363.html
Copyright © 2011-2022 走看看