Java框架 SpringMVC--域对象共享数据

1、使用ServletAPI向request域对象共享数据

Java代码:

@Controller
@RequestMapping("/test/scope")
public class TestScopeController {

    @RequestMapping("/servlet")
    public String testScopeByServletAPI(HttpServletRequest request) {
        request.setAttribute("testRequestScope", "hello servlet");
        return "target";
    }
}

index.html代码:




    
    首页


    

首页

测试ServletAPI向request域对象共享数据

target.html 代码:




    
    Target


    

Hello World

点击 index.html 中的超链接,结果为:

Java框架 SpringMVC--域对象共享数据_第1张图片

 

2、使用ModelAndView向request域对象共享数据

Java代码:

    @RequestMapping("/modelAndView")
    public ModelAndView testScopeByModelAndView() {
        /**
         * ModelAndView有Model和View的功能
         * Model主要用于向请求域共享数据
         * View主要用于设置视图,实现页面跳转
         */
        ModelAndView mav = new ModelAndView();
        // 向请求域共享数据
        mav.addObject("testScopeByModelAndView", "hello testScopeByModelAndView");
        // 设置视图,实现页面跳转
        mav.setViewName("target");
        return mav;
    }

index.html代码:

    测试 modelAndView 向 request 域对象共享数据

点击 index.html 中的目标超链接,结果为:

Java框架 SpringMVC--域对象共享数据_第2张图片

 

3、使用Model、ModelMap、Map向request域对象共享数据

Java代码:

    @RequestMapping("/model")
    public String testScopeByModel(Model model) {
        System.out.println(model.getClass().getName());
        // org.springframework.validation.support.BindingAwareModelMap
        model.addAttribute("testRequestScope", "hello model");
        return "target";
    }

    @RequestMapping("/modelMap")
    public String testScopeByModel(ModelMap modelMap) {
        System.out.println(modelMap.getClass().getName());
        // org.springframework.validation.support.BindingAwareModelMap
        modelMap.addAttribute("testRequestScope", "hello ModelMap");
        return "target";
    }

    @RequestMapping("/map")
    public String testScopeByMap(Map map) {
        System.out.println(map.getClass().getName());
        // org.springframework.validation.support.BindingAwareModelMap
        map.put("testRequestScope", "hello map");
        return "target";
    }

index.html代码:

    测试 Model 向 request 域对象共享数据
测试 ModelMap 向 request 域对象共享数据
测试 Map 向 request 域对象共享数据

ModelModelMapMap类型的参数其实本质上都是 BindingAwareModelMap 类型的

public interface Model{}
public class ModelMap extends LinkedHashMap {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}

4、向session域共享数据

Java代码:

    @RequestMapping("/session")
    public String testSession(HttpSession session) {
        session.setAttribute("testSessionScope","hello session");
        return "target";
    }

index.html代码:

    测试向 session 域对象共享数据

target.html代码:


    

Hello World

点击 index.html 中的对应的目标超链接,结果为:

Java框架 SpringMVC--域对象共享数据_第3张图片

5、向application域共享数据

Java代码:

    @RequestMapping("/application")
    public String testApplication(HttpSession session) {
        ServletContext application = session.getServletContext();
        application.setAttribute("testApplication", "hello application");
        return "target";
    }

index.html代码:

    测试向 application 域对象共享数据

target.html代码:


    

Hello World

点击 index.html 中的对应的目标超链接,结果为:

Java框架 SpringMVC--域对象共享数据_第4张图片

 

你可能感兴趣的:(Java,#,Java框架,java,servlet,开发语言)