You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by NR031 <na...@cognizant.com> on 2009/04/08 12:19:34 UTC

Pass parameter to ActionSupport class

Hi,

    I want to pass input parameter entered in the login page to
ActionSupport class. How to pass it.

I used httpservletreq object to get value, but getting NullPointerException
in the req.getParameter("submitvalue") line.

struts.xml :

<struts>
    <package name="ActionServlet" namespace="/JSPPages"
extends="struts-default">
        <action name="AJAX" class="com.cts.AJAX">               
            <result name="success">/JSPPages/AJAXSuccess.jsp</result>
            <result name="failure">/JSPPages/AJAXFailure.jsp</result>
            <result name="error">/JSPPages/AJAXError.jsp</result>
        </action>
    </package>
</struts>

Login page : 

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>This is my Struts2 and AJAX JSP</title>
    </head>
    <body>
        <form action="JSPPages/AJAX.action">
            <h2>This JSP page explains how Struts uses AJAX</h2><br>
            Click the button to see the AJAX page...<br>               
            <input type="submit" value="Show AJAX Page" name="submitvalue"/>        
            <input type="submit" value="Show Failure Page"
name="submitvalue"/>        
        </form>
    </body>
</html>

AJAX.java : 

package com.cts;

import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;

public class AJAX extends ActionSupport {

    HttpServletRequest req;

    @Override
    public String execute() throws Exception {
        String submit_value = req.getParameter("submitvalue");
        if (submit_value != null && submit_value.equalsIgnoreCase("Show AJAX
Page")) {
            return "success";
        } else if (submit_value != null &&
submit_value.equalsIgnoreCase("Show Failure Page")) {
            return "failure";
        }
        System.out.println("Error occurred :: submit_value = " +
submit_value);
        return "error";
    }
}

Is it possible to use HttpServletRequest inside execute() method? What is
the problem in the above code? and what is the solution?


Thanks in advance,
-- 
View this message in context: http://www.nabble.com/Pass-parameter-to-ActionSupport-class-tp22947415p22947415.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Pass parameter to ActionSupport class

Posted by NR031 <na...@cognizant.com>.
Hi,

   Thank you for your quick reply. I resolved my problem. This is what I
did.

Just put getters and setters for "submitvalue" in ActionSupport class:


private String submitvalue;

public String getSubmitvalue() {
        return submitvalue;
    }

