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. 学习DOS,个人笔记

    在win中\表示根目录,  在linux中/表示根目录         注意: 有些家庭版的系统会选择性的调用命令的,有的命令虽然有那个文件,但是不能使用.....     dir 命令   英语全称 ...

  2. sqlite嵌入式数据库简介及特性

    p.p1 { margin: 0; font: 12px "Helvetica Neue"; color: rgba(69, 69, 69, 1) } p.p2 { margin: ...

  3. ECMAScript概述及浅谈const,let与块级作用域

    ECMAScript可以看作javascript的标准规范,实际上javascript是ECMAScript的一门脚本语言,ECMAScript只提供了最基本的语言JavaScript对ECMAScr ...

  4. LeetCode 371两数之和

    题目描述: 不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a .b ​​​​​​​之和. 思路: 既然不能使用运算符操作就要考虑到,位运算的加法. 加法有进位的时候和不进位的时候 ...

  5. MyISAM与InnoDB两者之间区别与选择(转)

    Mysql在V5.1之前默认存储引擎是MyISAM:在此之后默认存储引擎是InnoDB MyISAM:默认表类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Acces ...

  6. Git软件安装过程

    Git程序安装过程 官网: https://git-scm.com/ 下载: https://git-scm.com/downloads 我的操作系统是 Windows + 64位的 https:// ...

  7. 【Linux】salt的cmd.script命令介绍

    salt是一个很棒的自动化运维工具之一,常用的有cmd.run,今天介绍的是cmd.script 其实一眼就能看出这个命令是执行脚本的命令 具体操作如下: 1.将/etc/salt/master中的 ...

  8. 【TNS】TNS-00515 TNS-12560 TNS-12545解决方案

    今天同事的plsql连接不上数据库,我用他的本地tnsping是不通的,于是上服务器上查看下,结果发现监听没起来,不知道怎么就断了 再次尝试重启 lsnrctl start 发现直接报错: NSLSN ...

  9. VL02N发货过账BAPI

    使用BAPI函数: BAPI_OUTB_DELIVERY_CONFIRM_DEC 进行delivery的发货过账,可能会有如此的需求,就是修改实际的发货日期.规划的GI.交货日期.装载日期.传输计划日 ...

  10. 阅读lodash源码之旅数组方法篇-compact和concat

    鲁迅说过:只有阅读过优秀库源码的人,才能配的上是真正的勇士. compact 创建一个新数组,包含原数组中所有的非假值元素.例如false, null,0, "", undefin ...