【问题标题】:call controller method from jsp and get model attributes in jsp从jsp调用控制器方法并在jsp中获取模型属性
【发布时间】:2014-05-10 03:53:41
【问题描述】:

由于某些原因(应用程序的现有设计),我需要使用 jsp include 在 jsp 中调用控制器方法。我需要访问父jsp中控制器中的设置属性。 下面是我的控制器:

@Controller
@SessionAttributes({"profile"})
@RequestMapping("/manageMyAccount")
public class MyPageDataController {

    @RequestMapping(value="/",method=RequestMethod.GET)
    public void initData(HttpServletRequest request, Model model, HttpSession session)        {
    Profile profile = (Profile) session.getAttribute("profile");
    model.addAttribute("test", "success");
    model.addAttribute("profile", profile);
} 

}

JSP 代码:

jsp:include page="/mvc/manageMyAccount/"/>

我试图在 jsp 中以${test} 的上述语句后获取 jsp 中的数据,但我变得空白。

【问题讨论】:

  • 测试未设置在任何范围内。试试 ${profile.test}。个人资料正在会话中。
  • 谢谢,但我也无法获取个人资料。使用 ${profile}
  • 我需要这个jsp上的某些数据/对象,并在Controller中设置为模型属性。如何将它们设置为控制器中的范围。我不希望他们参加会议。
  • 我可以获取个人资料。但是有没有办法在jsp中获得“test”属性?
  • 你做了什么,现在你可以得到个人资料?

标签: java jsp spring-mvc


【解决方案1】:

您必须在 jsp 中访问该变量

@RequestMapping(value="/",method=RequestMethod.GET)
    public String initData(HttpServletRequest request, Model model, HttpSession session)        {
    Profile profile = (Profile) session.getAttribute("profile");
    model.addAttribute("test", "success");
    model.addAttribute("profile", profile);
    return "manageMyAccount";
} 

并在“manageMyAccount.jsp”中使用${test}访问变量

【讨论】:

  • 试过这个我正在获取配置文件但没有测试。任何其他指针请。
猜你喜欢
  • 2016-02-12
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
  • 2012-02-06
  • 2017-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多