Solved

Struts - Validate, forward and re-direct.

Posted on 2004-08-18
21
440 Views
Last Modified: 2012-05-05
W.r.to:

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21097832.html

1. How can i make sure that if i set redirect=true,
     how can i write an interim stage to save the errors into the session instead of the request, and then write a listener to extract them back out again.

2. Can i use include="blah..blah" in my <action ..> of my struts-config.xml ?

Thanks.
0
Comment
Question by:sangeetha
  • 10
  • 7
  • 4
21 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11829447
1. What kind of errors? Validation errors?

2. No AFAIK.
0
 
LVL 35

Accepted Solution

by:
TimYates earned 80 total points
ID: 11829473
This is the way I do #1:

I have my own class which extends org.apache.struts.action.Action

Then I have overriden the saveErrors method like this:

  protected synchronized void saveErrors( HttpServletRequest request, ActionErrors errors )
  {
    if( ( errors == null ) || errors.isEmpty() )
    {
      session.removeAttribute(Globals.ERROR_KEY);
      return;
    }
    session.setAttribute( Globals.ERROR_KEY, errors );
  }

Then, I have a servletfilter like:

package my.package ;

import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;

public class MyFilter implements Filter
{
  public void doFilter( final ServletRequest request,
                        final ServletResponse response, FilterChain chain ) throws
                        IOException, ServletException
  {
      Object err = sess.getAttribute( org.apache.struts.Globals.ERROR_KEY ) ;
      sess.removeAttribute( org.apache.struts.Globals.ERROR_KEY ) ;
      if( err != null )
        request.setAttribute( org.apache.struts.Globals.ERROR_KEY, err ) ;
      chain.doFilter( request, response ) ;
    }
  }
}

and put it in the web.xml like:

  <filter>
    <filter-name>requestfilter</filter-name>
    <filter-class>my.package.MyFilter</filter-class>
  </filter>

So you can see, the action stores the errors into the session, then because it is "redirect=true", it causes another request to be made for the next page, then this filter picks up that there are errors, and stores them back into the request for this noew page :-)

Hope this helps!!

Tim
0
 
LVL 2

Author Comment

by:sangeetha
ID: 11829485
Yes..Validation errors.
:-)
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 2

Author Comment

by:sangeetha
ID: 11829605
I am getting "sess" cannot be resolved.

Do i need to create a local variable like:

ServletRequest sess = null;
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 20 total points
ID: 11829618
HttpSession sess =  request.getSession();
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11829637
whoops sorry...
0
 
LVL 2

Author Comment

by:sangeetha
ID: 11829652
I am getting:

The method getSession() is undefined for the type ServletRequest

