如何将对象从视图传递给java函数?

问题描述:

我有一个jsp页面如何将对象从视图传递给java函数?

<%@page import="org.springframework.web.servlet.ModelAndView"%> 
    <%@page import="mvc3.helpers.Utils"%> 
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
    <%@ page session="false" %> 
    <html> 
    <head> 
     <title>View Topic</title> 
    </head> 
    <body> 

    <fieldset> 
     <legend>${topic.getName()}</legend> 
     <div class="display-label-field"><b>Content:</b>${topic.getText()}</div> 
     <div class="display-label-field"><b>Comments count:</b>${topic.getCommentsCount()}</div> 
     <div class="display-label-field"><b>Last time updated:</b>${topic.getTimeLastUpdated()}</div> 

    </fieldset> 

    <p> 
    <%=Utils.actionLink("Comment Topic", "Topic", "AddComment", Integer.toString(topic.getId())) %> 
    <%=Utils.actionLink("Back to Topic List", "", "home", null)%> 
    </p> 

的问题是,$ {topic.getName()}工作正常,但<%= Utils.actionLink( “评论主题”, “主题” ,“AddComment”,Integer.toString(topic.getId()))%>导致主题无法解析错误。我该如何处理这个问题?

如果您在请求中设置属性话题再做

Integer.toString(((Topic)request.getAttribute("topic")).getId()) 
+0

这是主题无法解决的错误,而不是运行时。 – kosa 2012-08-01 20:09:44

+0

看到更新,把题目的类型放在那里 – 2012-08-01 20:12:54

+0

几乎正确。我们需要使用完整类型声明** mvc3.model.Topic **而不是**主题** – Eugene 2012-08-02 17:32:26

我们不应该访问的getId与EL?像下面的东西?

Integer.toString(${topic.getId()}) 

变量"topic"尚未设置为您的视图模型。在你的控制器中,你需要这样的东西:

myModelData.put("topic", topicId);