Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Struts: how to display dynamic data without a form

Posted on 2005-03-08
8
Medium Priority
?
347 Views
Last Modified: 2010-04-01
I'm a bit confused about this. I've been working with Struts for a few weeks now. I know how to work with ActionForms and Actions. But I wonder how you can display a page containing dynamic data generated using parameters in the url (with Struts). Can anyone help me here?
0
Comment
Question by:_manu_
8 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13484456
Using JSTL it's easy

myAction.do?action=Hello

<c:out value='${param.action}'/>

will print out: Hello

Why would you want to use parameters? The idea of having actionForms is that everything is hidden from the user and you don't need to use request parameters
0
 

Author Comment

by:_manu_
ID: 13485411
I should have been more explicit:

I have a page ('showScores.jsp') that displays a number of results from a database. How do (result) beans become available on that page if I just call that page?

And if I add a few parameters in the url (eg country=BE) the results should be limited to records only from Belgium or something. I'm not using a form here.

Do I have to use an application bean to get the results?
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13486242
Are you asking how to call the jsp without first calling the action, then then will result in errors, as you are expecting to output action form values either by the struts tags or by jstl.

Sorry to be obtuse but what exactly does " if I just call that page" mean?

I think I know what you're trying to do but I want to make sure.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:_manu_
ID: 13493674
Bloodredsun, if I go directly to the page "showScores.jsp" in my browser, I want to see some "scores" from my database. On that page there will be a combobox containing countries. If I change the item selected in the country combobox the page is "reloaded" with only results for that country.

I can see how Struts comes in when I select another country (SetCountryActionForm - SetCountryAction and return to showScores.jsp). But I don't see how I can display the result the FIRST time I load the page (results for all countries) - except by writing the logic in the jsp itself.
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13494230
The idea with struts is that you have a separation of business code, such as database access, with presentation layer; your JSP. This also means that your beans/actionForms are not available to the jsp if you try to access them directly, I often put my JSPs in the WEB-INF so that they cannot be accessed directly.

What you need to do is put in some logic in your SetCountryAction that checks whether a country is selected, if not it either goes to another page, or it forwards to the page that you want with a full resultSet from your database. If you want to access a jsp "directly", without an action, then use the ForwardAction e.g. in action-mappings element of struts-config.xml

<action path="/index" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/pages/index.jsp" />

This sounds more like an architecture issue rather than a Struts problem.
0
 
LVL 11

Expert Comment

by:raj3060
ID: 13522638
If you just want to display data, you should be able to do that:

Import the formClass in that jsp page and, and call getter methods for the values where you want and also put logic where you don't want someone to access some part..
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13523774
If you need to access a formbean/ActionForm via JSTL it's very easy.
It's <c:out value='${[scope].[beanName.[property]}'/>

e.g.
<c:out value='${requestScope.myActionForm.firstname}'/>

and testing for the value and so prevent nullpointers

<c:if test='${not empty requestScope.myActionForm}'>
//do stuff
</c:if>

0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 400 total points
ID: 13525158
Hello.

To reinforce some of the previous comments, the pattern for development in Struts is that the JSPs are only about generating the presentation, not about processing requests. For this reason you'll find that the majority of Struts development teams follow a practice of hiding JSPs behind WEB-INF so that they are not publicly visible. See
    http://husted.com/struts/catalog.html

Here's an article with a great diagram that illustrates the request processing path for a Struts application:
    http://www-106.ibm.com/developerworks/web/library/wa-struts/

Struts helps you build applications according to a web MVC design pattern that is referred to as Model 2 Architecture. Here's another article that may assist you in understanding the difference between what you have described wanting to do (which is Model 1 Architecture) and what Struts encourages (jump to the JSP Access Models section):
    http://java.sun.com/developer/onlineTraining/JSPIntro/contents.html

Best regards,
Jim Cakalic
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What happened when I tried to make my phone, a Samsung Galaxy S8, stop dropping all WiFi signals was a saga I won't repeat here, but what you might want to know is how it started and how I got it working again.
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question