Should I find and copy struts styles and javascripts to the /struts/ folder enable client-side validation?

StackOverflow https://stackoverflow.com/questions/14188272

質問

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>
役に立ちましたか?

解決

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.

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top