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. 远程分支删除后,git branch -a还能看到的解决方法

    详情https://www.cnblogs.com/wangiqngpei557/p/6058115.html 大家在删除远程分支后 git branch -a 还是可以看到已删除的远程分支,时间一长 ...

  2. YGGL.sql

    (将表复制粘贴至记事本,再用source命令导入到数据库中) CREATE TABLE `departments` ( `部门编号` char(3) NOT NULL COMMENT '部门编号', ...

  3. day121:MoFang:植物的状态改动(幼苗→成长期)&植物的浇水功能

    目录 1.当果树种植以后在celery的异步任务中调整浇水的状态 2.客户端通过倒计时判断时间,显示浇水道具 3.客户端判断当前种植物状态控制图标的显示和隐藏 4.当用户单击浇水图标, 则根据当前果树 ...

  4. 【Flutter】可滚动组件之GridView

    前言 GridView可以构建一个二维网格列表.需要关注的是gridDelegate参数,类型是SliverGridDelegate,它的作用是控制GridView子组件如何排列(layout).Sl ...

  5. redis之集群一:主从

    Redis的三种集群模式 Redis有三种集群模式,第一个就是主从模式,第二种"哨兵"模式,第三种是Cluster集群模式,第三种的集群模式是在Redis 3.x以后的版本才增加进 ...

  6. 【Linux】linux的所有文件分类解析

    今天看书的时候,无意间看到/dev/文件夹,以前没注意,今天去看了下发现,很多文件的开头文件属性都是一些不怎么见到的 常见的是   -     这个是代表文件,可以vim编辑的 d     这个是代表 ...

  7. poj-DNA排序

    描述 现在有一些长度相等的DNA串(只由ACGT四个字母组成),请将它们按照逆序对的数量多少排序. 逆序对指的是字符串A中的两个字符A[i].A[j],具有i < j 且 A[i] > A ...

  8. mount: /dev/sdxx already mounted or /xxxx busy解决方法

    异常现象: 解决方法: 1.    輸入root的密碼,進入單用戶2.    重新掛載/目錄,使其變為可讀可寫 # mount –o rw,remount / 3.    修改/etc/fstab文件 ...

  9. MySQL全面瓦解19:游标相关

    定义 我们经常会遇到这样的一种情况,需要对我们查询的结果进行遍历操作,并对遍历到的每一条数据进行处理,这时候就会使用到游标. 所以:游标(Cursor)是处理数据的一种存储在MySQL服务器上的数据库 ...

  10. 基于 WebRTC 实现自定义编码分辨率发送

    2020年如果问什么技术领域最火?毫无疑问:音视频.2020年远程办公和在线教育的强势发展,都离不开音视频的身影,视频会议.在线教学.娱乐直播等都是音视频的典型应用场景. 更加丰富的使用场景更需要我们 ...