I want to use below code in jstl.
String shouldFollow=(String)request.getAttribute("shouldFollow");
How can i do so in jstl
Firstly, it might do you good to research the difference between JSTL and the Expression Language.
Your line of code can be wrapped in an expression like so:
${ shouldFollow }
The PageContext object will resolve the expression by searching the attribute mapping of the pageContext, request, session, and servletContext scopes respectively given this syntax.
You can use expressions for the values of attributes of JSTL tags:
<c:if test="${ shouldFollow }">
...
</c:if>
Where the body of this tag will execute if shouldFollow is true.
Related
I'm trying to separate my Java Code from my JSP files and I'm having a bit of a problem. I'm trying to find out whether the user is a guest or not and then printing the appropriate action EG login form or their Username.
Heres my index.jsp file:
<% if(view.guest) { %>
<%= "Scriptlet: Login Form Here" %>
<% } else { %>
<%= "Scriptlet: User Name Here" %>
<% } %>
<br/><br/>
<c:choose>
<c:when test="${view.guest eq true}">
JSTL Tag: Login Form Here
</c:when>
<c:otherwise>
JSTL Tag: User Name Here
</c:otherwise>
</c:choose>
This produces the following output:
Scriptlet: Login Form
JSTL Tag: User Name
As you can see the Scriptlet produces the expected results, but the JSTL tags produce the opposite. Infact if I reverse the JSTL's conditional to false (for debugging purposes) it still produces the same result "JSTL Tag: User Name"
Btw view.guest is a public boolean variable of the object view.
The JSP EL doesn't access local variables. It accesses attributes of the page, request, session or application scope. And it also assumes you're not using public fields (which should never be used), but respect the Java Bean conventions.
And, just as in Java, comparing a boolean with true is unnecessary. You just need
<c:when test="${view.guest}">
And the condition will evaluate to true if there is a page, request, session or application attribute named "view", having a public getGuest() or isGuest() method returning true.
If you didn't use scriptlets at all, you would never have local variables in your pages, and you would never have this problem.
I've seen a lot of examples online of code where a method is called with a "f:ajax" tag but the name of the method gets shortened in the tag when the method name starts with "get". I haven't been able to find the reason for this. Below is an example of what I mean.
For instance, in the xhtml file "sayWelcome" is called:
...
<h:commandButton value="Welcome Me">
<f:ajax execute="name" render="output" />
</h:commandButton>
<h:outputText id="output" value="#{helloBean.sayWelcome}" />
...
but the method in the bean is called "getSayWelcome":
public String getSayWelcome(){
return name;
}
Why does the "get" get dropped from the method name in the "f:ajax" tag?
The JavaBeansSpecification defines the naming convention for the properties adding the get and set before the functions that acts as properties, see this article for more info.
The expression language that use JSF also complains the JavaBeans properties name convention you can check this in this article, refer to the Referring to Object Properties Using Value Expressions section.
So in conclusion when the EL (Expression language) finds a sentence as <h:outputText id="output" value="#{helloBean.sayWelcome}" /> it will try to call a getter or the setter function for the sayWelcome property, it depends if it need to assign the value (setter) or get the value (getter).
I have a map which contain request parameters and their value, which will be used later on the jsp page. I'm usinng jsp page inclusion later and I don't know what exact params would I use
How I suppose, the filling should be implemented:
<c:forEach var="theParameter" items="${ parametersMap }" >
<c:set var="${theParameter.key}" value="${theParameter.value}" />
</c:forEach>
But I'm getting the error that the 'var' attribute can't use expression
Do you have any ideas about workaround?
EDIT:
Example:
parametersMap = [ 'param1' : 'value1' ; 'param2' : 'value2']
as result I would like something like this:
<c:set var="param1" value="value1" />
<c:set var="param2" value="value2" />
You need to do like this
<c:set target="${RequestScope[aNewMap]}"
property="${theParameter.key}" value="${theParameter.value}"/>
I solved my problem by adding custom tag, which on java part retrieves the Hashmap object and fill the request (which can be retieved from pageContext)
I have a list containing users. I am trying to print it in JSP but some how I am not able to get it to print it. Getting this exception HTTP Status 500 - javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'users[0]' available as request attribute
Code in JSP
<c:forEach items="${users}" var="user" varStatus="status">
<spring:bind path="users[${status.index}].name">
<c:out value="${status.value}" />
</spring:bind>
</c:forEach>
Controller
ModelAndView modelAndView = new ModelAndView("go_some_JSP_page");
List<UserEntity> users = userManager.getAllObjects();
modelAndView.addObject("users", users);
BTW, UserEntity has name field. If I remove the binding and try to print the user.name using <c:out value="user.name" /> it prints the value
Where am I going wrong and what do I need to do? Thanks
Not working code below. [I have to invoke formatting on field #NumberFormat so have to try it using status variable]
<spring:bind path="user.name">
<c:out value="${status.value}" />
</spring:bind>
Gets this error --> javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
So added a bean binding and then I get empty table :(. I believe thats because the instance is empty. So this does not seems like a right approach.
#ModelAttribute("user")
public UserEntity userEntityBinding() {
return UserEntity.newInstance();
}
A working code exists at https://github.com/hth/StatusInvoke.git
Let me know if you face any problem deploying it.
This question has been solved. Thanks for looking at it.
You can try using LazyList instead of simple list. If you want to take a look at the example then you can refer one of my question. In the question statement I have mentioned how to use the LazyList.
Hope that helps you. Cheers.
this, if the modelandview are returned, is the correct way to populate the list
ModelAndView modelAndView = new ModelAndView("go_some_JSP_page");
List<UserEntity> users = userManager.getAllObjects();
modelAndView.addObject("users", users);
And this is the correct way to reference the list
<c:forEach items="${users}" var="user" varStatus="status">
<spring:bind path="user.name">
<c:out value="${status.value}" />
</spring:bind>
</c:forEach>
Your problem must be elsewhere is the name field definitely populated, is the correct jsp being called... the above code is correct and should work.
The correct answer to invoke #NumberFormat annotation is by using spring:eval expression tag
<spring:eval expression="user.balance" />
This invokes the annotation and performs formatting as mentioned in the annotation
I don't think you can use spring:bind in that case, AFAIK it tries to get the variable from the ModelMap, it's not able to get it from the "for" var.
I just want to send the value of my dropdownlist with a requestparameter. In my case being
Kidscalcula_web/start.htm?klasid=myValueHere
I know a way of doing this but it sounds so irrational to use it for this. If I was bored I'd probably write some jQuery to do a post and send the parameter for instance.Now it really sounds like a very bad idea to manually make my requeststring, since Spring takes care of that. So how could I make a simple form that just sends my dropdownvalue to my controller?
It's just that I can't find something so trivial anywhere, and one of you can probably help me out quickly.I suppose the controller would be just as trivial as:
#RequestMapping(value = "post")
public String postIndex(#RequestParam("klasid") String klasid, HttpServletResponse response,
HttpServletRequest request) {
}
But I really can't find any examples on how to make a JSP to send me that value. Is this possible with the <form>taglib ?
The <form> taglib is generally used with form-backing command objects, rather than being bound to the controllers using individual #RequestParam arguments. This is why you won't see any documentation examples of that combination being used together.
For example, rather than having #RequestParam("klasid"), you'd have a command class with a field called klasid, and Spring would bind the whole lot together:
#RequestMapping(value = "post")
public String postIndex(#ModelAttribute MyCommandClass command) { /../ }
This makes sense when you consider that forms typically have multiple parameters, and it'd get cumbersome to declare them all using #RequestParam.
Having said that, you can still do it - any form controls will generate request parameters that #RequestParam can bind to, but if you choose to deviate from Spring MVC's form-backing command pattern, then it's quite awkward.
You don't even need a taglib to send this request. You can create a simpliest HTML form with method = "GET" (what is the default value of method):
<form action = "...">
<select name = "klasid">
<option value = "value1">Option 1</option>
<option value = "value2">Option 2</option>
<option value = "value3">Option 3</option>
</select>
<input type = "submit" />
</form>