0

以下是创建或更新 Post 对象的表单(我也使用 spring 表单标签):

<sf:form method="<c:choose><c:when test="${post.id==0}">post</c:when><c:otherwise>put</c:otherwise></c:choose>" commandName="post">
    Title:<sf:input path="title" /><sf:errors path="title" /><br />
    Body:<sf:textarea path="body" cols="70" rows="6" /><sf:errors path="body" /><br />
</sf:form>

而当post.id==0, 则表示要创建这篇文章,而表单的方法应该是“POST”,否则它会被更新并且方法将是“PUT”。

但是上面的代码会导致异常:

org.apache.jasper.JasperException: /WEB-INF/jsp/posts/_form.jsp (line: 5, column: 41) Unterminated &lt;sf:form tag

什么问题,怎么解决?


更新:

对于行动,它应该是:

<c:url value="/posts/" /> for create and 
<c:url value="/posts/${post.id}/" /> for update.

那么最后的动作将是/appcontext/posts

/appcontext/posts/1

更新2:

对于行动,我可以使用:

${post.id == 0 ? '/posts/' : '/posts/${post.id}'}

但是,这里注意 url 中的“/”,它将与主机相关。

也就是说,表单动作将是:

http://localhost/posts

虽然我希望它是:

http://localhost/context/posts

这就是为什么我更喜欢<c:url>它会为我添加上下文。

我想要这种方式:

${post.id == 0 ? '<c:url value="/posts/" />' : '<c:url value="/posts/${post.id}"/>'}

哪个不起作用。

4

1 回答 1

1

您在其他标签的属性中使用标签。"是嵌套的。

试试这个:

 <sf:form method="${post.id == 0 ? 'post' : 'put'}" commandName="post"
          action="${post.id == 0 ? './posts/' : './posts/${post.id}'}">
    ...

    <input type="submit" value="Submit" name="submit" />
    or
    <a href="#" onClick="submit();">submit with link and JavaScript</a>

 </sf:form>


 <c:url value="${post.id == 0 ? './posts/' : './posts/${post.id}'}" var="url">
 </c:url>

 <a href="${url}">Huhu ein Link</a>

如果您使用./它将引用相同的路径。您还可以通过request.getContextPath()在您的JSP Page或中获取上下文路径Servlet

看看这里

> ${post.id == 0 ? '<c:url value="/posts/" />' : '<c:url
> value="/posts/${post.id}"/>'}

这也行不通,又嵌套了一遍。请阅读Java 教程

于 2012-08-31T08:38:13.140 回答