Win a copy of Microservices Testing (Live Project) this week in the Spring forum!

James Gadbury

Greenhorn
+ Follow
since Mar 20, 2009
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by James Gadbury

Sorry to bump an old thread but I wanted to post a link to an excellent article I just read on Javaworld which would be most useful to others that find their way here.

Write thread-safe servlets
10 years ago

Mohamed Farouk wrote:Hello James
Sorry for late reply,
I use in general: StrutsSpringTestCase

The following code should lead you in the right direction. Let me know if I can be of any more help!


No worries - thanks for replying.

Mohamed Farouk wrote:
Using Struts 2's Junit plugin (StrutsTestCase 2.1.8.1), do you know how to...

- test that after execution of an action, control is passed to a particular page ?


Yes, I've been checking the result name with the above method. But what about a forward or redirect URL? I guess I can use response.getDestinationUrl() or response.getRedirectUrl() to ensure I am forwarded to result.jsp or redirected to somethingelse.action, for example.

Mohamed Farouk wrote:
- test for ActionError messages ?


Perfect!

Mohamed Farouk wrote:
- specify a different struts config file. I have many other struts config files besides my default struts.xml ?


What about Struts configuration files? Did you misread as Spring or am I missing something? I have my interceotors, actions and their results defined in various xml files. By default, StrutsTestCase uses struts.xml but what about other struts configuration xml files?

Thanks again for your help - much appreciated! Kind regards,

James
11 years ago
Hi all,

I am looking to unit test my Struts actions (currently using the Struts JUnit plugin (and my test classes extend StrutsTestCase). At the moment, my actions have nasty static calls to the get the Principal, such as:



My actions implement various interfaces such as SessionAware to make my unit-testing life easier. However, does implementing the PrincipalAware interface compromise security? For example, by implementing the PrincipalAware interface, I have to implement a setter for the PrincipalProxy. Is there not a risk that a user could alter security and set this themselves?

Thanks for reading. Kind regards,

James
11 years ago
Hi Mohamed,

I too have started exploring StrutsTestCase so thanks for your substantial feedback - it's a great help. I will have a look through the articles you posted.

Looking through various documentation and examples, it appears the Struts 2 JUnit plugin's StrutsTestCase class is missing some functionality of the older StrutsTestCase project (hosted on SourceForge). For example, methods such as verifyNoActionErrors(), verifyActionErrors(), verifyForwardPath() and setConfigFile() are not available (as far as I can see) in the Struts 2 plugin version. I'm not sure how to perform this functionality with the Struts 2 plugin - perhaps I'm missing something but it seems limited compared with the functionality of the older SourceForge version of the project.

Using Struts 2's Junit plugin (StrutsTestCase 2.1.8.1), do you know how to...

- test that after execution of an action, control is passed to a particular page ?

- test for ActionError messages ?

- specify a different struts config file. I have many other struts config files besides my default struts.xml ?

Any help would be greatly apreciated! Kind regards,

James
11 years ago
Hi all,

I have been looking at using StrutsTestCase. I would like to use StrutsTestCase with Junit 4. Does anyone here use StrutsTestCase with JUnit 4? I found a JIRA regarding the issue here but it is open and unassigned: https://issues.apache.org/jira/browse/WW-3403

Any help or advice would be greatly appreciated.

Kind regards,

James
11 years ago
I was Googling around looking how to get the requestURL using the ognl syntax #request. I found this post:

http://old.nabble.com/struts-result-going-to-a-current-page-td15509367.html

The way I am doing it is:



The above solution is ugly. Is there a more elegant solution (perhaps something has changed with Struts in the few years that the post was made...) ? Is it possible to use #request.requestURL, or something similar? I tried a few by trial and error and was unable to find a solution on the web.

Thanks for your time. Kind regards,

James
12 years ago
Hi all,

I have been given the following brief:

"Consider the design of a web application using Struts2 and Sitemesh (and possibly Spring) for traditional displays and mobile displays."

Having had a look at how some of the major sites may be doing it, I also thought it would be a good idea to ask here to see how others may be satisying such criteria.

We already have an existing web app project designed initially for traditional displays but it now also provides one mobile page. We have "/" and "mobile" packages in the struts.xml for traditional and mobile, respectively. Both packages have near-duplicated action mappings. The same action is used for both traditional and mobile devices but the namespace is different ("/m") and the result name paths to the jsp pages differ slightly (i.e. WEB-INF/jsp/test.jsp -and- WEB-INF/jsp/mobile/test.jsp). We also use Sitemesh and have two decorators for the traditional and mobile display.

I have considered separate projects; one for a traditional displays and one for mobile device but the maintenance overhead and duplicated code sounds like a headache from the start.
I have considered having single jsp pages that would handle traditional and mobile devices in the same file but that could get convoluted and result in large, messy files.

I don't want to get too set on the design or implementation just yet. I'd be very interested to hear from more experienced users how they are doing it. Is anyone able to offer their experience / advice regarding the design and structure of a single web application that can support different display technologies?

Thanks for reading. Kind regards,

James
12 years ago
Hi all,

I would be very interested to hear of different opinions, experiences and uses regarding wildcard mappings.

I started using Struts2 some months ago. Very quickly, I decided I would use wildcard mappings - they seemed neat and kept the struts2 configuration xml files tidy. I left validation of jsp pages until towards the end... Consider the following action mapping as an example:



I have the corresponding methods add() and edit() in my AccountAction class.

The trouble I found with wildcard mappings is that I can only map one result with the name "input". When validation failed, result name "input" was returned. If validation failed for add, I wanted to forward to add.jsp (to give the user another chance to add an account and display the errors). If validation failed for edit, I wanted to forward to edit.jsp. Therefore, I was unable to distinguish which page I wanted to go to when validation failed and I got result name "input" back from either of the action methods.

I found some solutions.

1) I can store the destination page in a variable in AccountAction. Therefore, when edit() is called, I could set the String inputDestination to "edit.jsp" and in my result mapping have:



Although my action must now know about jsp pages and their names, and set the correct input destination in each method.

This seems a little wrong to me... I'm working around an issue that shouldn't exist.

2) I experimented by writing a new Workflow interceptor. This basically returned "input" with "_methodName" appended, rather than just "input". So if validation of edit Account failed, my custom Workflow interceptor would return "input_edit" as the result name, allowing me to handle different validation failures. But this seemed wrong.

I am currently not using Wildcard mappings for this reason. Any thoughts on ways around this issue (or my own two proposals) would be greatly appreciated!

Thanks for reading!

James
12 years ago
Hi all,

I have created my own workflow interceptor which I would like to replace the default workfloaw interceptor for some actions. I would like to define my own stack to be the same as the default stack but with my custom workflow interceptor in place.

I have done it the manual way. i.e:

<interceptors >
<interceptor name="dynamicInputWorkflow" class="one.of.my.packages.DynamicInputWorkflowInterceptor"/>
<interceptor-stack name="dynamicInputWorkflowStack" >
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="dynamicInputWorkflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>

Is it possible to define a custom stack to use default stack and replace workflow interceptor with my own custom workflow interceptor without having to manually copy and paste the whole default interceptor stack?

Thanks for your help. Kind regards,

James
12 years ago
Hi all,

Sorry to hijack the thread but it is relevant - I too am having problems pre-populating a checkboxlist.

I have a List containing an enum of a type I defined called Role. I am trying to pre-populate a checkboxlist using the following code:



getRoles returns List<Role>, and I can see the list contains two entries, for example when I inspect the object: [ROLE_PROVIDER, ROLE_ADMIN]. Unfortunately, my check boxes are not pre-populated (i.e. none of them are checked).

Thanks in advance for reading. Kind regards,

James
12 years ago
Resolution posted here: http://www.nabble.com/s%3Aradio-and-display-tag-td23886156.html

Hope this helps someone else some day.
13 years ago
My colleagues and I have found the solution so here it is in case someone else comes across this...

We realised that changing the versions of our Struts2 libraries...

struts2-core-2.0.12.jar to struts2-core-2.1.6.jar
xwork-2.0.6.jar to xwork-2.1.2.jar

...was causing the problem with the static content not loading.

By changing the struts2 filter mapping in the web app deployment descriptor (web.xml) from...


to...



...one can use the struts2-core.2.1.6.jar and xwork.2.1.2.jar

If a moderator reads this, could this post please be moved to the struts2 forum - when I posted, I had no idea what the problem was related to!
13 years ago
Sorry for the bump but does anyone have any ideas at all? I think I will have to post up on the displaytag and struts forums (if they have any).
13 years ago
Hi all,

Sorry if this is in the wrong forum - I'm not sure what this should fall under. I have a Maven-based WTP web app using struts 2 / Sitemesh / EXT-JS.

When I run the project using a Tomcat 6 embedded server in Eclipse (3.4) and inspect using Firebug, I am being told that various decorator-related css files and EXT-JS JavaScript could not be found (404). I can also see that files are not loaded as my Sitemesh decorator is not applied and the EXT-JS Javascript is not executed (see below). In fact, my Struts 2 internationalisation is not being applied from my package_<country_code>.properties files, so I suspect something is very wrong with the project structure:



I have triple-checked the URLs - they appear fine to me. The web app is deployed in it's entirety on to the Eclipse-based local Tomcat server and I can see all the files including the 'missing' ones within the <ContextPath>/static/css and <ContextPath>/static/ext directories. I have also tried deleting and adding a new Tomcat server in Eclipse.

Here is my project structure.



This is what my project looked like prior to the EXT-JS - in fact, I had some EXT-JS functioning so something has broken since:



Tomcat server context.xml:


Tomcat server server.xml:


extTest.jsp:


Does anyone have any ideas or pointers at all? I noticed I had a context element with a docRoot parameter in one of the deployment descriptors once but now it has gone...

13 years ago