Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Struts - Validate, forward and re-direct.

Posted on 2004-08-18
21
Medium Priority
?
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 320 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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 80 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 320 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

721 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