Set request attribute using JSTL - java

I have the following code:
<bean:define id="hasDocuments" name="BudgetSimulationDetailForm" property="hasDocuments" type="java.lang.Boolean"/>
<%
request.setAttribute("enablebtnRelatedDocs", "true");
request.setAttribute("hasDocuments", String.valueOf(hasDocuments));
%>
I want to remove the scriptlet, I tried using c:set with different scopes but it didn't work.
Is it possible to set a request attribute using JSTL tags?
I tried this and did not work:
<c:set name="enablebtnRelatedDocs" value="true" scope="request"/>
and also
<c:set name="enablebtnRelatedDocs" value="${true}" scope="request"/>
Afterwards there is an include:
<jsp:include page="/gema/jsp/includes/detail/top_Detail.jsp">
<jsp:param name="title_key" value="${title}" />
<jsp:param name="title_bundle" value="buc" />
<jsp:param name="standard_buttons_include" value="true" />
<jsp:param name="typeId" value="53555" />
<jsp:param name="detail" value="budget" />
</jsp:include>
Inside the included JSP the request attribute is not visible, apparently.

Sounds Good, You want to use JSP Standard Tag Library instead of Scriplet.
Yes, It's possible using c:set. Read more about Core Tag Library
<c:set var="enablebtnRelatedDocs" value="${true}" scope="request"/>
<c:out value="${requestScope.enablebtnRelatedDocs }"/>
By default c:set set a attribute in page context. you can set it in any scope.

By default, the JSTL Core library function "set" accepts the following attributes:
JSTL Core set property (credits to tutorialspoint.com) :
value,
target,
property,
var,
scope
You should use "var=" instead of "name=". Hope this helps!
Happy coding!
1:

Related

JSP read param section

I have a problem with passing parameters from one jsp page to the other one. Code looks like this:
home page:
<jsp:include page="page1.jsp">
<jsp:param name="par1" value="1"/>
<jsp:param name="par2" value="2"/>
<jsp:param name="par3" value="3"/>
<jsp:param name="par4" value="4"/>
</jsp:include>
and reciever:
<c:set var="a" value="${param.par1}" />
<c:set var="b" value="${param.par2}" />
<c:set var="c" value="${param.par3}" />
<c:set var="d" value="${param.par4}" />
<label>
"${a}"<br/>
"${b}"<br/>
"${c}"<br/>
"${d}"<br/>
"${param}"
and the browser displays just empty string.
What is wrong with the param attribute?
EDIT: It is liferay app.
The values get lost when you send the values in the way mentioned because of the scope.
Read about different scopes in servlets and Jsp.
It is better you have a servlet (intermediate b/w Jsp's) before sending the values to another jsp page.
First you send the values to a servlet.In the servlet, you have a HttpServletRequest object.
Create your Param object and set the values.
Set the Param object in HttpServletRequest object using setAttribute().
Then use RequestDispatcher to go to another jsp page.
Now you should be able to see the values.
if your receiver code is inside page1.jsp then it will work.

Where can the variable in jsp be defined except java classes?

Suppose I have the following code:
<c:when test="${isFoo}">
Where can isFoo be defined except:
java classes
<c:set ... /> construction ?
You can also set it using c:set. For instance:
<c:set var="isFoo" value="true" scope="page" />
In JSP itself by scriptlets. E.g. as a request attribute:
<% request.setAttribute("isFoo", true); %>
Scriptlets are however discouraged since over a decade.
By an implicit EL variable. E.g. as a request parameter:
http://example.com/page.jsp?isFoo=true
Which can be accessed by ${param}:
<c:if test="${param.isFoo}">
There are many more, you can find them all in this overview.
See also:
Our EL (Expression Language) tag wiki page

Struts2 anchor tag doesn't include contextPath

%{#request.contextPath} doesn't work inside an s:a tag in Struts2. (Struts 2.2.1 to be specific.) Is there a way to make it work? It works in other Struts2 tags.
Here are two lines in a JSP file in a Struts 2 project whose context path is "/websites":
<s:a href="%{#request.contextPath}/clickme" theme="simple">Click here.</s:a>
<s:form method="post" action="%{#request.contextPath}/submitme" theme="simple"></s:form>
And here is the output:
Click here.
<form id="submitme" name="submitme" action="/websites/submitme" method="post"></form>
Notice that the context path is left off the anchor but is included in the form.
P.S. I can't use ${#pageContext.request.contextPath} here because ${} isn't allowed in Struts2 tags. Besides, I'm trying to be consistent. And I also try generally to avoid ${} since it does not auto-escape the output.
Thanks!
This should work:
<s:set id="contextPath" value="#request.get('javax.servlet.forward.context_path')" />
<s:a href="%{contextPath}/clickme" theme="simple">Click here.</s:a>
However, you're not supposed to do this. When you need an url, use the <s:url> tag:
<%-- Without specifying an action --%>
<s:url id="myUrl" value="clickme" />
<s:a href="%{myUrl}" theme="simple">Click here.</s:a>
<%-- With an action --%>
<s:url id="myUrl" action="clickme" />
<s:a href="%{myUrl}" theme="simple">Click here.</s:a>
By the way, you don't need a context path for the action attribute of a form:
<s:form method="post" action="submitme" theme="simple"></s:form>
Does Struts 2 support EL?
You can use ${request.contextPath} if it does....

Struts Nested Tag with Dynamic Parameters

I have a legacy Struts 1 application which uses the nested tag. Can I inject a dynamic parameter into the nested tag? For example,
<nested:select disabled="<c:out value='${requestScope.disableSelectBox}' />" />
I also tried doing:
<nested:select disabled="${requestScope.disableSelectBox}" />
In both of the above examples, the disabled attribute was not properly set and it was ignored. If I printout the value with a c:out, the correct value of disableSelectBox is displayed:
<c:out value="${requestScope.disableSelectBox}" />
A colleague suggested that I should use:
<nested:select disabled="<%=request.getAttribute("disableSelectBox"); %>" />
The trouble is that it is considered bad practice to use java scriplets in a JSP page. Is there any way to embed a dynamic variable into a Struts 1 nested tag? Switching to Struts 2 is not an option.
Thanks!
Struts 1 (as far as I can remember) cannot allow you to do:
<nested:select disabled="<c:out value='${requestScope.disableSelectBox}' />" />
As it can't process JSP tags inside any of their attribute declarations, Check what nested:select disabled attribute required needs.
But Struts do support EL and JSP Scriplets (so your colleague is correct). JSP Scriptlet will "render" the value of the <%=request.getAttribute("disableSelectBox"); %> and assign it to the <nested:select disabled="<%=request.getAttribute("disableSelectBox"); %>" />
So (if I assume that the values returns a true or false,
<nested:select disabled="${requestScope.disableSelectBox}" />
and
<nested:select disabled="<%=request.getAttribute("disableSelectBox"); %>" />
will be rendered as (if results returns true)
<nested:select disabled="true" />
before it is sent to Struts to render the nested tag (sorry for using the word "render", you can use translate if you want).

c:set tag to set a non-primitive type value

What's a possible way to use Spring Security tag <sec:authentication property="principal.id" /> as the value for the <c:set…> tag?
These statements:
<c:set var="userId" value="<sec:authentication property='principal.id' />"/>
<c:set var="userId" value="<sec:authentication property=\"principal.id\" />"/>
won't work.
There is no reason to use «value» attribute in your case. Use <c:set> tag in this way:
<c:set var="userId">
<sec:authentication property="principal.id"/>
</c:set>

Categories