0

Client-side validation doesn't work for me. First I thought that it is a myeclipse fault that doesn't copy them to the root folder but then I discovered the js and css files reside in the struts core jar. Now I wonder what I should do! Should if I find and copy all js and css files from their appropriate folders to the webRoot or there is an intelligent workaround like changing the configuration? Should struts copy them by self?

I use Tiles with Struts. Could it be the problem?

My JSP files are in the WEB-INF folder! Can it have caused some problem?

Can using something like struts2-jquery plugin solve my problem?

I use struts2!

My struts2 filter configuration is

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>

    <listener>
    <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
Johnny
  • 1,509
  • 5
  • 25
  • 38
  • You need to provide more information, for examples, your S2 filter configuration, often a cause of S2 not serving its static files correctly. – Dave Newton Jan 07 '13 at 11:47
  • I copied struts filter configuration. It is so simplistic as you can see! – Johnny Jan 07 '13 at 11:53
  • and the struts Belterra configuration, is indeed, the problem. by not following the struts 2 documentation, you have eliminated struts 2 from processing static files. there is a reason the default mapping suggestion is an asterisk. – Dave Newton Jan 07 '13 at 12:45
  • You are right! changing the url pattern to /* solved the problem. I don't know how to thank you! – Johnny Jan 07 '13 at 13:24

2 Answers2

3

The specific problem here is the filter configuration:

<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>*.action</url-pattern>
</filter-mapping>

The recommended configuration (unless you specifically know what you're doing) is to map to *:

<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>*</url-pattern>
</filter-mapping>

If you map only to *.action, non-action requests (like CSS and JavaScript) won't be processed by the filter. S2 examines the request for files it (S2) knows about, like its own CSS and JavaScript files, and serves those requests itself even though they're not action requests.

This is documented in the S2 guides' Static Content section.

It's perfectly valid to map to *.action, but then you do need to extract the static files and put them at their required location, at least if you intend to use the default S2 themes/JavaScript. That's also optional: the framework is designed to get you started relatively quickly, but if you have specific needs that the framework doesn't handle, using that part of S2 may or may not be the best choice.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Thanks. It is the exact solution! Myeclipse default configuration was *.action and I hadn't touch it. – Johnny Jan 07 '13 at 18:53
2

In a Java project (among the others), Eclipse will copy all your files from the SOURCE folder to the OUTPUT folder.

This means that if you put MyClass.java and foobar.properties in your /src folder, you will have MyClass.class and foobar.properties under your /bin folder (according to the name chosen for the output folder), when you will build your project.

For more automation, like replacing tokens in configuration files (let's say environment variables config), or dynamically retrieving the needed libraries, the two main tools generally adopted are Apache Ant (with Apache Ivy as dependancy manager), or Apache Maven.

Usually,

  • properties files are put in the root of the src folder;
  • Struts2 XML Validation files are put under the same package of the Action to validate;
  • Struts2 XML Visitor Validation files are put under the same package of the POJO to validate;
  • JSP files, JS files, CSS files are put (if they need to be inside the war) adjacent the /WEB-INF folder, and they will not be moved by anyone.

A typical structure could be:

src |
    |-- java
    |-- web
       |-- css
       |-- js
       |-- jsp
       |-- WEB-INF
          |-- lib

Take a look at this SO answers too:

Best location to put your CSS and JS files in a Mavenized Java Web app?

Where do CSS and JavaScript files go in a Maven web app project?


That said, you need to be more explicit when you refer to client validation. If you mean Javascript validation, then refactor the project like above and then describe your problem, taking a look at the JS console;

but using Struts2 it would be better to validate the input with XML Validation, because client side validation is not reliable:

let's say I bypass javascript controls injecting HTML directyl with FireBug, or that i create a Form at runtime with the desired parameters and send it to the server, or that I'm using a browser with javascript disabled... the client is not under your control, then it is a good practice to validate server side.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • But I think that eclipse or struts2 should extract and copy its own css files to the appropriate folder. Browser web console says 404 error for struts css/js files and it is right because they do not exist. My project directory structure is the default one. in the root(src - resources - WebRoot) and in the WebRoot I have (WEB-INF META-INF css js). I have placed the jsp files in the WEB-INF/pages folder for security reasons. The lib in the WEB-INF is published by myeclipse and I have set it to publish all the additional libraries(including struts2).I wonder if I must extract all files from jar – Johnny Jan 07 '13 at 10:44
  • BTW, I have decided to renounce the xml validation and its client side advantages because its unwanted behaviour(http://stackoverflow.com/questions/14185558) I couldn't solve. But struts default css/js files are necessary for a good design structure! – Johnny Jan 07 '13 at 10:50
  • you have no need to extract (!) css and js from a Jar (!), just put them under /css and /js folders in Eclipse. They will be there and noone would touch them, and you will retrieve them from the JSPs. Your problem seems to be that you do not have the necessary files in the right folder. – Andrea Ligios Jan 07 '13 at 10:59
  • Copying struts-core jar files to different folders didn't work. I published it anywhere and finally I extracted the files as the web page expected and it worked. I have never worked with Maven or Ivy or ant directly so I postpone trying them at a later time. It looks that there can't be a better answer to my question. Comunque, grazie mille! – Johnny Jan 07 '13 at 11:47
  • The problem was solved by @DaveNewton 's hint. Read the comments of the question. – Johnny Jan 07 '13 at 13:26
  • It's not so much that client side validation is "unreliable", it's just that it can't be the *only* validation: everything must be validated on the server side as well. (Plus the client-side validation doesn't support all of the validations available on the serber side.) – Dave Newton Jan 07 '13 at 17:00