set Attribute via href - java

I am trying to set a variable (named as "o") in jsp in the body of tag - how could I do it without scriplets?
I have wrote this piece of code but it is not working:
<a class="overfl" href="myServlet?action=request.setAttribute('o',i)"> ${values[i]} </a>

Try with JSTL Core c:set Tag to set the attribute in any scope.
Sample code:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="salary" scope="request" value="${2000*2}"/>
ServletRequest#setAttribute() method doesn't return any value.
Get the value back in the same way as you are doing here ${values[i]}
or try with JSTL Core c:out Tag to get the value back.
In your case simply pass the action values as query parameter as shown below:
<a class="overfl" href="myServlet?action=${i}"> ${values[i]} </a>
And get the value back at server side using
String action = servletRequest.getParameter("action");

If the varaible is not already defined in your request's attribute so call <%request.setAttribute('o',i); %> then if you want to write it to the jsp output you have to to write <%request.getAttribute('o') %> in the place where you want to add it's value like this :
<%request.setAttribute('o',i); %>
<a class="overfl" href="myServlet?action=<%=request.getAttribute('o') %>"> ${values[i]} </a>

Related

Hybris: How to pass class as a parameter to cms:component?

I've started to learn Hybris and I want to find out how to pass the class as a parameter to a custom CMS Component (for the component's root element).
Let's suppose that in a jsp file named customNavigationComponent.jsp I have this piece of code:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="cms" uri="http://hybris.com/tld/cmstags"%>
<c:forEach items="${navigation.entries}" var="navigationLink">
<div class="custom-component-wrapper">
<cms:component component="${navigationLink.item}"/>
</div>
</c:forEach>
And the custom component looks like this:
<a href="${component.url}">
<div>${component.linkText}</div>
</a>
I would like to understand what I need to do to pass the class navigation-link as a parameter, like this:
<cms:component component="${navigationLink.item}" class="navigation-link"/>
so that the rendered result will be similar to:
<a href="stackoverflow.com" class="navigation-link">
<div>Lorem Ipsum</div>
</a>
There is no attribute called, class in cms:component and therefore the following statement will not work:
<cms:component component="${navigationLink.item}" class="navigation-link"/>
Please check https://help.sap.com/doc/a4265d5ea8314eb2929e6cf6fb8e35a5/1811/en-US/de/hybris/platform/cms2lib/cmstags/CMSComponentTag.html
In order to understand it better, you can compare it with c:forEach. At https://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/forEach.html, you will find a list of attributes available with c:forEach and if you want to dig deeper, you can further check https://tomcat.apache.org/taglibs/standard/apidocs/javax/servlet/jsp/jstl/core/LoopTagSupport.html
If you want to use your statement, you will need to create a custom tag.

Custom tag that evaluates its body based on the variables of the tag

Ok, now this is something for the hard core JSTL wizards I guess ;-)
I would like to have a tag whose body does not substitute the variables of the surrounding page. It should only take the variables into account that have been specified in the body of the tag or in the tag itself like this:
<c:set var="outsideVar" value="outside value"/>
<a:component>
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<div data-component-id="9">
<c:set var="componentId" value="9"/>
<c:set var="path" value='${abc:getCurrentPath()}_${componentId}'/>
<c:set var="resource" value='${abc:getResourceFromPath(path)}'/>
<c:set var="val" value="${resource.getValue('paragraphValue')"/>
<p>${fn:escapeXml(val)}</p>
${outsideVar}
</div>
</a:component>
The outsideVar variable should not be set and the body of the tag should behave like the content of a <jsp:include/>
This is a more specific question of this one:
Is it possible to create a local page scope in a JSP?
The link also explains the background of this question.
Your custom tag can grab and remove all page attributes before evaluating the body, and then clear and restore afterwards.

Why my included JSP file won't get processed correctly?

