• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Set a specified action when a jsp page is called

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
until now i've used the struts.xml for set a specified class to a word and set the final redirect to the jsp. Exemple, writing www.mydomain.com/showCatalog , the word "showCatalog" calls the retrieveList.java and once it return "SUCCESS", the showCatalog.jsp is showed.


What i need to know is if there are a method for the inverse procces: calling a speciefed jsp page, i need first of all call a class. In fact showCatalog.jsp alone haven't a sense because first of all i need to generate the product list throught retrieveList.java class. So, i want create a rule for do that. Is it possible?
 
Bartender
Posts: 9615
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Luca Coraggio wrote: i need first of all call a class.



I'm not sure I understand what you are trying to accomplish. You want a Java class to call a JSP?
 
Luk Cora
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, if i call the url www.mydomain.com/showCatalog.jsp in which is present an <s:iterator for iterate throught a product list, i need first of all to retrieve the list from the database, right?
The product list is retrievable throught a specified class (retrieveList.java), so what i want to do is to say to struts that in case the client send a request for showCatalog.jsp, the first thing to do server-side is to launch retrieveList.java
 
Joe Ess
Bartender
Posts: 9615
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's two problems with this approach. The first is that it bypasses the Front Controller which is the entire reason to use Struts (see this article on the architecture of Struts). Second, Struts tags require the Controller to initialize them so they will throw errors if you try to load the JSP directly.
That said, Struts is built on the standard JEE functionality so there is nothing preventing you from using another approach, like creating a JSP that doesn't have Struts tags and using a custom tag to insert your list or a JSP tag to include a Struts-generated list or a scriptlet to do the database access right in the JSP. Some of these alternatives may be better from an architectural standpoint but they'll get you what you want.
 
Luk Cora
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joe Ess wrote:There's two problems with this approach. The first is that it bypasses the Front Controller which is the entire reason to use Struts (see this article on the architecture of Struts). Second, Struts tags require the Controller to initialize them so they will throw errors if you try to load the JSP directly.
That said, Struts is built on the standard JEE functionality so there is nothing preventing you from using another approach, like creating a JSP that doesn't have Struts tags and using a custom tag to insert your list or a JSP tag to include a Struts-generated list or a scriptlet to do the database access right in the JSP. Some of these alternatives may be better from an architectural standpoint but they'll get you what you want.


But if i retrieve the list in the jsp itself, i break the MVC standard point, right?
 
Joe Ess
Bartender
Posts: 9615
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Luca Coraggio wrote:
But if i retrieve the list in the jsp itself, i break the MVC standard point, right?


That's what I was getting at with my first point above about the Struts architecture. Requesting the view (JSP) directly breaks the MVC pattern as well, but the pattern is useful only as long as it facilitates you getting your job done.
 
Luk Cora
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that MVC was an architetural pattern that did not affects the final usage... I mean that its refers to how the project is structured (jsp pages, servlet classes, database implementation), but the final user could have called the jsp directly, and with a "background" architecture, i could redirected him FIRST to the model+controller implementation, and AFTER to the jsp itself.

in this mode:
user request a .jsp page ---> I know that jsp needs some data that i manage in the model-controller part (Servlet) of my structure, so "behind the scenes" i invoke it ---> after invoked my classes, i effectively redirect to jsp

I don't and won't break the MVC pattern, i only want to "override" the beavhior when a jsp page is called without pass throught the Servlet (Model/Controller)
 
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Place your JSPs under WEB-INF. That way a user can never access one directly without going through the controller.
 
Joe Ess
Bartender
Posts: 9615
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posted here, copied to this topic

Luca Coraggio wrote: There's a jsp page with a form that calls a java class and insert a product into the database. After that the product has been inserted, i've to redirect the user into his control panel.
The controlPanel.jsp page needs the information about the user that i can retrieve in another .java class.

The structure is this:
action insertProductForm ---> calls InitProdForm.java, performs some queries and set the variables (to use into the jsp) ---> redirect to the insertProduct.jsp
Once the form in insertProduct.jsp is submitted, i call the action insertProduct ----->this calls a insertProduct.java class, and if all goes fine, it return "SUCCESS".

If SUCCESS is returned, i need to open controlPanel.jsp page, but before that i need to retrieve the user information to show inside the page. User information are retrievable inside another class called getUserInfo.java

I thought to implement the methods and beans dedicated to the retrieve of user infos inside insertProduct.java, but i don't like this solution 1) because it's redundant, 2)because the name of the class (InserProduct) is strongly indicative and there are no sense to include in it methods about user management

 
Joe Ess
Bartender
Posts: 9615
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at any non-trivial Struts program, you'll see that the action classes tend to be very short. Most of the processing is delegated to business logic beans and/or Data Access Objects. This is because many pages share data elements and pushing the creation and handling of those objects into a lower tier facilitates reuse.
The Struts 1 FAQ has a entry on this topic and the examples packaged with the Struts distribution are built with this philosophy. Read both.
 
Luk Cora
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, if a struts servlet can redirect to two or more jsp pages, the only thing i can do is to declare variables with respective getter and setter, of every content i need in every of the jsp pages?
 
Joe Ess
Bartender
Posts: 9615
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Struts 2 does not use servlets. It uses actions

...the only thing i can do is to declare variables with respective getter and setter, of every content i need in every of the jsp pages...



I take it you didn't follow the links I posted. From the second link:

The best thing is to use the Action as a thin adaptor between the web/presentation-tier and your business classes (including those that access a database).
So you first design a business API that uses plain Java classes. The best thing is to use objects that take ordinary Java types and return a JavaBean or collection of JavaBeans. The Action then calls these objects and passes the result back to the web/presentation tier.


 
Luk Cora
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joe Ess wrote:Struts 2 does not use servlets. It uses actions

...the only thing i can do is to declare variables with respective getter and setter, of every content i need in every of the jsp pages...



I take it you didn't follow the links I posted. From the second link:

The best thing is to use the Action as a thin adaptor between the web/presentation-tier and your business classes (including those that access a database).
So you first design a business API that uses plain Java classes. The best thing is to use objects that take ordinary Java types and return a JavaBean or collection of JavaBeans. The Action then calls these objects and passes the result back to the web/presentation tier.



i read it, but it not solves my question.
In my case i've


As you can see, the registration class (servlet/action) can redirect to two different pages according to the returned result. In one page i need a list of postcodes, in the other the list of products.
So, is it conceptually correct to declare both variables i need in the same struts servlet/action Registration.java?

 
Joe Ess
Bartender
Posts: 9615
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Luca Coraggio wrote:
So, is it conceptually correct to declare both variables i need in the same struts servlet/action Registration.java?



I don't know if "conceptually" is the right term. It doesn't work against the framework and, properly documented, would probably be easy enough for someone else to understand.
It would probably be more flexible and consistent to redirect "success" to a showCatalog action (I assume the catalog page is used elsewhere) rather than going directly to the JSP.
 
Luk Cora
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait this is what i asking for!
I've an action showCatalog that in case of "SUCCESS" go to showCatalog.jsp
And i have this other action Register that can return both to registration.jsp or to showCatalog.jsp according to the resulting string.

"Conceptually" would be proper to place as result an action instead of a jsp page, for properly separate business logics between classes (a class called registration should not have references to the products list for exemple)!

What i mean is this:

Actually i've



the action "register" might call showCatalog action instead the jsp itself, and in this mode i will solve my main question.



Can i do it? It's right?

 
Joe Ess
Bartender
Posts: 9615
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Luca Coraggio wrote:
Can i do it? It's right?



That should work.
 
Crusading Chameleon likes the size of this ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic