完整的项目案例: springmvc.zip

目录

实例

项目结构:

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"> <!-- 请求总控器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

配置dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="edu.nf.ch05.controller"/> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> </beans>

Controller

1、(ModelAttributeController)

package edu.nf.ch05.controller;

import edu.nf.ch05.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; /**
* @author wangl
* @date 2018/10/30
*/
@Controller
public class ModelAttributeController { /**
* @ModelAttribute标注的方法都会在controller方法执行前先执行
* 其实就是在执行请求方法前,先将一些数据放入到Model中
* @return
*/
@ModelAttribute("user")
public Users getUsers(){
Users user = new Users();
user.setUserName("user5");
user.setAge(25);
return user;
} /**
* 在执行test4之前,先执行getUsers()方法
* @return
*/
@GetMapping("/test4")
public ModelAndView test4(){
return new ModelAndView("index2");
} /**
* 在方法参数前标注@ModelAttribute,表示映射参数后将该参数存入Model中
*
* 注意:在执行test5之前,同样先执行getUsers()方法,
* 如果Model中已经存在相同key的对象,那么映射的参数就会覆盖
* 原先Model中的数据
* @param user
* @return
*/
@PostMapping("/test5")
public ModelAndView test5(@ModelAttribute("user") Users user){
return new ModelAndView("index2");
}
}

2、(ModelController)

package edu.nf.ch05.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView; import java.util.HashMap;
import java.util.Map; /**
* @author wangl
* @date 2018/10/30
* 使用Model、ModelMap、Map来绑定数据
* 注意:这个三个类在运行时都是同一个对象
*/
@Controller
public class ModelController { /**
* 使用ModelAndView将数据加入到model中
* @return
*/
@GetMapping("/test")
public ModelAndView test(){
ModelAndView mv = new ModelAndView("index1");
//将数据添加到Model中(其实就是放入请求作用域)
//mv.addObject("userName", "user1");
//mv.addObject("age", 21);
//将数据封装到一个Map中,然后再将map存入Model
Map<String, Object> modelMap = new HashMap<>();
modelMap.put("userName", "user2");
modelMap.put("age", 22);
mv.addAllObjects(modelMap);
return mv;
} /**
* 由Spring传入一个map对象,将数据直接存入这个map中
* @param map
* @return
*/
@GetMapping("/test1")
public ModelAndView test1(Map<String, Object> map){
map.put("userName", "user3");
map.put("age", 23);
return new ModelAndView("index1");
} /**
* 也可以使用Spring提供的ModelMap对象,
* 然后传递给ModelAndView
* @param map
* @return
*/
@GetMapping("/test2")
public ModelAndView test1(ModelMap map){
map.put("userName", "user4");
map.put("age", 24);
return new ModelAndView("index1");
} /**
* 也可以使用Spring提供的Model对象,直接将数据存入Model中
* @param model
* @return
*/
@GetMapping("/test3")
public ModelAndView test2(Model model){
model.addAttribute("userName", "user4");
model.addAttribute("age", 24);
return new ModelAndView("index1");
}
}

3、(SessionAttributesController)

package edu.nf.ch05.controller;

import edu.nf.ch05.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView; /**
* @author wangl
* @date 2018/10/30
*/
@Controller
/**
* 将model中对象存入会话作用域,value属性对应model的key,
* 并且可以存放多个key,它是一个数组,例如:
* @SessionAttributes({"user","user2"})
* 注意:能放入会话作用域的只能标注了@ModelAttribute的方法返回值,
* 以及手动存入Map、Model、ModelMap中的值
*/
@SessionAttributes({"addr","user"})
public class SessionAttributesController { @ModelAttribute("user")
public Users getUsers(){
Users user = new Users();
user.setUserName("user5");
user.setAge(25);
return user;
} /**
* 此方法转发到index3.jsp后可以从请求和会话作用域中取值
* @return
*/
@GetMapping("/test6")
public ModelAndView test6(Model model){
//注意:如果类上修饰了@SessionAttributes,并且也指定了addr的键,
//那么addr的值也会存入会话作用域
model.addAttribute("addr","zhuhai");
return new ModelAndView("index3");
} /**
* 如果类上修饰了@SessionAttributes并且指定了user的键,同时方法参数上
* 修饰了@ModelAttribute并且也指定了user的键,那么此时会从会话作用域中
* 获取一个一个user的值存入model中
* 另外,当有表单映射数据时会覆盖原有的model中的user对象信息和会话作用域中的user对象信息
* @param user
* @return
*/
@PostMapping("/test7")
public ModelAndView test7(@ModelAttribute("user") Users user){
return new ModelAndView("index3");
}
}

前台页面

(adduser.html)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>测试覆盖ModelAttribute</h1>
<form method="post" action="test5">
Name:<input type="text" name="userName"/><br/>
Age:<input type="text" name="age"/><br/>
<input type="submit" value="submit"/>
</form>
<h1>测试覆盖SessionAttributes</h1>
<form method="post" action="test7">
Name:<input type="text" name="userName"/><br/>
Age:<input type="text" name="age"/><br/>
<input type="submit" value="submit"/>
</form>
</body>
</html>

index1.jsp

<%--
Created by IntelliJ IDEA.
User: wangl
Date: 2018/10/30
Time: 15:44
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${requestScope.userName},${requestScope.age}<br/>
</body>
</html>

index2.jsp

<%--
Created by IntelliJ IDEA.
User: wangl
Date: 2018/10/30
Time: 15:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${user.userName},${user.age}
</body>
</html>

index3.jsp

