以下是创建或更新 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 <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}"/>'}
哪个不起作用。