Spring MVC处理模型数据

添加模型数据的方法:

假设参数中有一个User对象,我们想给它命名user2,要把它添加到模型数据中。

  1. ModelAndView :在模型当中 必然有一个名字为user的对象(还有一个user2)
  2. Model :在模型当中 必然有一个名字为user的对象还有一个user2)
  3. Map :在模型当中 必然有一个名字为user的对象还有一个user2)
  4. 直接写在参数里 :在模型当中 必然有一个名字为user的对象
  5. 套用@ModelAttribute注解 :只有一个user2

ModelAndView

(模型数据+视图)处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据。

Map及Model

import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.jredu.entity.Notice; @Controller
@RequestMapping("/model")
/**
* ModelAndView:传递下一个页面需要的数据,设置转发页面
* @author Administrator
*
*/
public class ModelController { /**
* 获取公告
*/
@RequestMapping("/m1")
public ModelAndView getNotice1(int id,ModelAndView modelAndView) {
//获取公告
//..
Notice notice=new Notice();
notice.setId(id);
notice.setTitle("本周天不上班");
notice.setContent("由于时就大召开,本周天不用上班");
//使用modelandview对象,视图名称,模型数据
modelAndView.setViewName("hello4");
//传递模型数据
modelAndView.addObject("notice", notice);
modelAndView.addObject("a","abc");
return modelAndView;
} /**
* 获取公告
*/
@RequestMapping("/m2")
public ModelAndView getNotice2(int id,ModelAndView modelAndView) {
//获取公告
//..
Notice notice=new Notice();
notice.setId(id);
notice.setTitle("本周天不上班");
notice.setContent("由于时就大召开,本周天不用上班");
//使用modelandview对象,视图名称,模型数据
modelAndView.setViewName("hello4");
//传递模型数据
Map<String, Object> map=new HashMap<String, Object>();
map.put("notice", notice);
map.put("a", "abc");
modelAndView.addAllObjects(map);
return modelAndView;
} //ModelAndView Model+String
@RequestMapping("/m3")
public String getNotice3(Model model) {
//..业务处理
Notice notice=new Notice();
notice.setId(15);
notice.setTitle("本周天不上班");
notice.setContent("由于时就大召开,本周天不用上班");
model.addAttribute("notice", notice);
model.addAttribute("a", "1234");
return "hello4";
} @RequestMapping("/m4")
public String getNotice4(Map<String, Object> map) {
Notice notice=new Notice();
notice.setId(15);
notice.setTitle("本周天不上班");
notice.setContent("由于时就大召开,本周天不用上班");
map.put("notice", notice);
map.put("a", "lalallala");
return "hello4";
} }

@SessionAttribute

import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.jredu.entity.Notice;
import com.jredu.entity.User; @Controller
@RequestMapping("/session")
//@SessionAttributes({"user","user2"})
@SessionAttributes(types={User.class,Notice.class})
/**
* @SessionAttributes("user")
* @SessionAttributes:
* 可以把模型数据当中对应的对象存储到session中
* session.setAttr("user",request.getAttr("user"))
* @author Administrator
*
*/
public class SessionController { @RequestMapping("/se1")
public String sess1(User user,Notice notice) {
return "hello6";
} /**
* 如果参数列表当中有两个类型相同的参数,只会在模型数据中存储一个
* 使用@ModelAttribute可以给两个类型相同的对象做区分,
* 让他们都可以存储在模型数据中
* 如果遇到有两个类型相同并且都需要存储到session中时,
* 可以使用model去解决问题
* @param user
* @param user2
* @return
*/
@RequestMapping("/se2")
public String sess2(User user,User user2,Model model) {
model.addAttribute("user", user);
model.addAttribute("user2", user2);
return "hello6";
} }

@ModelAttribute

在方法定义上使用@ModelAttribute,Spring MVC在调用目标处理方法前,会先逐个调用在方法上标注了@ModelAttribute的方法。

在方法的参数前使用@ModelAttribute:将方法参数对象添加到模型中。