:-(
0
 
LVL 35

Expert Comment

by:girionis
ID: 11829679
You have to return an HttpSession object. Look at my comment above.
0
 
LVL 2

Author Comment

by:sangeetha
ID: 11829713
Yes...I included that and got that error. This is what i have now:

public class MyFilter implements Filter {
   
    /* (non-Javadoc)
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response,
                               FilterChain chain) throws IOException, ServletException {
              
        HttpSession sess =  request.getSession();
        Object err = sess.getAttribute( org.apache.struts.Globals.ERROR_KEY );
        sess.removeAttribute( org.apache.struts.Globals.ERROR_KEY );
        if( err != null ) {
            request.setAttribute( org.apache.struts.Globals.ERROR_KEY, err ) ;
          chain.doFilter( request, response ) ;
        }
    }

    /* (non-Javadoc)
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
       
    }

    /* (non-Javadoc)
     * @see javax.servlet.Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
       
    }
}

but i get the above error.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11829736
public void doFilter(ServletRequest request, ServletResponse response,
                            FilterChain chain) throws IOException, ServletException {
             
        HttpSession sess =  ((HttpServletRequest)request).getSession();
        Object err = sess.getAttribute( org.apache.struts.Globals.ERROR_KEY );
        sess.removeAttribute( org.apache.struts.Globals.ERROR_KEY );
        if( err != null ) {
            request.setAttribute( org.apache.struts.Globals.ERROR_KEY, err ) ;
          chain.doFilter( request, response ) ;
        }
    }
0
 
LVL 35

Expert Comment

by:girionis
ID: 11829741
Can you try this:

HttpSession sess =  ((HttpServletRequest) request).getSession();
0
 
LVL 35

Expert Comment

by:girionis
ID: 11829751
Or just change the parameter to be of type HttpServletRequest/Response
0
 
LVL 2

Author Comment

by:sangeetha
ID: 11829798
Ok..thanks....

now, where should i put:

<filter>
    <filter-name>requestfilter</filter-name>
    <filter-class>my.package.MyFilter</filter-class>
  </filter>

in my web.xml file. I am getting errors if i put anywhere in the file. This is my web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
      <servlet>
            <servlet-name>action</servlet-name>
            <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
            <init-param>
                  <param-name>config</param-name>
                  <param-value>/WEB-INF/struts-config.xml</param-value>
            </init-param>
            <init-param>
                  <param-name>debug</param-name>
                  <param-value>3</param-value>
            </init-param>
            <init-param>
                  <param-name>detail</param-name>
                  <param-value>3</param-value>
            </init-param>
            <load-on-startup>0</load-on-startup>
      </servlet>
      <servlet-mapping>
            <servlet-name>action</servlet-name>
            <url-pattern>*.do</url-pattern>
      </servlet-mapping>

</web-app>
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 80 total points
ID: 11829907
straight after <webapp>

<web-app>
     <filter>
         <filter-name>requestfilter</filter-name>
         <filter-class>my.package.MyFilter</filter-class>
     </filter>
     <servlet>
0
 
LVL 2

Author Comment

by:sangeetha
ID: 11830044
Ok...didnt show any error in the file. but i am getting the following error in my 'Console':

15:05:38,660 INFO  [TomcatDeployer] undeploy, ctxPath=/KalendarTool, warUrl=file:/C:/Programme/jboss/server/default/deploy/KalendarTool.war/
15:05:38,785 INFO  [TomcatDeployer] deploy, ctxPath=/KalendarTool, warUrl=file:/C:/Programme/jboss/server/default/deploy/KalendarTool.war/
15:05:39,129 INFO  [PropertyMessageResources] Initializing, config='org.apache.struts.action.ActionResources', returnNull=true
15:05:39,175 ERROR [Digester] Begin event threw exception
java.lang.reflect.InvocationTargetException

// blah..blah..

Caused by: java.lang.LinkageError: loader constraints violated when linking org/apache/struts/action/ActionMapping class
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:141)
      at org.apache.struts.config.FormBeanConfig.class$(FormBeanConfig.java:190)
      at org.apache.struts.config.FormBeanConfig.setType(FormBeanConfig.java:190)
      ... 97 more
15:05:39,191 ERROR [ActionServlet] Parsing error processing resource path
java.lang.reflect.InvocationTargetException

// blah..blah..

Thanks for your responses. I will award points. Meanwhile, Please tell me how can i resolve this.
0
 
LVL 2

Author Comment

by:sangeetha
ID: 11830054
My web.xml is as follows:

<web-app>
  <filter>
      <filter-name> requestfilter </filter-name>
      <filter-class>de.grassgmbh.kalendartool.filters.MyFilter</filter-class>
  </filter>
<servlet>
// blah...blah..
0
 
LVL 35

Expert Comment

by:girionis
ID: 11830075
There is a parsing error somewhere. Does it happen when you try to forward or call a JSP page? Can you post your forward code?
0
 
LVL 2

Author Comment

by:sangeetha
ID: 11830240
The error is shown when i save the web.xml file !

Is it problem in detecting the MyFilter Package?

I have:

KalendarTool
|
|____ src
|           |
|           |___ de.grassgmbh.kalendartool
|           |                   |
|           |                   |____ resources.properties
|           |
|           |___ de.grassgmbh.kalendartool.form
|           |                   |
|           |                   |___ FilterForm.java
|           |                   |___ RegisterMaskForm.java
|           |                  
|           |___ de.grassgmbh.kalendartool.action                  
|           |                   |
|           |                   |___ FilterAction.java
|           |                   |___ RegisterMaskAction.java
|           |
|           |___ de.grassgmbh.kalendartool.filters
|                               |
|                               |___ MyFilter.java
|
|_____ We-Root
               |
               |____ WEB-INF
               |           |
               |           |____ struts-config.xml, web.xml, etc..
               |
               |____ jsp
                           |
                           |____ MynageCapacity
               |                           |
                                            |____ FilterList.jsp
                                            |____ RegisterMaskList.jsp
                                            |____ ShowList.jsp


0
 
LVL 2

Author Comment

by:sangeetha
ID: 11830305
BTW,

  <filter-class>de.grassgmbh.kalendartool.filters.MyFilter</filter-class>  refers to the Filter Class.

but why and where we use: <filter-name> requestfilter </filter-name>

?

Thanks.

0
 
LVL 35

Expert Comment

by:girionis
ID: 11830381
Can you post the xml file?

> <filter-name> requestfilter </filter-name>

The <filter-name> tag defines the logical name of the filter that you use to access it. It can be different than the actual filter class.
0
 
LVL 2

Author Comment

by:sangeetha
ID: 11830456
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question