【问题标题】:How to call ModelAndView method or getting reference to it in JSP page?如何在 JSP 页面中调用 ModelAndView 方法或获取对它的引用?
【发布时间】:2014-06-11 09:08:53
【问题描述】:

我反映了如何使用扩展类通过 JSP 页面在 ModelAndView 中使用 getter 调用方法或获取属性值。

例如:

我会将 ModelAndView 类扩展为 View 类,并在那里实现一些方法来获取一些数据。

import org.springframework.web.servlet.ModelAndView;

public class View extends ModelAndView {

    private Map store = new HashMap<>() ;

    public Object getElement(String index) {
        return store.get(index);
    }

    public void setElement(String index,Object o) {
         store.put(index,o);
    }

}

现在控制器将处理即将到来的请求。

public class WelcomeController extends AbstractController {

    private String viewName;

    @Override
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        View v = new View("example.jsp");
        v.setElement("test", "Hello im test!");
        return v

        // I would not use here method addObject();
        // how put reference to jsp page of View (ModelAndView)
        // code like this return StackOverflowError
        // v.addObject("reference_to_view",v);
    }

}

什么都没有,除了从 JSP 页面中的 View 获取我的字符串测试:

example.jsp

<body>
    <h1>WELCOME</h1>
    // how can i here call method getElement of object View.
    ${this.getElement("test")}
    ${getElement("test")}
</body>

我如何调用方法 getElement();从 WelcomeController 返回的对象 View

【问题讨论】:

  • 我认为在 JSP 中你不能访问整个 ModelAndView 只是它的 ModelMap 的项目,所以你必须使用 addObject(..) 将你的商店添加到它

标签: java jsp spring-mvc


【解决方案1】:

从 JSP 中调用方法并不容易。这就是 Spring 中的 ModelAndView 的原因:您提供了对视图的引用,并使用可从视图轻松访问的属性填充模型。

public class WelcomeController extends AbstractController {

    private String viewName;

    @Override
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        ModelAndView mav = new ModelAndView("example.jsp");
        mav.addObject("test", "Hello im test");
        return mav;
    }
}

然后在您的视图中,您可以简单地访问 sub-jacent 模型中的任何对象:

<body>
    <h1>WELCOME</h1>
    ${test}
</body>

如果您真的想使用您的元素构造来模仿 Spring ModelMap,最简单的方法是使用 v.addObject("element", store); 将您自己的地图暴露给 Spring 模型。然后在你的 JSP 中,你可以这样做:

<body>
    <h1>WELCOME</h1>
    // how can i here call method getElement of object View.
    ${element["test"]}
</body>

【讨论】:

    【解决方案2】:

    也许我错了,但你不能定义你的控制器吗:

    @ManagedBean(name = WelcomeController.CONTROLLER_NAME)
    public class WelcomeController extends AbstractController {
    
    public static final String CONTROLLER_NAME = "welcomeController";
    ..
    public String calculateSomeValue() {
       return "Hello";
    }
    }
    

    并在jsf页面中使用它:

    <body>
        <h1>WELCOME</h1>
        ${welcomeController.calculateSomeValue()}
    </body>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 2013-02-14
      • 1970-01-01
      • 2016-05-16
      • 1970-01-01
      相关资源
      最近更新 更多