@Controller
@RequestMapping("/attr")
/**
* @ModelAttribute的本质作用就是在模型当中添加数据
* (request.setAttribute(被@ModelAttribute所标记的对象))
* 当我们调用被@RequestMapping所标的方法时,
* 会先调用被@ModelAttribute所标记的方法
* @author Administrator
*
*/
public class ModelAttributeController { @RequestMapping("/attr1")
public String attr1(User user) {
user.setUname("xiaoli");
return "hello5";
} /**
* 处理器方法当中的参数会直接放到模型数据中
* reqeust.setAttribute(类对象名称首字母小写)
* 键的名称是类的名字首字母小写
* @param user2
* @return
*/
@RequestMapping("/attr2")
public String attr2(User user2) {
user2.setUname("laowang");
return "hello5";
} /**
* 模型数据中包含两个相同的的数据,但是名字不一样user,user3
* @param user3
* @param model
* @return
*/
@RequestMapping("/attr3")
public String attr3(User user3,Model model) {
user3.setUname("laowang");
model.addAttribute("user3", user3);
return "hello5";
} /**
* 加了@ModelAttribute的参数会把原来模型数据的名字改了,
* user-->user4
* @param user4
* @return
*/
@RequestMapping("/attr4")
public String attr4(@ModelAttribute("user4")User user4) {
user4.setUname("xiaozhang");
return "hello5";
} /**
* 在方法定义上使用@ModelAttribute:
* Spring MVC在调用目标处理方法前,
* 会先逐个调用在方法上标注了@ModelAttribute的方法
* @param user
* @return
*/
@RequestMapping("/attr5")
public String attr5() {
return "hello5";
} @ModelAttribute("user5")
public User test() {
User user=new User();
user.setUname("admin");
return user;
} @ModelAttribute("user6")
public User test6() {
User user=new User();
user.setUname("admin2");
return user;
} @ModelAttribute("user7")
public User test7() {
User user=new User();
user.setUname("admin3");
return user;
} @RequestMapping("/attr6")
public String attr6(
@ModelAttribute("user5")User user5,
@ModelAttribute("user6")User user6,
@ModelAttribute("user7")User user7) {
user5.setUname("admin");
user6.setUname("admin2");
user7.setUname("admin3");
return "hello5";
} /**
* 添加模型数据的方法
* 假设现在参数中有一个User对象,
* 我们想给他命名user2,
* 要把它添加到模型数据中
* 1.ModelAndView (在模型当中必然有一个名字叫user的对象,还有一个user2)
* 2.Model (在模型当中必然有一个名字叫user的对象,还有一个user2)
* 3.Map (在模型当中必然有一个名字叫user的对象,还有一个user2)
* 4.直接写在参数里 (在模型当中必然有一个名字叫user的对象)
* 5.套用@ModelAttribute注解(只有一个user2)
*/ }

Spring MVC转发和重定向

转发和重定向:返回字符串中含有:

  • Forward
  • redirect
package com.jredu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/redi")
public class RediController { @RequestMapping("/r1")
public String redi1(){ return "redirect:../redi.jsp";
}
//重定向的相对路径是相对于/redi这个路径。
//redi是一个虚拟路径,使用相对路径..之后,回到WebRoot路径下,进行重定向。
//浏览器端无法访问WEB-INF目录下的文件。 @RequestMapping("/r2")
public String redi2(){ return "forward:../for.jsp";
} //通过转发跳到一个新的页面,新的页面在进行跳转,则新页面跳转的路径是转发前的路径。
}

Spring MVC静态资源处理方式

方式一:采用Servlet容器中默认的Servlet进行处理。在Web.xml中配置。

在DispatcherServlet前面配置,激活容器默认的Servlet。

方式二:<mvc:resources /> 根据路径来配置。在servlet.xml中配

