aspose file tools *
The moose likes Struts and the fly likes Working with Struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Working with Struts2 " Watch "Working with Struts2 " New topic
Author

Working with Struts2

Faraz Ali
Ranch Hand

Joined: May 15, 2008
Posts: 108
Hi,

I am confused about how to properly used the struts2. I have few questions below:
1. How to do forward, include in actions, just like we used to do in servlets, and in Struts1 there was ForwardAction. How to do similar thing here?
2. FrontController pattern is still good to use with Struts2 action?
3. What will be the best application flow in struts2. Means from jsp to call action, action decide where to forward (just like controller), than from here we set get something in request/session, and sends to jsp for display?? is it correct?
4. What is the best way to access request/response/session/context objects from struts actions?
5. Can we use JSTL/EL in jsps along with struts2? Or OGNL is good? which one is best?
6. What type of code should be in the actions.

I am very much curious about all of these things before i shift my work to struts2.

Thank you,
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 689

Hi..

Here are the Ans for your Question.

1 ) When you write in your browser action name then it Pass to Struts.xml file
then your xml map Action and according your Action it call Action class and call method after return Success and error and input string return value it again pass Struts.xml and according return type it include jsp.

like : in XML

<action name="doLogin" class="com.printeverywhere.action.AdminLoginAction">
<result name="input">/jsp/login.jsp</result>
<result name="success" type="redirectAction">showMainMenu</result>
</action>

2 ) Yes.

3 ) yes your action decide where to forward (just like controller), than from here we set get something in request/session, and sends to jsp for display.

4 ) In Action class it's java code so as per your java convention to access it.

5 ) You can use any thing with Struts 2. like JSTL/EL .even Spring also integrate with it.

6 ) Action class must contains Java code obviously.

And my advise if you think to switch on Strus 2.0 then it is very good.

Because it is easy to understand and develop application with Struts 2.0.

You also get more advance plug in for Struts 2.0 which makes your application fast developed.


Thanks,
Nishan Patel.


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

Originally posted by Faraz Ali:
2. FrontController pattern is still good to use with Struts2 action?


If we use an MVC pattern for our application, Struts is the controller. We then implement the Model as an Action and a View as a JSP.


[How To Ask Questions On JavaRanch]
Faraz Ali
Ranch Hand

Joined: May 15, 2008
Posts: 108
Thanks a lot for your valuable comments.

And can we pass query string parameters to the action like,

namespace/test.action?user=abc

I tried above thing but its not working. I also used param tags in <s:action> and getting it from request.getParamter in action which implemented servletrequestaware, but its not working.

Can you also tel me how to do this?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

Originally posted by Faraz Ali:
And can we pass query string parameters to the action like,
namespace/test.action?user=abc


The URL request syntax is specified by the HTTP protocol. Struts simply wraps that syntax up with a handy object-oriented framework. If you aren't getting the results you think you should, your struts configuration or object declaration is probably at fault.
Faraz Ali
Ranch Hand

Joined: May 15, 2008
Posts: 108
Thanks,

I have fixed it.

I am also trying to include the result of an action using <s:include>. I am calling it from jsp page. I tried everything, but its unable to locate my action.

Is there any special requirements. Or can you please teach me the directory structure mappings.

Thanking you in advance,
Faraz Ali
Ranch Hand

Joined: May 15, 2008
Posts: 108
Hi,

I Fixed above issue.

Can anyone tell me how to use struts2 <s:tree with dynamic values. Because i am trying put JSP EL inside name attribute, and it says that this tag cannot accept expression values.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Originally posted by Faraz Ali:
And can we pass query string parameters to the action like,
namespace/test.action?user=abc


Hi Can you tell me how did you achieve this in Struts 2,i'm trying the same but unable to find the solution.

--
Deepak Lal


When The Going Gets Tougher,The Tougher gets Going
Faraz Ali
Ranch Hand

Joined: May 15, 2008
Posts: 108
I used <s:action> instead. Set the executeResult="false" and in your action store it in some scope variables, than display it to jsp. Producing same result for me, but i am still curious why <s:include and <jsp:action not working at all.

Have you tried <jsp:action> to call struts2 action?
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi Faraz,
I'm still confused with using of <s:action> and Setting the executeResult="false" ,can you tell me in brief how you accomplished it..

if you can give an example in STRUTS 2,it would be helpful.can you paste the sample logic,,,i have been trying and being unsuccessful...

Help provided will be highly appreciated.

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi Faraz,
Any updates on this?
--
Deepak Lal
Faraz Ali
Ranch Hand

Joined: May 15, 2008
Posts: 108
Hi Deepak,

Sorry for delay. Follow the steps below:

1. Send request to the action like this:


Or if you want to send request paramters with your action, you can do like this:


2. Above code will invoke the action, and see below how i configured this action in struts.xml:


Above things states that when a request comes at the action MyController, forward it to mypage.jsp

3. Now see how i code my action:


4. mypage.jsp is very simple. I did not use struts tag jsp because i dont know how to use them properly, so i used jsp EL to display the request attribute. Like below:
<body>
My name is : ${thename}
</body>


I hope this will help.
Faraz Ali
Ranch Hand

Joined: May 15, 2008
Posts: 108
One thing, since execute=false, will not render the output, we are storing its result in the request container and if we want to send it back to the jsp from where the request came, than we need to put the action tag in a if condition so that i do not execute twice.

Its working for me but frankly speaking, i am not happy with it at all. I still want to use <jsp:include> or <s:action> but i am tired trying it, its not working.

Can anybody help us on this?

Thanks,
 
 
subject: Working with Struts2