I'm trying to get struts working with wml. Everything works fine beside the fact, that Struts always thinks it is able to place a cookie at the browser (Nokia 6210). This seems not to be working, as nothing keeps stored in session. I can't reproduce this error with the blueprint from Nokia Toolkit, even if cookies are deactivated. Manual URL-encoding has no effect, as the session-id is never added to url if a cookie was set ... Anybody solved similar problem?
[Craig McClannahan] In most servlet containers, you can turn off the use of cookies for session maintenance -- check the docs for your container on how.
For URL rewriting to work, you need to remember the following:
At JavaOne 2001, it seemed to be suggested that j2ee would have a MVC framework join the recommendations. Later in the talk ("scalibility in the web tier with jsp blah blah" ridiculously long title), it struts was alluded to as being part of j2ee in the future. So is Struts the ever mysterious "Java Server Faces"?
[Craig McClanahan] No, it's not ... but they will work together.
JavaServer Faces is focused on creating a GUI component model for web apps, so that you can create development tools that give page developers an experience similar to WebForms -- drag some GUI components onto your page and attach event handlers to them. Due to the nature of HTML and
HTTP, some of these events will happen on the client side, and some will require a roundtrip to the server to do something and redisplay the same page. JSF is going to be developed under the Java Community Process as JSR-127, and I've been invited to join the expert group.
From the perspective of Struts, JavaServer Faces will provide an alternate approach to building the pages in the view layer -- and you can still use the the rest of the framework to manage the overall application.
As for "an MVC framework as part of J2EE's future", a *lot* of people at JavaOne seemed to think that this would be a good idea, and that Struts would make a good framework ... you never know what the future might
My code looks like this:
<logic:iterate id="pelement" name="MyBean" property="pelements">
<% String name = ((PElement)pelement).getName(); %>
<html:link href="/<%= name %>/index.jsp">
^^^^^^^^^^^ Here is my problem!
<bean:write name="pelement" property="longname"/>
</html:link><br />
</logic:iterate>The links always contain the <%= name %> string instead of the evaluated expression. I tried it with different quoting techniques but without luck.
[Craig McClanahan] You are up against the fact that runtime expressions and string constants cannot be mixed in an attribute of a custom tag -- it's got to be one or the other. One way to deal with it is this:
<html:link href="<%= "/" + name + "/index.jsp" %>">
which uses a Java string expression to calculate the entire value.
I find it sometimes easier to move all validation off the jsp, and hence all java off the jsp. I think its just basically a personal preference. This way I'm making full use of the struts error handling model and keep all my java in one place. There always seems to be more than one way to do things, just depends on what u need to do and whats your preference.
[Craig McClanahan] In future versions of Struts, we will be integrating the ability to create client side validations. In my mind, the purpose of these validations is to improve the quality of the user interface -- in this case, by catching errors that can be caught inside the page (such as required fields and invalid number formats) without wasting the time needed to submit the form and get the errors back.
However, it is important to understand that you should *ALWAYS* perform these validations again on the server side. Consider the following cases:
Basically, never trust the client to do any validations.
I have a frameset of 2 frames. The first frame loads a JSP file containing a custom tag. The tag retrieves a list from my database and displays it as a drop down list. The tag then marks the first entry in the list as default and then loads another file in the second frame. Instead of having a static welcome page in the second frame, I'm wondering if there's any way to perform this:
1. The custom tag finishes in the first frame. It puts some kind of ID in the session.
2. Before the second frame loads, a struts action is executed. The action retrieves information from my database based on the ID the custom tag put in the session, construct a form containing the information returned from the database and forwards to the JSP page to be loaded to the second frame. The JSP load info from the form so that I have default page with information loaded from the database.
[Ted Husted] The page in the "second" frame could also be a JSP that performed a similar lookup on your database, and displayed whatever is appropriate for the default entry. After that, other requests could be targetted for the second frame.
Both JSPs and Actions are called by a HTTP request. It can be helpful to think of Actions as "invisible pages" that respond to a request with a "visible page". Any place where you request (or "load") a page, you can request an Action instead. So however you "load another file in the second frame", you should be able to request an Action there instead. Your tag might also include the database ID as part of the request (/Action.do?ID=XXX), so you don't have to bother with the session.
The best practice is to always link to an Action first, and then have the Action forward to your JSP. Then, the Action can do the database lookup, and pass the list in the request. In this case, it could probably pass the list and the initial detail to both frames at once.
Members of my team are gradually turning against using the Struts taglibs and resorting to scriptlets. IMHO: scriptlets bad, tags good. I've had more experience in using them than the others, but I'm finding it difficult to fight my corner in the face of ever increasing skepticism. Could anyone out there with really valid arguments as to why the use of the Struts taglibs (especially the logic tags as they're getting the most grief from my team at the moment) or taglibs in general is "good", or why the use of scriptlets is "bad" help me out (in the interest of fairness, vice versa arguments also happily received!)??
[Ted Husted] The first three reasons are
Taglibs are callable routines for JSP pages that can be maintained over time. Before long, scriptlets tend to devolve into cut and paste spaghetti.
See also < http://www.servlets.com/soapbox/problems-jsp.html >.
Someone told me they thought some of my tag-libs were deprecated and that I shouldn't be using them anymore.
[Ted Husted] The original (.5) omnibus "struts" taglib is deprecated, being replaced by html, logic, bean, and template.
Along the way, the "form" taglib was also renamed to "html". Of course, you can create whatever reference to the taglib you want, so this is not a technical problem.
Has anybody tried to use Taglibs with Struts instead of Struts tag libraries for features which are available in Taglibs ?
[Ted Husted] I often use custom tags from other sources, including Jakarta Taglibs. Struts plays nice with others ;-)
We are using action forwards, but the url in the browser never changes. Everything works happily, so the only thing we want is for the browser to change and display the new URL (mainly for the refresh button).
[Ted Husted] I believe Refresh repeats the last request, rather than the URL that appears in the browser's address window.
If you want refresh to redisplay the current page (without repeating the last request, which may include input from a form), then you should use the redirect technique that Craig mentioned. (In which case generating a new request becomes a benefit rather than a drawback.)
My web page is generated on the fly. The no of form elements are unknown. In this scenario, how can I use the struts framework?
[Ted Husted] Create an Action to generate and output your page, and then return null to the controller. You would also have to populate the form yourself, since JavaServer Pages cannot be manufactured this way.
Must I use Session scope for an ActionForm that is part of a multi-page wizard?
[Craig McClanahan] The alternative approach is to have *all* of the input fields for the entire wizard included on every page -- but the ones you are not displaying on the current page would be created with <html:hidden> instead of <html:text> or whatever.
Can I display an indicator next a field when there is an error?
[Maya Muchnik] The struts-example shows how to display a field label together with its error. One way to also display an indicator would be to setup a boolean for each of the fields in your ActionForm. If a field fails validation, set its boolean to true. Then, test the boolean in your JSP to see if the error image should be displayed or not.
[Steve A Drake] Check out the pager taglib: http://jsptags.com/tags/navigation/pager/
Very handy! For anyone else looking at this for paging long-winded result sets, and using Tomcat 4.0-b1, I needed to edit the demo "pager-demo.jsp" and change:
<pg:pager maxIndexPages="<%= 20 %>">
to:
<pg:pager maxIndexPages="20">
This fixes a hack put in to convert "20" to an int as delineated in the pager troubleshooting section.
At first, it was to make it easier to develop the tags as the rest of the framework was developed. Placing the tags in another repository would mean that we would have to be more careful about changes and deprecations as people would be using them in other environment.
Once the set of standard tags is published (this is in progress now), we will refactor the Struts tag libraries, and offer the remaining multi-use tags to Jakarta taglibs.