You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Lidin NT <li...@kottsoftware.com> on 2025/03/26 08:37:57 UTC
Issue with ServletActionContext.getRequest() returning null in JSP (Struts 7)
Hi all,
I'm working on a Struts 7 application and facing an issue where userViewUtil is showing an empty value in JSP. Setup:
I'm using <s:bean> and <s:set> to create and set values in JSP:
<s:bean name="com.demo.common.util.UserViewUtilWrapper" var="userViewUtilWrapper"/>
<s:bean name="com.demo.common.util.ServletWrapper" var="servletWrapper"/>
<s:set var="userViewUtil" value="#userViewUtilWrapper.getUserViewUtil(#servletWrapper.getRequest())"/>
When I try to display the values, they are empty:
<s:property value="#userViewUtil"/>
<s:property value="#servletWrapper.getRequest()"/>
Code Details:
UserViewUtilWrapper Class:
This is the method from UserViewUtilWrapper class:
public UserViewUtil getUserViewUtil(HttpServletRequest request) {
return UserViewUtil.getUserViewUtil(request);
}
ServletWrapper Class:
This is the method from ServletWrapper class:
public HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
} Problem:
*
#servletWrapper.getRequest() returns null .
*
Therefore, #userViewUtilWrapper.getUserViewUtil(#servletWrapper.getRequest()) also becomes null .
Question:
1.
Why is ServletActionContext.getRequest() returning null in JSP?
2.
How can I fix this issue so that userViewUtil returns the expected value?
Any help would be greatly appreciated!
Thanks,
Lidin
Re: Issue with ServletActionContext.getRequest() returning null in JSP (Struts 7)
Posted by Lukasz Lenart <lu...@apache.org>.
śr., 26 mar 2025 o 09:38 Lidin NT <li...@kottsoftware.com> napisał(a):
> Why is ServletActionContext.getRequest() returning null in JSP?
Calling JSPs directly is a Bad Idea
https://struts.apache.org/security/#never-expose-jsp-files-directly
And calling JSPs directly means you skip the whole Action related
machinery, with security checks and so on. That's why the context is
empty.
> How can I fix this issue so that userViewUtil returns the expected value?
As already mentioned by Wolfgang, wrap JSP with an action. If you
using the Convention plugin it should be as simple as Wolfgang says or
you can use Wildcard mapping
Regards
Łukasz
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Issue with ServletActionContext.getRequest() returning null in JSP (Struts 7)
Posted by Wolfgang Knauf <wo...@gmx.de.INVALID>.
Hi,
how do you invoke the jsp? I had a (maybe) similar issue and learned
that I cannot invoke JSPs directly (by browsing to the jsp url), but
have to call an ".action" url instead.
Best regards
Wolfgang
Am 26.03.25 um 09:37 schrieb Lidin NT:
>
> Hi all,
>
>
>
>
> I'm working on a Struts 7 application and facing an issue where userViewUtil is showing an empty value in JSP. Setup:
>
>
> I'm using <s:bean> and <s:set> to create and set values in JSP:
>
> <s:bean name="com.demo.common.util.UserViewUtilWrapper" var="userViewUtilWrapper"/>
> <s:bean name="com.demo.common.util.ServletWrapper" var="servletWrapper"/>
> <s:set var="userViewUtil" value="#userViewUtilWrapper.getUserViewUtil(#servletWrapper.getRequest())"/>
>
>
> When I try to display the values, they are empty:
>
> <s:property value="#userViewUtil"/>
> <s:property value="#servletWrapper.getRequest()"/>
> Code Details:
> UserViewUtilWrapper Class:
>
>
> This is the method from UserViewUtilWrapper class:
>
> public UserViewUtil getUserViewUtil(HttpServletRequest request) {
> return UserViewUtil.getUserViewUtil(request);
> }
> ServletWrapper Class:
>
>
> This is the method from ServletWrapper class:
>
> public HttpServletRequest getRequest() {
> return ServletActionContext.getRequest();
> } Problem:
>
>
> *
>
> #servletWrapper.getRequest() returns null .
> *
>
> Therefore, #userViewUtilWrapper.getUserViewUtil(#servletWrapper.getRequest()) also becomes null .
>
> Question:
>
>
> 1.
>
> Why is ServletActionContext.getRequest() returning null in JSP?
> 2.
>
> How can I fix this issue so that userViewUtil returns the expected value?
>
>
>
> Any help would be greatly appreciated!
>
>
>
>
> Thanks,
>
> Lidin
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org