<mvc:resources location=”/img/” mapping=” /img/* ” >

</mvc:sources>

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:p="http://www.springframework.org/schema/p"
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">
<!-- 配置springmvc自动扫描的包 -->
<context:component-scan base-package="com.jredu.controller">
<!-- 可以配置过滤不需要的文件或需要的文件 -->
</context:component-scan>
<!-- 设置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/pages/"
p:suffix=".jsp"/>
<mvc:default-servlet-handler />
<mvc:annotation-driven>
</mvc:annotation-driven>
</beans>

方式三:所有非MVC管理的组件都经过default-mvc来处理。

除了控制器一概不管理。主要添加注解驱动。

     <mvc:default-servlet-handler />
<mvc:annotation-driven>
</mvc:annotation-driven>

Spring MVC—模型数据,转发重定向,静态资源处理方式的更多相关文章

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

    问题描述 在用springmvc开发应用程序的时候.对于像我一样的初学者,而且还是自学的人,有一个很头疼的问题.那就是数据都已经查出来了,但是页面的样式仍然十分简陋,加载不了css.js,图片等资源文 ...

  2. spring mvc:拦截器不拦截静态资源的三种处理方式

    方案一.拦截器中增加针对静态资源不进行过滤(涉及spring-mvc.xml) <mvc:resources location="/" mapping="/**/* ...

  3. spring mvc 自定义handler不拦截静态资源

    处理静态资源的handler和处理Controller请求中的handler不同,对应的Interceptor也不同 查找对应handler是在DispatcherServlet中 因此一些自定义的I ...

  4. Nginx与Tomcat实现请求动态数据与请求静态资源的分离

    上篇博客说明了Nginx在应用架构中的作用,以及负载均衡的思路.这篇实践一下其中的访问静态资源与访问动态资源的操作. 一.认识访问静态资源与访问动态资源的区别 静态资源:指存储在硬盘内的数据,固定的数 ...

  5. Spring MVC使用ModelAndView进行重定向

    1.Servlet重定向forward与redirect: 使用servlet重定向有两种方式,一种是forward,另一种就是redirect.forward是服务器内部重定向,客户端并不知道服务器 ...

  6. Spring MVC使用ModelAndView进行重定向(转)

    1.Servlet重定向forward与redirect: 使用servlet重定向有两种方式,一种是forward,另一种就是redirect.forward是服务器内部重定向,客户端并不知道服务器 ...

  7. Spring MVC 前后台数据交互

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址地址:<Spring MVC 前后台数据交互> 1.服务端数据到客户端 (1)返回页面,Controller中方法 ...

  8. 0060 Spring MVC的数据类型转换--ConversionService--局部PropertyEditor--全局WebBindingInitializer

    浏览器向服务器提交的数据,多是字符串形式,而有些时候,浏览器需要Date.Integer等类型的数据,这时候就需要数据类型的转换器 使用Spring的ConversionService及转换器接口 下 ...

  9. 0061 Spring MVC的数据格式化--Formatter--FormatterRegistrar--@DateTimeFormat--@NumberFormat

    Converter只完成了数据类型的转换,却不负责输入输出数据的格式化工作,日期时间.货币等虽都以字符串形式存在,却有不同的格式. Spring格式化框架要解决的问题是:从格式化的数据中获取真正的数据 ...

随机推荐

  1. Python输出HelloWorld(以及环境配置)

    java usebean 用法: https://blog.csdn.net/zhang150114/article/details/89434617

  2. Java学习日报7.23

    import java.util.Scanner;public class LandP {public static void main(String args[]) { System.out.pri ...

  3. 51 张图助你彻底掌握 HTTP!

    前言 如果说 TCP/IP 协议是互联网通信的根基,那么 HTTP 就是其中当之无愧的王者,小到日常生活中的游戏,新闻,大到双十一秒杀等都能看到它的身影,据 NetCraft 统计,目前全球至少有 1 ...

  4. swap是干嘛的?

    本文截取自:http://hbasefly.com/2017/05/24/hbase-linux/ swap是干嘛的? 在Linux下,SWAP的作用类似Windows系统下的"虚拟内存&q ...

  5. ARM杂散知识

    画重点: 1.存储器格式:重点是大小端识别 经常考 2.对齐后结构体占用空间大小:使用aligned,packed,#pragma pack()三种方式都要会 Thumb指令集 Thumb指令集能够以 ...

  6. #3使用html+css+js制作网页 番外篇 使用python flask 框架 (I)

    #3使用html+css+js制作网页 番外篇 使用python flask 框架(I 第一部) 0. 本系列教程 1. 准备 a.python b. flask c. flask 环境安装 d. f ...

  7. Head First 设计模式 —— 08. 外观 (Facade) 模式

    思考题 想想看,你在 JavaAPI 中遇到过哪些外观,你还希望 Java 能够新增哪些外观? P262 println.log 日志接口.JDBC 接口 突然让想感觉想不出来,各种 API 都用得挺 ...

  8. 一文详解 ARP 协议

    我把自己以往的文章汇总成为了 Github ,欢迎各位大佬 star https://github.com/crisxuan/bestJavaer 公众号连载计算机网络文章如下 ARP,这个隐匿在计网 ...

  9. 【C++】《C++ Primer 》第十五章

    第十五章 面向对象程序设计 一.OOP:概述 面向对象程序设计(OOP)的核心思想是数据抽象.继承和动态绑定. 通过使用数据抽象,可以将类的接口和实现分离. 使用继承,可以定义相似的类型并对其相似关系 ...

  10. Java API 操作HBase Shell

    HBase Shell API 操作 创建工程 本实验的环境实在ubuntu18.04下完成,首先在改虚拟机中安装开发工具eclipse. 然后创建Java项目名字叫hbase-test 配置运行环境 ...