0

The following is form for create or upadte a Post object(I use the spring form tag also):

<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>

And when the post.id==0,it means that this post is to be created while the method of the form should be "POST",otherwise it is updated and the method will be "PUT".

But the above code will cause exception:

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

What's the problem,how to fix it?


update:

for the action it should be :

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

Then the finally action will be /appcontext/posts or

/appcontext/posts/1

update2:

For action I can use :

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

However,this note the '/' in the url, it will be relatived to the host.

That's to say,the form action will be:

http://localhost/posts

While I want it be:

http://localhost/context/posts

That's why I perfer <c:url> which will add the context for me.

And I want this manner:

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

Which does not work.

edze
  • 2,965
  • 1
  • 23
  • 29
hguser
  • 35,079
  • 54
  • 159
  • 293

1 Answers1

1

You use tags in the property of an other tag. The " is nested.

Try this:

 <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>

If you use ./ it will refer to the same path. You can also get the context path via request.getContextPath() in your JSP Page or Servlet.

Take a look here.

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

This can not work too, it is also nested again. Please read the Java Tutorial.

Community
  • 1
  • 1
edze
  • 2,965
  • 1
  • 23
  • 29