I am trying (and learning) to build a java web framework, and in the process of developing its' code generator based on the content of the database. In the view making process, I stumble in a difficulty, which I don't know how to solve it.
Firstly, I want all the pages to be created using the following index.jsp :
<body>
<%# include file="header.jsp" %>
<hr/>
<%# include file="body.jsp" %>
<hr/>
<%# include file="footer.jsp" %>
</body>
And, in the body.jsp, I want it to be like this :
<jsp:include page="${application_modul}" %>
Where application_modul is an attribute defined in its' controller this way :
request.setAttribute("application_modul","user_account\\view_user_account.jsp");
It can find the file correctly, but the processed jsp is not what I expected. Here :
<c:forEach items="[application.models.UserAccountModel#18a49e0, application.models.UserAccountModel#1f82982]" var="item" varStatus="status" >
<tr>
....
You can see the items attribute of jstl forEach, got its variable name (toString())...
Any Idea what the problem is????
I hope I describe my problem correctly
Many thanks!
PS :
I already create a quick fix for this, but not what I want it though. In the generated view_user_account.jsp, I do it like this :
<body>
<%# include file="header.jsp" %>
<hr/>
<c:forEach items="${row}" var="item" varStatus="status" >
<tr>
....
<hr/>
<%# include file="footer.jsp" %>
</body>
You can see that I create the whole file here...
EDITED:
PS : ${row} is an ArrayList populated with data from certain table
So, to summarize your problem in a single sentence, JSTL tags are not been parsed and they end up plain in generated HTML output?
You need to declare JSTL taglib in top of the JSP page where you're using JSTL tags to get them to run. For the JSTL core taglib, that'll be
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
I am not sure but, Try this...
index.jsp
<jsp:param name="parameterName" value="{parameterValue | <%= expression %>}" />

how to pass parameter to a javascript function using struts taglib?

I have a javascript function updateHiddenField(value) that gets a value as parameter and then sets this value as the value of a hidden field.
I have a JavaBean flight with parameters id and flightNo.
Problem: How can I pass the parameter id of JavaBean flight to the js function updateHiddenField(value) when using <html:link> tag(struts)?
<html:link href="javascript:updateHiddenField(idToPassHere)"><bean:write name="flight" property="flightNo"/></html:link>
Thanks
Umar
You don't seem to be gaining anything by using <html:link> over <a> - so that would be the easiest change:
<a href="javascript:updateHiddenField(<bean:write name="flight" property="id"/>)">
<bean:write name="flight" property="flightNo"/>
</a>
More generally I would suggest you add event listeners to your DOM objects, rather than mixing markup & functionality.
<html:link href="javascript:updateHiddenField(<s:property value="id"/>)"><bean:write name="flight" property="flightNo"/></html:link>
Assuming your taglib import is <% #taglib prefix="s" uri="/struts-tags" %>

Evaluate variable before passing to JSP Tag Handler

When trying to use a custom JSP tag library, I have a variable defined in JSP that I would like to be evaluated before being passed to the tag library. However, I cannot seem to get it to work. Here's a simplified version of my JSP:
<% int index = 8; %>
<foo:myTag myAttribute="something_<%= index %>"/>
The doStartTag() method of my TagHandler uses the pageContext's output stream to write based on the inputted attribute:
public int doStartTag() {
...
out.println("Foo: " + this.myAttribute);
}
However, the output I see in my final markup is:
Foo: something_<%= index %>
instead of what I want:
Foo: something_8
My tag library definition for the attribute is:
<attribute>
<name>myAttribute</name>
<required>true</required>
</attribute>
I have tried to configure the attribute with rtexprvalue both true and false, but neither worked. Is there a way I can configure the attribute so that it's evaluated before being sent to the Handler? Or am I going about this totally wrong?
I'm relatively new to JSP tags, so I'm open to alternatives for solving this problem. Also, I realize that using scriptlets in JSP is frowned upon, but I'm working with some legacy code here so I'm kind of stuck with it for now.
Edit:
I have also tried:
<foo:myTag myAttribute="something_${index}"/>
which does not work either - it just outputs something_${index}.
I don't believe that you can use a <%= ... %> within an attribute in a custom tag, unless your <%= ... %> is the entire contents of the attribute value. Does the following work for you?
<% int index = 8; %>
<% String attribValue = "something_" + index; %>
<foo:myTag myAttribute="<%= attribValue %>"/>
EDIT: I believe the <% ... %> within the custom tag attribute can only contain a variable name. not any Java expression.
To keep your JSP code clean and neat, avoid scripting when possible. I think this is the preferred way to do it:
<foo:myTag >
<jsp:attribute name="myAttribute">
something_${index}
</jsp:attribute>
</foo:myTag >
if your tag also contains a body, you'll have to change it from
<foo:myTag myAttribute="<%= attribValue %>">
body
</foo:myTag >
to
<foo:myTag >
<jsp:attribute name="myAttribute">
something_${index}
</jsp:attribute>
<jsp:body>
body
</jsp:body>
</foo:myTag >
<foo:myTag myAttribute="something_${index}"/>

Categories