zoukankan      html  css  js  c++  java
  • Spring MVC—模型数据,转发重定向,静态资源处理方式


    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>
  • 相关阅读:
    java数据库编程之DAO模式
    java数据库编程之JDBC
    java数据库编程之初始Mysql
    java数据库编程之常用的操作用户和赋权限
    java数据库编程之事务、视图、索引、备份、恢复
    java数据库编程之嵌套子查询及exists的使用
    .net窗体程序的基础知识及详细笔记
    sql sever 基础知识及详细笔记
    java基础知识及详细笔记
    计算机基础知识及笔记
  • 原文地址:https://www.cnblogs.com/aixing/p/13327653.html
Copyright © 2011-2022 走看看