如何从控制器中获取价值? [英] How to retrive value from controller in view?

查看:74
本文介绍了如何从控制器中获取价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring框架.
这是我的控制器:

I am using Spring framework.
Here is my controller:

@RequestMapping("/com/index.do")
public String index(ModelMap model) throws Exception {
	MyClass obj=new MyClass();
	model.addAttribute("obj",obj);
	return "/com/index";
}


这是我的看法:


Here is my view:

<%@page contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"%>
<%@page import="org.springframework.ui.ModelMap" %>

<%
	ModelMap model=new ModelMap();
	Object obj=model.get("obj");
%>


obj在这里为空.
在这种情况下,如何在视图中检索该obj?
注意:我需要在jsp标记(<%%>)中使用,而不是这样:


In here obj is null.
In this case, how to retrieve that obj in view?
Note: I need to use in jsp tag (<% %>), not like this:

${obj}


谢谢!

推荐答案

{obj}


谢谢!


您需要您的控制器方法来指定视图"本身.因此,返回值应该是一个字符串,该字符串与aaaa.jsp的视图名称相对应.然后,您可以将地图添加到模型中,它将在JSP中可用.这是示例代码:

You need your controller method to specify the "view" itself. So the return value should be a String that corresponds to the view name for aaaa.jsp. You can then add the map to the model and it will be available in the JSP. Here is the sample code:

@RequestMapping(value = "/forgotPWD",params="username", method = RequestMethod.POST)
public String forgotPassword(@RequestParam(value = "username", required = false) String username,
         Map<String, Object> map, Model model) {
    [snip]
    map.put("forgotUser","Mail has been sent to your mail address");
    model.addAttribute("userMap", map);
    [snip]
    return "message.jsp"; // or just "message" depending on Spring settings
}


这篇关于如何从控制器中获取价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