wood burning stoves 2.0*
The moose likes Struts and the fly likes Can't conditionally exclude parts of Struts page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Can Watch "Can New topic
Author

Can't conditionally exclude parts of Struts page

Austin Tashis
Greenhorn

Joined: May 23, 2003
Posts: 19
I'm working on a page in a Weblogic 10 application that needs to be included in other pages but also opened as a stand-alone page, so I thought I'd just put some c:if tags around the elements that are required only in the stand-alone page so they can be excluded when it's included in another page, like this:


I thought, as long as the beginning and end tags are present or absent under the same conditions, everything would be hunky-dory. Not only was I mistaken, but I'm still having a hard time understanding the error feedback I'm getting.



Huh? It can't use the start tag because it can't find the end tag, and it can't use the end tag because it can't find the start tag??? But! But!

And this is with both "ifs" set to true, even if I put ${true} right in the test attribute (or false--it doesn't make any difference). And it does the same thing if I use <logic:equal parameter="standalone" value="true">.

I tried this on Tomcat, using a simpler page, and also got an error, but this time it was the </c:if> tag that was unbalanced.

I've already come up with a workaround, but I'm still puzzled by this.
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
</html:form> tag closing in <c:if test="${standAlone}"> tag this is not right.
tag opening & closing should be in propare sequence.
paste whole jsp here, we can figure out error.


Thanks,
Shailesh
Austin Tashis
Greenhorn

Joined: May 23, 2003
Posts: 19
Not quite sure what you mean by "sequence." All tags are opened before they're closed.

The only thing here that's not quite correct, as I see it, is that the html and form tags are not being opened and closed in the same context, but the contexts they are in are logically identical. I even declared a special boolean variable to connect them.

Can it be that the error checking is so strict that it throws an error if there's even the slightest possibility that an error might occur, e.g. if something were to happen to the value of that variable, instead of waiting for an actual error to occur? That seems a little paranoid.

All the html you need is there. You can imagine any entities that might be appropriate in the context of an html form element. If it is a stand-alone page, the"ifs" will be true and it will use its own form and html elements; if it's embedded in another page, it will be inserted within the context of that page's form element.

Shailesh Narkhede wrote:</html:form> tag closing in <c:if test="${standAlone}"> tag this is not right.
tag opening & closing should be in propare sequence.
paste whole jsp here, we can figure out error.
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368


If ${param.standalone} false then <html:html> tag not come in picture then </html:html> not having start tag.

In above code if ${standAlone} come false then </html:form> will not come in picture.
this means <html:form action="/form-submit.do"> will not have end tag.



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The issue is that you can't arbitrarily nest tags like that--you can't do it in XML, either.
Austin Tashis
Greenhorn

Joined: May 23, 2003
Posts: 19
Sure, but the XHTML hierarchy and the JSP hierarchy are really two different things. I guess I was thinking the same would be true of Struts and JSTL tags. It's too bad there's no way to programmatically include Struts HTML elements without excluding all their descendant nodes. That's a real limitation.

David Newton wrote:The issue is that you can't arbitrarily nest tags like that--you can't do it in XML, either.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There's no difference between JSTL custom tags and Struts custom tags (other than their functionality, of course).
Austin Tashis
Greenhorn

Joined: May 23, 2003
Posts: 19
Other than that, eh? ;-)

I suppose it's tied to the functional difference, but I am having a hard time thinking of anything analogous to the <html:form> and <html:html> tags where they replace themselves with their corresponding html tags. One consequence is that they tend to have incredibly large chunks of JSP code, in all its many varieties, in their contexts, which becomes non-reusable, unless it's included from other files, which is probably the right way to do things anyway. I could go for smaller forms too, but I can't change that.

Considering how fussy they are about what you can wrap around them, I wish they could be as picky about what goes on between them. I'd start using them everywhere.

David Newton wrote:There's no difference between JSTL custom tags and Struts custom tags (other than their functionality, of course).
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

