spring mvc 异常处理和session添加
在controller中进行设置
package com.stone.controller; import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
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.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.view.InternalResourceViewResolver; import com.stone.model.User; @Controller
@RequestMapping("/user")
@SessionAttributes("loginUser")//需要处理的session
public class UserController { private final static Map<String, User> users = new HashMap<String, User>(); public UserController() {
users.put("ldh", new User("ldh", "刘德华", "123", "123@123.com"));
users.put("zxy", new User("zxy", "张学友", "123", "123@123.com"));
users.put("gfc", new User("gfc", "郭富城", "123", "123@123.com"));
users.put("lm", new User("lm", "黎明", "123", "123@123.com"));
} @RequestMapping("/users")
public String list(Model model) {
model.addAttribute("users", users);
return "user/list";
} @RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
return "user/login";
} @RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(String username, String password, Model model) {
if (!users.containsKey(username)) {
throw new RuntimeException("用户名不存在!");
}
if (!password.equals(users.get(username).getPassword())) {
throw new RuntimeException("用户密码不正确");
}
// 进行session的设置
model.addAttribute("loginUser", users.get(username));
return "redirect:/user/users/";
}
//进行异常处理
@ExceptionHandler(value = { RuntimeException.class })
public String handlerException(Exception ex, HttpServletRequest req) {
req.setAttribute("ex", ex);
return "error";
}
}
spring mvc 异常处理和session添加的更多相关文章
- 理解Spring MVC Model Attribute和Session Attribute
作为一名 Java Web 应用开发者,你已经快速学习了 request(HttpServletRequest)和 session(HttpSession)作用域.在设计和构建 Java Web 应用 ...
- 【译】理解Spring MVC Model Attribute 和 Session Attribute
作为一名 Java Web 应用开发者,你已经快速学习了 request(HttpServletRequest)和 session(HttpSession)作用域.在设计和构建 Java Web 应用 ...
- JavaWeb:Cookie处理和Session跟踪
JavaWeb:Cookie处理和Session跟踪 Cookie处理 什么是Cookie Cookie 是存储在客户端计算机上的文本文件,保留了各种跟踪信息.因为HTTP协议是无状态的,即服务器不知 ...
- 【Java Web开发学习】Spring MVC异常统一处理
[Java Web开发学习]Spring MVC异常统一处理 文采有限,若有错误,欢迎留言指正. 转载:https://www.cnblogs.com/yangchongxing/p/9271900. ...
- spring mvc 异常统一处理方式
springMVC提供的异常处理主要有两种方式: 一种是直接实现自己的HandlerExceptionResolver: 另一种是使用注解的方式实现一个专门用于处理异常的Controller——Exc ...
- C++复习8.异常处理和RTTI
C++异常处理和RTTI技术 20130930 1.异常处理的基本知识 C语言中是没有内置运行时错误处理机制,对于错误发生的时候使用的几种处理机制: 函数返回彼此协商后统一定义的状态编码来表示操作成功 ...
- C异常处理和C++异常处理的对比
每一种编译器实现异常处理的方式会有所不同,但是都是基于Windows的SEH异常处理.这里以MSC编译器为例. C异常处理 #include <Windows.h> int main(in ...
- java面试题之----spring MVC的原理和MVC
1.什么是mvc? 1.1原始比较初级的设计模式: 1.2 MVC设计模式 2MVC设计模式的优势与核心在于其能解耦和: 传统的设计模式相当于是一个串联的设计,只要其中一个环节出了问题便会使下一环节中 ...
- spring mvc下shiro的session,request等问题
最近的一个项目使用的是spring mvc,权限框架使用的是shiro. 不过有一个问题一直困扰着我,现在的session到底是谁的session,是servlet的还是shiro的. 于是我把spr ...
随机推荐
- 设置自己Eclipse代码风格(内部)
经过这几次的代码提交,发现很多人的代码风格不够规范.个人认为很有必要强制性规定一下代码的规范. 整体来说,有三种代码风格,其中两种类似于这样的: public void function(){ //f ...
- opencart 图片管理器 500错误
网站点击文件夹打不开返回500错误,通过ftp删除大于1M的图片文件即可
- js浏览器兼容
//window.event IE:有window.event对象 FF:没有window.event对象.可以通过给函数的参数传递event对象.如onmousemove=doMouseMo ...
- 图解SQL的各种连接join
对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...
- ural2014 Zhenya moves from parents
Zhenya moves from parents Time limit: 1.0 secondMemory limit: 64 MB Zhenya moved from his parents’ h ...
- Xcode的管理工具
Xcode插件管理工具Alcatraz Alcatraz 1.简介 Alcatraz是一个能帮你管理Xcode插件丶模版及颜色配置的工具.它可以直接集成在Xcode的图形界面中,让你感觉就像在使用Xc ...
- 1、Sencha cmd学习笔记(一) 使你的sencha cmd跑起来
带着Ext JS 5来使用sencha cmd ------------------------------------------------------------------- 这个指导通过处 ...
- (中等) UESTC 360 Another LCIS ,线段树+区间更新。
Description: For a sequence S1,S2,⋯,SN, and a pair of integers (i,j), if 1≤i≤j≤N and Si<Si+1<S ...
- NIO 入门基础
输入/输出:概念性描述 I/O 简介 I/O ? 或者输入/输出 ? 指的是计算机与外部世界或者一个程序与计算机的其余部分的之间的接口.它对于任何计算机系统都非常关键,因而所有 I/O 的主体实际上是 ...
- bzoj 4383: [POI2015]Pustynia
复习了一下线段树优化建图的姿势,在线段树上连边跑拓扑排序 这题竟然卡vector……丧病 #include <bits/stdc++.h> #define N 1810000 using ...