You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Srineel Mazumdar <sm...@gmail.com> on 2012/11/07 13:17:34 UTC

Getting request as null

Hi,

I am getting request object as null. Checked google..no help..


*Class where I am getting null*

package com.home.practice.shop;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.home.practice.shop.domain.logic.PetStoreFacade;
import com.opensymphony.xwork2.ActionSupport;

public  class BaseAction extends ActionSupport {

private PetStoreFacade petStore;
HttpServletRequest request = ServletActionContext.getRequest();
 public void setServlet() {

ServletContext context = ServletActionContext.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils
.getRequiredWebApplicationContext(context);
this.petStore = (PetStoreFacade) wac.getBean("petStore");

}

protected PetStoreFacade getPetStore() {
return petStore;
}

}

But strangely in the following class the same code I have wriiten and I am
getting request object

public class ViewCategoryAction  extends BaseAction {

public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
System.out.println("******************" + request);


Why is it so ? ServletActionContext.getRequest() is returning null in base
class but not in child class.

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="
http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


<bean id="baseAction" class="com.home.practice.shop.BaseAction" scope =
"prototype" abstract="true"/>

<bean id="addItemToCartAction"
class="com.home.practice.shop.AddItemToCartAction" scope = "prototype"
parent = "baseAction">
<property name="workingItemId" value="" />
<property name="cart" ref="domain.cart" />
</bean>

<!-- <bean id="baseAction" class="com.home.practice.shop.BaseAction" /> -->

<bean id="doNothingAction" class="com.home.practice.shop.DoNothingAction"
scope = "prototype" parent = "baseAction"/>

<bean id="editAccountAction"
class="com.home.practice.shop.EditAccountAction" scope = "prototype" parent
= "baseAction">
<property name="username" value="" />
<property name="password" value="" />
<property name="repeatedPassword" value="" />
<property name="languages">
<list>
<value>enlish</value>
<value>japanese</value>
</list>
</property>
<property name="categories">
<list></list>
</property>
<property name="validate" value="" />
<property name="forwardAction" value="" />
<property name="account" ref="domain.account" />
<property name="myList" ref = "myPageList" />
</bean>

<bean id="editAccountFormAction"
class="com.home.practice.shop.EditAccountFormAction" scope = "prototype"
parent = "baseAction"/>
 <bean id="listOrdersAction"
class="com.home.practice.shop.ListOrdersAction" scope = "prototype" parent
= "baseAction"/>
 <bean id="newAccountAction"
class="com.home.practice.shop.NewAccountAction" scope = "prototype" parent
= "baseAction"/>

<bean id="newAccountFormAction"
class="com.home.practice.shop.NewOrderFormAction" scope = "prototype"
parent = "baseAction">
<property name="order" ref="domain.order" />
<property name="shippingAddressRequired" value="false" />
<property name="confirmed" value="false" />
<property name="cardTypeList">
<list>
<value>Visa</value>
<value>MasterCard</value>
<value>American Express</value>
</list>
</property>
</bean>
 <bean id="newOrderAction" class="com.home.practice.shop.NewOrderAction"
scope = "prototype" parent = "baseAction">
<property name="order" ref="domain.order" />
<property name="shippingAddressRequired" value="false" />
<property name="confirmed" value="false" />
<property name="cardTypeList">
<list>
<value>Visa</value>
<value>MasterCard</value>
<value>American Express</value>
</list>
</property>
</bean>


<bean id="newOrderFormAction"
class="com.home.practice.shop.NewOrderFormAction" scope = "prototype"
parent = "baseAction">
<property name="order" ref="domain.order" />
<property name="shippingAddressRequired" value="false" />
<property name="confirmed" value="false" />
<property name="cardTypeList">
<list>
<value>Visa</value>
<value>MasterCard</value>
<value>American Express</value>
</list>
</property>
</bean>
 <bean id="removeItemFromCartAction"
class="com.home.practice.shop.RemoveItemFromCartAction" scope = "prototype"
parent = "baseAction">
<property name="workingItemId" value="" />
<property name="cart" ref="domain.cart" />
</bean>

<bean id="searchProductsAction"
class="com.home.practice.shop.SearchProductsAction" scope = "prototype"
parent = "baseAction"/>


<bean id="signonAction" class="com.home.practice.shop.SignonAction" scope =
"prototype" parent = "baseAction">
<property name="username" value="" />
<property name="password" value="" />
<property name="repeatedPassword" value="" />
<property name="languages">
<list>
<value>enlish</value>
<value>japanese</value>
</list>
</property>
<property name="categories">
<list></list>
</property>
<property name="validate" value="" />
<property name="forwardAction" value="" />
<property name="account" ref="domain.account" />
<property name="myList" ref = "myPageList" />
</bean>