...

JSTL custom tags are custom tags, and Struts 2 custom tags are custom tags. They're processed in the same way, at the same time. You can't do this, either:It has nothing to do with being a Struts tag--do you see what I mean? Those are just JSTL tags. This is illegal JSP.
Austin Tashis
Greenhorn

Joined: May 23, 2003
Posts: 19
The difference is that there's nothing to be gained from enclosing conditionals in other conditionals like that. You can get all the permutations you need without breaking any rules. But there are good reasons for wanting to exclude HTML tags (e.g. they might be illegal or redundant in certain contexts), and you can do it with with plain old HTML tags and produce perfectly valid HTML,



but Struts doesn't permit this, nor is there any way, through manipulating the available attributes, to cause it not to render its elements.

David Newton wrote:...

JSTL custom tags are custom tags, and Struts 2 custom tags are custom tags. They're processed in the same way, at the same time. You can't do this, either:It has nothing to do with being a Struts tag--do you see what I mean? Those are just JSTL tags. This is illegal JSP.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Whether or not there's "anything to be gained" isn't relevant: illegal JSP is illegal JSP. This has *nothing* to do with Struts, and everything to do with JSP compilation.

Yes, you can do it with plain old HTML, because HTML elements aren't custom tags, and aren't processed by the JSP compiler as anything other than string literals. They don't *do* anything.

In your original example what's in the section between the <html:form> tags? If they're not <html:...> form elements then I hardly see the point of using a Struts tag to generate the form element in the first place. If they are then what's the point of having them there without the form tag?

IMO you're trying to do too much in the JSP anyway, and it'd be easier and cleaner to maintain using some other mechanism, like JSTL templates, or some other solution.

In any case, there's not much more to be said about this--you're trying to do something JSP simply doesn't support.
Austin Tashis
Greenhorn

Joined: May 23, 2003
Posts: 19
Yes, they are Struts form element tags. The point of having them there without the form tag, as I think I said before, is that the page can be embedded within a larger form in another page.

Imagine a big, complex form, made up of little modules, some of which are pretty useful all by themselves, so you might want to use the same code to create that module both inside the big page and all by its lonesome in a little popup window or letterbox iframe.

Wow! Reusable code! What a concept!

But in the popup window or iframe, it needs an html element and a form element, but if you embed those elements in the middle of another page's form element, guess what? It throws an error, of course.

Now, I can make (and have made) a little shell HTML file with nothing but html, head, body and form elements, and embed the guts of the module in that so it doesn't have to have its own html and form elements so it can then be embedded in the big form, but that's one more file I need to create and maintain, just because there's no way to make it simply grow those parts when it needs them.

It's not such a big deal. I can work with it. I just hate constraints that are imposed without any evident added value. Like I said, it could only throw errors when there are real, not just potential, problems. It was a design decision, and, I think, a bad one.

David Newton wrote:Whether or not there's "anything to be gained" isn't relevant: illegal JSP is illegal JSP. This has *nothing* to do with Struts, and everything to do with JSP compilation.

Yes, you can do it with plain old HTML, because HTML elements aren't custom tags, and aren't processed by the JSP compiler as anything other than string literals. They don't *do* anything.

In your original example what's in the section between the <html:form> tags? If they're not <html:...> form elements then I hardly see the point of using a Struts tag to generate the form element in the first place. If they are then what's the point of having them there without the form tag?

IMO you're trying to do too much in the JSP anyway, and it'd be easier and cleaner to maintain using some other mechanism, like JSTL templates, or some other solution.

In any case, there's not much more to be said about this--you're trying to do something JSP simply doesn't support.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

JSP made a *lot* of design and implementation decisions that don't make a lot of sense, IMO, but there's not much we can do about it at this point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't conditionally exclude parts of Struts page
 
Similar Threads
Cannot retrieve mapping for action
Struts Validate problem
html:img NullPointerException
Difficulty with basic struts app
no getter method