Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

Struts - Validate, forward and re-direct.

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
sangeetha
Asked:
sangeetha
  • 10
  • 7
  • 4
3 Solutions
 
girionisCommented:
1. What kind of errors? Validation errors?

2. No AFAIK.
0
 
TimYatesCommented:
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
 
sangeethaAuthor Commented:
Yes..Validation errors.
:-)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sangeethaAuthor Commented:
I am getting "sess" cannot be resolved.

Do i need to create a local variable like:

ServletRequest sess = null;
0
 
girionisCommented:
HttpSession sess =  request.getSession();
0
 
TimYatesCommented:
whoops sorry...
0
 
sangeethaAuthor Commented:
I am getting:

The method getSession() is undefined for the type ServletRequest

:-(
0
 
girionisCommented:
You have to return an HttpSession object. Look at my comment above.
0
 
sangeethaAuthor Commented:
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
 
TimYatesCommented:
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
 
girionisCommented:
Can you try this:

HttpSession sess =  ((HttpServletRequest) request).getSession();
0
 
girionisCommented:
Or just change the parameter to be of type HttpServletRequest/Response
0
 
sangeethaAuthor Commented:
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
 
TimYatesCommented:
straight after <webapp>

<web-app>
     <filter>
         <filter-name>requestfilter</filter-name>
         <filter-class>my.package.MyFilter</filter-class>
     </filter>
     <servlet>
0
 
sangeethaAuthor Commented:
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
 
sangeethaAuthor Commented:
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
 
girionisCommented:
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
 
sangeethaAuthor Commented:
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
 
sangeethaAuthor Commented:
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
 
girionisCommented:
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
 
sangeethaAuthor Commented:
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 10
  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now