 <bean id="updateCartQuantitiesAction"
class="com.home.practice.shop.UpdateCartQuantitiesAction" scope =
"prototype" parent = "baseAction">
<property name="workingItemId" value="" />
<property name="cart" ref="domain.cart" />
</bean>


<bean id="viewCartAction" class="com.home.practice.shop.ViewCartAction"
scope = "prototype" parent = "baseAction">
<property name="workingItemId" value="" />
<property name="cart" ref="domain.cart" />
</bean>


<bean id="viewCategoryAction"
class="com.home.practice.shop.ViewCategoryAction" scope = "prototype"
parent = "baseAction"/>

<bean id="viewItemAction" class="com.home.practice.shop.ViewItemAction"
scope = "prototype" parent = "baseAction"/>

<bean id="viewOrderAction" class="com.home.practice.shop.ViewOrderAction"
scope = "prototype" parent = "baseAction">
<property name="username" value="" />
<property name="password" value="" />

<property name="repeatedPassword" value="" />

<property name="languages">
<list>
<value>enlish</value>
<value>japanese</value>
</list>
</property>
<property name="categories">
<list></list>
</property>
<property name="validate" value="" />

<property name="forwardAction" value="" />

<property name="account" ref="domain.account" />

<property name="myList"
ref = "myPageList" />
</bean>

<bean id="viewProductAction"
class="com.home.practice.shop.ViewProductAction" scope = "prototype" parent
= "baseAction"/>
 <bean id="domain.account" class="com.home.practice.shop.domain.Account"
scope = "prototype"/>
<bean id="domain.cart" class="com.home.practice.shop.domain.Cart" scope =
"prototype"/>
<bean id="domain.cartItem" class="com.home.practice.shop.domain.CartItem"
scope = "prototype"/>
<bean id="domain.category" class="com.home.practice.shop.domain.Category"
scope = "prototype"/>
<bean id="domain.item" class="com.home.practice.shop.domain.Item" scope =
"prototype"/>
<bean id="domain.lineItem" class="com.home.practice.shop.domain.LineItem"
scope = "prototype"/>
<bean id="domain.order" class="com.home.practice.shop.domain.Order" scope =
"prototype"/>
<bean id="domain.product" class="com.home.practice.shop.domain.Product"
scope = "prototype"/>
<bean id="myPageList" class =
"org.springframework.beans.support.PagedListHolder" scope = "prototype"/>


</beans>


Regards,
Srineel

Re: Getting request as null

Posted by Chris Pratt <th...@gmail.com>.
If I had to guess, I'd say that the ServletActionContext hasn't been
completely initialized when the Class is instantiated, so calling
getServletRequest in an instance initializer isn't going to work.  Either
call ServletActionContext.getServletRequest() in the method body, or
(better option) use the ServletRequestAware interface on your Action and
have the ServletRequest injected into your Action.
  (*Chris*)


On Wed, Nov 7, 2012 at 4:17 AM, Srineel Mazumdar <sm...@gmail.com>wrote:

> Hi,
>
> I am getting request object as null. Checked google..no help..
>
>
> *Class where I am getting null*
>
> package com.home.practice.shop;
>
> import javax.servlet.ServletContext;
> import javax.servlet.http.HttpServletRequest;
>
> import org.apache.struts2.ServletActionContext;
> import org.springframework.web.context.WebApplicationContext;
> import org.springframework.web.context.support.WebApplicationContextUtils;
>
> import com.home.practice.shop.domain.logic.PetStoreFacade;
> import com.opensymphony.xwork2.ActionSupport;
>
> public  class BaseAction extends ActionSupport {
>
> private PetStoreFacade petStore;
> HttpServletRequest request = ServletActionContext.getRequest();
>  public void setServlet() {
>
> ServletContext context = ServletActionContext.getServletContext();
> WebApplicationContext wac = WebApplicationContextUtils
> .getRequiredWebApplicationContext(context);
> this.petStore = (PetStoreFacade) wac.getBean("petStore");
>
> }
>
> protected PetStoreFacade getPetStore() {
> return petStore;
> }
>
> }
>
> But strangely in the following class the same code I have wriiten and I am
> getting request object
>
> public class ViewCategoryAction  extends BaseAction {
>
> public String execute() throws Exception {
> HttpServletRequest request = ServletActionContext.getRequest();
> System.out.println("******************" + request);
>
>
> Why is it so ? ServletActionContext.getRequest() is returning null in base
> class but not in child class.
>
> applicationContext.xml
>
> <?xml version="1.0" encoding="UTF-8"?>
>
>
> <beans xmlns="http://www.springframework.org/schema/beans"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="
> http://www.springframework.org/schema/aop"
> xmlns:tx="http://www.springframework.org/schema/tx"
> xsi:schemaLocation="
> http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
> http://www.springframework.org/schema/aop
> http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
> http://www.springframework.org/schema/tx
> http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
>
>
> <bean id="baseAction" class="com.home.practice.shop.BaseAction" scope =
> "prototype" abstract="true"/>
>
> <bean id="addItemToCartAction"
> class="com.home.practice.shop.AddItemToCartAction" scope = "prototype"
> parent = "baseAction">
> <property name="workingItemId" value="" />
> <property name="cart" ref="domain.cart" />
> </bean>
>
> <!-- <bean id="baseAction" class="com.home.practice.shop.BaseAction" /> -->
>
> <bean id="doNothingAction" class="com.home.practice.shop.DoNothingAction"
> scope = "prototype" parent = "baseAction"/>
>
> <bean id="editAccountAction"
> class="com.home.practice.shop.EditAccountAction" scope = "prototype" parent
> = "baseAction">
> <property name="username" value="" />
> <property name="password" value="" />
> <property name="repeatedPassword" value="" />
> <property name="languages">
> <list>
> <value>enlish</value>
> <value>japanese</value>
> </list>
> </property>
> <property name="categories">
> <list></list>
> </property>
> <property name="validate" value="" />
> <property name="forwardAction" value="" />
> <property name="account" ref="domain.account" />
> <property name="myList" ref = "myPageList" />
> </bean>
>
> <bean id="editAccountFormAction"
> class="com.home.practice.shop.EditAccountFormAction" scope = "prototype"
> parent = "baseAction"/>
>  <bean id="listOrdersAction"
> class="com.home.practice.shop.ListOrdersAction" scope = "prototype" parent
> = "baseAction"/>
>  <bean id="newAccountAction"
> class="com.home.practice.shop.NewAccountAction" scope = "prototype" parent
> = "baseAction"/>
>
> <bean id="newAccountFormAction"
> class="com.home.practice.shop.NewOrderFormAction" scope = "prototype"
> parent = "baseAction">
> <property name="order" ref="domain.order" />
> <property name="shippingAddressRequired" value="false" />
> <property name="confirmed" value="false" />
> <property name="cardTypeList">
> <list>
> <value>Visa</value>
> <value>MasterCard</value>
> <value>American Express</value>
> </list>
> </property>
> </bean>
>  <bean id="newOrderAction" class="com.home.practice.shop.NewOrderAction"
> scope = "prototype" parent = "baseAction">
> <property name="order" ref="domain.order" />
> <property name="shippingAddressRequired" value="false" />
> <property name="confirmed" value="false" />
> <property name="cardTypeList">
> <list>
> <value>Visa</value>
> <value>MasterCard</value>
> <value>American Express</value>
> </list>
> </property>
> </bean>
>
>
> <bean id="newOrderFormAction"
> class="com.home.practice.shop.NewOrderFormAction" scope = "prototype"
> parent = "baseAction">
> <property name="order" ref="domain.order" />
> <property name="shippingAddressRequired" value="false" />
> <property name="confirmed" value="false" />
> <property name="cardTypeList">
> <list>
> <value>Visa</value>
> <value>MasterCard</value>
> <value>American Express</value>
> </list>
> </property>
> </bean>
>  <bean id="removeItemFromCartAction"
> class="com.home.practice.shop.RemoveItemFromCartAction" scope = "prototype"
> parent = "baseAction">
> <property name="workingItemId" value="" />
> <property name="cart" ref="domain.cart" />
> </bean>
>
> <bean id="searchProductsAction"
> class="com.home.practice.shop.SearchProductsAction" scope = "prototype"
> parent = "baseAction"/>
>
>
> <bean id="signonAction" class="com.home.practice.shop.SignonAction" scope =
> "prototype" parent = "baseAction">
> <property name="username" value="" />
> <property name="password" value="" />
> <property name="repeatedPassword" value="" />
> <property name="languages">
> <list>
> <value>enlish</value>
> <value>japanese</value>
> </list>
> </property>
> <property name="categories">
> <list></list>
> </property>
> <property name="validate" value="" />
> <property name="forwardAction" value="" />
> <property name="account" ref="domain.account" />
> <property name="myList" ref = "myPageList" />
> </bean>
>
>  <bean id="updateCartQuantitiesAction"
> class="com.home.practice.shop.UpdateCartQuantitiesAction" scope =
> "prototype" parent = "baseAction">
> <property name="workingItemId" value="" />
> <property name="cart" ref="domain.cart" />
> </bean>
>
>
> <bean id="viewCartAction" class="com.home.practice.shop.ViewCartAction"
> scope = "prototype" parent = "baseAction">
> <property name="workingItemId" value="" />
> <property name="cart" ref="domain.cart" />
> </bean>
>
>
> <bean id="viewCategoryAction"
> class="com.home.practice.shop.ViewCategoryAction" scope = "prototype"
> parent = "baseAction"/>
>
> <bean id="viewItemAction" class="com.home.practice.shop.ViewItemAction"
> scope = "prototype" parent = "baseAction"/>
>
> <bean id="viewOrderAction" class="com.home.practice.shop.ViewOrderAction"
> scope = "prototype" parent = "baseAction">
> <property name="username" value="" />
> <property name="password" value="" />
>
> <property name="repeatedPassword" value="" />
>
> <property name="languages">
> <list>
> <value>enlish</value>
> <value>japanese</value>
> </list>
> </property>
> <property name="categories">
> <list></list>
> </property>
> <property name="validate" value="" />
>
> <property name="forwardAction" value="" />
>
> <property name="account" ref="domain.account" />
>
> <property name="myList"
> ref = "myPageList" />
> </bean>
>
> <bean id="viewProductAction"
> class="com.home.practice.shop.ViewProductAction" scope = "prototype" parent
> = "baseAction"/>
>  <bean id="domain.account" class="com.home.practice.shop.domain.Account"
> scope = "prototype"/>
> <bean id="domain.cart" class="com.home.practice.shop.domain.Cart" scope =
> "prototype"/>
> <bean id="domain.cartItem" class="com.home.practice.shop.domain.CartItem"
> scope = "prototype"/>
> <bean id="domain.category" class="com.home.practice.shop.domain.Category"
> scope = "prototype"/>
> <bean id="domain.item" class="com.home.practice.shop.domain.Item" scope =
> "prototype"/>
> <bean id="domain.lineItem" class="com.home.practice.shop.domain.LineItem"
> scope = "prototype"/>
> <bean id="domain.order" class="com.home.practice.shop.domain.Order" scope =
> "prototype"/>
> <bean id="domain.product" class="com.home.practice.shop.domain.Product"
> scope = "prototype"/>
> <bean id="myPageList" class =
> "org.springframework.beans.support.PagedListHolder" scope = "prototype"/>
>
>
> </beans>
>
>
> Regards,
> Srineel
>