    public void setSubmitvalue(String submitvalue) {
        this.submitvalue = submitvalue;
    }
    

NOTE : same name shoulb be used in jsp as well as in java class. i.e.,
"submitvalue"


Thanks once again Garli,
===============================================================================

Nils-Helge Garli wrote:
> 
> Hi!
> 
> Passing parameters to the action is handled automatically for you by
> the framework if you have the appropriate setters on the action. I
> suggest that you start with the tutorials from the documentation:
> http://struts.apache.org/2.1.6/docs/bootstrap.html
> 
> Nils-H
> 
> On Wed, Apr 8, 2009 at 12:19 PM, NR031 <na...@cognizant.com> wrote:
>>
>> Hi,
>>
>>    I want to pass input parameter entered in the login page to
>> ActionSupport class. How to pass it.
>>
>> I used httpservletreq object to get value, but getting
>> NullPointerException
>> in the req.getParameter("submitvalue") line.
>>
>> struts.xml :
>>
>> <struts>
>>    <package name="ActionServlet" namespace="/JSPPages"
>> extends="struts-default">
>>        <action name="AJAX" class="com.cts.AJAX">
>>            <result name="success">/JSPPages/AJAXSuccess.jsp</result>
>>            <result name="failure">/JSPPages/AJAXFailure.jsp</result>
>>            <result name="error">/JSPPages/AJAXError.jsp</result>
>>        </action>
>>    </package>
>> </struts>
>>
>> Login page :
>>
>> <html>
>>    <head>
>>        <meta http-equiv="Content-Type" content="text/html;
>> charset=UTF-8">
>>        <title>This is my Struts2 and AJAX JSP</title>
>>    </head>
>>    <body>
>>        <form action="JSPPages/AJAX.action">
>>            <h2>This JSP page explains how Struts uses AJAX</h2><br>
>>            Click the button to see the AJAX page...<br>
>>            <input type="submit" value="Show AJAX Page"
>> name="submitvalue"/>
>>            <input type="submit" value="Show Failure Page"
>> name="submitvalue"/>
>>        </form>
>>    </body>
>> </html>
>>
>> AJAX.java :
>>
>> package com.cts;
>>
>> import com.opensymphony.xwork2.ActionSupport;
>> import javax.servlet.http.HttpServletRequest;
>>
>> public class AJAX extends ActionSupport {
>>
>>    HttpServletRequest req;
>>
>>    @Override
>>    public String execute() throws Exception {
>>        String submit_value = req.getParameter("submitvalue");
>>        if (submit_value != null && submit_value.equalsIgnoreCase("Show
>> AJAX
>> Page")) {
>>            return "success";
>>        } else if (submit_value != null &&
>> submit_value.equalsIgnoreCase("Show Failure Page")) {
>>            return "failure";
>>        }
>>        System.out.println("Error occurred :: submit_value = " +
>> submit_value);
>>        return "error";
>>    }
>> }
>>
>> Is it possible to use HttpServletRequest inside execute() method? What is
>> the problem in the above code? and what is the solution?
>>
>>
>> Thanks in advance,
>> --
>> View this message in context:
>> http://www.nabble.com/Pass-parameter-to-ActionSupport-class-tp22947415p22947415.html
>> Sent from the Struts - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Pass-parameter-to-ActionSupport-class-tp22947415p22947663.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Pass parameter to ActionSupport class

Posted by Nils-Helge Garli Hegvik <ni...@gmail.com>.
Hi!

Passing parameters to the action is handled automatically for you by
the framework if you have the appropriate setters on the action. I
suggest that you start with the tutorials from the documentation:
http://struts.apache.org/2.1.6/docs/bootstrap.html

Nils-H

On Wed, Apr 8, 2009 at 12:19 PM, NR031 <na...@cognizant.com> wrote:
>
> Hi,
>
>    I want to pass input parameter entered in the login page to
> ActionSupport class. How to pass it.
>
> I used httpservletreq object to get value, but getting NullPointerException
> in the req.getParameter("submitvalue") line.
>
> struts.xml :
>
> <struts>
>    <package name="ActionServlet" namespace="/JSPPages"
> extends="struts-default">
>        <action name="AJAX" class="com.cts.AJAX">
>            <result name="success">/JSPPages/AJAXSuccess.jsp</result>
>            <result name="failure">/JSPPages/AJAXFailure.jsp</result>
>            <result name="error">/JSPPages/AJAXError.jsp</result>
>        </action>
>    </package>
> </struts>
>
> Login page :
>
> <html>
>    <head>
>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
>        <title>This is my Struts2 and AJAX JSP</title>
>    </head>
>    <body>
>        <form action="JSPPages/AJAX.action">
>            <h2>This JSP page explains how Struts uses AJAX</h2><br>
>            Click the button to see the AJAX page...<br>
>            <input type="submit" value="Show AJAX Page" name="submitvalue"/>
>            <input type="submit" value="Show Failure Page"
> name="submitvalue"/>
>        </form>
>    </body>
> </html>
>
> AJAX.java :
>
> package com.cts;
>
> import com.opensymphony.xwork2.ActionSupport;
> import javax.servlet.http.HttpServletRequest;
>
> public class AJAX extends ActionSupport {
>
>    HttpServletRequest req;
>
>    @Override
>    public String execute() throws Exception {
>        String submit_value = req.getParameter("submitvalue");
>        if (submit_value != null && submit_value.equalsIgnoreCase("Show AJAX
> Page")) {
>            return "success";
>        } else if (submit_value != null &&
> submit_value.equalsIgnoreCase("Show Failure Page")) {
>            return "failure";
>        }
>        System.out.println("Error occurred :: submit_value = " +
> submit_value);
>        return "error";
>    }
> }
>
> Is it possible to use HttpServletRequest inside execute() method? What is
> the problem in the above code? and what is the solution?
>
>
> Thanks in advance,
> --
> View this message in context: http://www.nabble.com/Pass-parameter-to-ActionSupport-class-tp22947415p22947415.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org