<%--
Created by IntelliJ IDEA.
User: wangl
Date: 2018/10/30
Time: 15:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>请求作用域</h2>
${requestScope.user.userName},${requestScope.user.age}
<h2>会话作用域</h2>
${sessionScope.user.userName},${sessionScope.user.age}
</body>
</html>

Spring MVC Spring中的Model (五)的更多相关文章

  1. spring mvc EL ModelAndView的 Model 值 在jsp中不显示

    问题:spring mvc开发过程中, 经常会给model addAttribute, 然后通过EL在jsp中显示,比如 ${msg}, 但是有时候会出现jsp最后显示的还是${msg},而不是msg ...

  2. spring mvc controller中获取request head内容

    spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...

  3. Spring MVC Controller中解析GET方式的中文参数会乱码的问题(tomcat如何解码)

    Spring MVC Controller中解析GET方式的中文参数会乱码的问题 问题描述 在工作上使用突然出现从get获取中文参数乱码(新装机器,tomcat重新下载和配置),查了半天终于找到解决办 ...

  4. Spring MVC程序中得到静态资源文件css,js,图片文件的路径问题总结

    上一篇 | 下一篇 Spring MVC程序中得到静态资源文件css,js,图片 文件的路径 问题总结 作者:轻舞肥羊 日期:2012-11-26 http://www.blogjava.net/fi ...

  5. Spring MVC -- Spring MVC入门

    本篇博客首先介绍Spring MVC的优点,然后介绍Spring MVC的基本组件,包括DispatcherServlet,并学习如何开发一个“传统风格”的控制器,这是在Spring 2.5版本之前开 ...

  6. Spring MVC+Spring +Hibernate配置事务,但是事务不起作用

    最近做项目,被一个问题烦恼了很久.使用Spring MVC+Spring +Hibernate开发项目,在使用注解配置事务管理,刚开始发现无论如何数据库都无法更新,但是可以从数据库查询到数据.怀疑是配 ...

  7. freemarker + spring mvc + spring + mybatis + mysql + maven项目搭建

    今天说说搭建项目,使用freemarker + spring mvc + spring + mybatis + mysql + maven搭建web项目. 先假设您已经配置好eclipse的maven ...

  8. Spring MVC + Spring + Mybitis开发Java Web程序基础

    Spring MVC + Spring + Mybitis是除了SSH外的另外一种常见的web框架组合. Java web开发和普通的Java应用程序开发是不太一样的,下面是一个Java web开发在 ...

  9. Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建

    目录 Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建 0.项目准备 1.数据持久层Mybatis+MySQL 1.1 MySQL数据准备 1.2 Mybatis ...

  10. velocity+spring mvc+spring ioc+ibatis初试感觉(与struts+spring+hibernate比较)

    velocity+spring mvc+spring ioc+ibatis框架是我现在公司要求采用的,原因是因为阿里巴巴和淘宝在使用这样的框架,而我公司现在还主要是以向阿里巴巴和淘宝输送外派人员为 主 ...

随机推荐

  1. 【EF6学习笔记】(三)排序、过滤查询及分页

    本篇原文地址:Sorting, Filtering, and Paging 说明:学习笔记参考原文中的流程,为了增加实际操作性,并能够深入理解,部分地方根据实际情况做了一些调整:并且根据自己的理解做了 ...

  2. vue-14-less 语法的使用

    vue-15-rem-less 在计算手机端页面的时候, 使用rem和less的方式, 可以方便的设置需要的大小等 1, 在index.html中添加rem的script 代码 在head中添加 &l ...

  3. Nginx 配置https 服务

    一.HTTPS 服务 为什么需要HTTPS? 原因:HTTP不安全 1.传输数据被中间人盗用.信息泄露 2.数据内容劫持.篡改 HTTPS协议的实现 对传输内容进行加密以及身份验证 HTTPS加密校验 ...

  4. MYSQL事务隔离级别详解附加实验

    参考: https://dev.mysql.com/doc/refman/5.7/en/set-transaction.html http://xm-king.iteye.com/blog/77072 ...

  5. WEB安全之垃圾信息防御措施

    防止垃圾评论与机器人的攻击手段如下: 1)IP限制.其原理在于IP难以伪造.即使是对于拨号用户,虽然IP可变,但这也会大大增加共攻击的工作量. 2)验证码.其重点是让验证码难于识别,对于“字母+数字” ...

  6. java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.SpecialProvider.<init>()

    Caused by: org.apache.ibatis.builder.BuilderException: Error invoking SqlProvider method (tk.mybatis ...

  7. mybatis逆向工程(MyBatis Generator)

    mybatis逆向工程(MyBatis Generator) 1. 什么是mybatis逆向工程 mybatis官方为了提高开发效率,提高自动对单表生成sql,包括 :mapper.xml.mappe ...

  8. 关于div容器在ie6下默认高度不为0(存在默认高度)

    最近做项目的时候遇到一个问题,相信很多人都遇到过,就是在测试兼容性的时候,在ie6下小于12px 的背景的高度不等于原高,或许这样说你可能不是很明白,那就举个例子吧! 如图所示: 锯齿状的背景图本来是 ...

  9. c# partial 关键字的使用

    C# 2.0 引入了局部类型的概念.局部类型允许我们将一个类.结构或接口分成几个部分,分别实现在几个不同的.cs文件中. 局部类型适用于以下情况: (1) 类型特别大,不宜放在一个文件中实现.(2) ...

  10. Discuz网警过滤关键词库

    积累近几年discuz关键词过滤 使用方法:1.进入后台/内容/词语过滤批量添加.2.打开CensorWords.txt,复制里面的文本信息到批量添加的输入框内,点击确定即可.如图: 关键词下载:Ke ...