the same code doesn't work when I put into jstl tag - jstl

When I do like this
<c:choose>
<c:when test="${book.display}">
<eb:glyphicon-status icon="eye-open" value="${book.id}" />
</c:when>
<c:otherwise>
<eb:glyphicon-status icon="eye-close" value="${book.id}" />
</c:otherwise>
</c:choose>
It works fine.
But when I create tag showeye
<%# tag language="java" pageEncoding="UTF-8"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib tagdir="/WEB-INF/tags" prefix="eb"%>
<%# attribute name="isDisplay" required="true" rtexprvalue="true" type="java.lang.Boolean" %>
<%# attribute name="bookId" required="true" rtexprvalue="true" type="java.lang.Long" %>
<c:choose>
<c:when test="${isDisplay}">
<eb:glyphicon-status icon="eye-open" value="${bookId}" />
</c:when>
<c:otherwise>
<eb:glyphicon-status icon="eye-close" value="${bookId}" />
</c:otherwise>
</c:choose>
And when I put it on the page
<eb:showeye isDisplay="${book.display}" bookId="${book.id}" />
The condition does not work properly. I always got false result.
What is the problem?

Related

Spring MVC serve static file when submit a form

I'm running through weird behavior in Spring MVC. I try to make custom login page with spring security.
Here is my simple login page with a link to a style sheet login.jsp:
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
<link href="/resources/css/style.css" rel="stylesheet"/>
</head>
<body>
<h1>Login Form</h1>
<form:form action="/authuser" method="post">
<c:if test="${param.logout != null}">
<i>You have been logout!</i>
</c:if>
<c:if test="${param.error != null}">
<i>Invalid username or password!</i>
</c:if>
<p>
User name: <input type="text" name="username"/>
</p>
<p>
Password: <input type="password" name="password"/>
</p>
<input type="submit" value="Login"/>
</form:form>
</body>
</html>
when I fill out the form and hit submit it redirect me to http://localhost:8080/resources/css/test.css and serves me with stylesheet file!
when I remove the stylesheet link, it works fine and redirects me to the home page.

Using spring form tag inside a custom JSTL library

I need to create a custom JSTL tag which wraps multiple spring form tags. A single tag which produces the below content with custom attribute values as well.
<div class="col-md-4 col-sm-6 cal-xs-12">
<div class="form-group">
<label for="statusCode">Employee Status Code: </label>
<form:input path="statusCode" class="form-control" id="statusCode" value="${statusCode}" />
</div>
</div>
Is this achievable?
create a file formInputFiled.tag inside WEB-INF/tags/form directory.
formInputFiled.tag:
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# attribute name="id" required="true" rtexprvalue="true" %>
<%# attribute name="path" required="true" rtexprvalue="true" %>
<%# attribute name="label" required="true" rtexprvalue="true" %>
<%# attribute name="value" required="true" rtexprvalue="true" %>
<%# attribute name="parentDivClass" required="true" rtexprvalue="true" %>
<%# attribute name="divClass" required="true" rtexprvalue="true" %>
<%# attribute name="inputClass" required="true" rtexprvalue="true" %>
<div class="${parentDivClass}">
<div class="${divClass}">
<label for="${id}">${label}</label>
<form:input path="${path}" class="${inputClass}" id="${id}" value="${value}" />
</div>
</div>
Add taglib declaration in your jsp like below:
<%# taglib prefix="form" tagdir="/WEB-INF/tags/form" %>
Finally use the new tag like below:
<form:formInputFiled id="statusCode" path="statusCode" label="Employee Status Code:" value="${statusCode}" parentDivClass="col-md-4 col-sm-6 cal-xs-12" divClass="form-group" inputClass="form-control"/>

Set a value for <form: input> in Spring's form tag library

How can I possibly set a value for a form input field using the form tag library in Spring MVC shown below even if it has no value attribute and is not accepted according to form tld?
<form:input path ="name" id="name" cssStyle="display: none"/>
Not based on requirement:
<input type="hidden" id="name" value="Raven"/>
Don't use a <form:input>? It's for binding to beans.
You can use a regular <input> element ;)
If you want to set its value:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<c:set var="name" value="Raven" />
<input type="hidden" name="name" value="<c:out value='${name}' />" />

Spring 3.2.4 + Tiles 3 = infinite loop when resolving view?

I'm trying to use Spring MVC 3.2.4 & Tiles 3.0.1 with Regexp prefixes in my tiles.xml file. I'm a newbie with Tiles, so I might be doing something wrong, but have been trying to follow things I have read on in the Tiles doc site as well as apply stuff from the tech.finn.no blog.
Unfortunately, when I use completeAutoLoad=true with RegExp prefix in my tiles.xml file, I get stuck in an infinite loop when trying to resolve the view.
Stack Trace:
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.StackOverflowError
at javax.servlet.http.HttpServletResponseWrapper.encodeURL(HttpServletResponseWrapper.java:76)
at javax.servlet.http.HttpServletResponseWrapper.encodeURL(HttpServletResponseWrapper.java:76)
....
at javax.servlet.http.HttpServletResponseWrapper.encodeURL(HttpServletResponseWrapper.java:76)
at javax.servlet.http.HttpServletResponseWrapper.encodeURL(HttpServletResponseWrapper.java:76)
at javax.servlet.http.HttpServletResponseWrapper.encodeURL(HttpServletResponseWrapper.java:76)
at javax.servlet.http.HttpServletResponseWrapper.encodeURL(HttpServletResponseWrapper.java:76)
at org.springframework.security.web.context.SaveContextOnUpdateOrErrorResponseWrapper.encodeURL(SaveContextOnUpdateOrErrorResponseWrapper.java:181)
at javax.servlet.http.HttpServletResponseWrapper.encodeURL(HttpServletResponseWrapper.java:76)
at javax.servlet.http.HttpServletResponseWrapper.encodeURL(HttpServletResponseWrapper.java:76)
at javax.servlet.http.HttpServletResponseWrapper.encodeURL(HttpServletResponseWrapper.java:76)
....
at javax.servlet.http.HttpServletResponseWrapper.encodeURL(HttpServletResponseWrapper.java:76)
at javax.servlet.http.HttpServletResponseWrapper.encodeURL(HttpServletResponseWrapper.java:76)
at javax.servlet.http.HttpServletResponseWrapper.encodeURL(HttpServletResponseWrapper.java:76)
at javax.servlet.http.HttpServletResponseWrapper.encodeURL(HttpServletResponseWrapper.java:76)
at org.springframework.web.servlet.tags.UrlTag.createUrl(UrlTag.java:227)
at org.springframework.web.servlet.tags.UrlTag.doEndTag(UrlTag.java:174)
at org.apache.jsp.tag.webutil.load_002dscripts_tag._jspx_meth_spring_005furl_005f0(load_002dscripts_tag.java:300)
at org.apache.jsp.tag.webutil.load_002dscripts_tag.doTag(load_002dscripts_tag.java:113)
at org.apache.jsp.WEB_002dINF.views.tiles.layouts.default_jsp._jspx_meth_util_005fload_002dscripts_005f0(default_jsp.java:176)
at org.apache.jsp.WEB_002dINF.views.tiles.layouts.default_jsp._jspService(default_jsp.java:93)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
pom.xml:
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>3.0.1</version>
</dependency>
webmvc-config.xml:
<bean class="org.springframework.web.servlet.view.tiles3.SpringWildcardServletTilesApplicationContext" autowire="constructor" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" >
<property name="cache" value="false"/>
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="completeAutoload" value="true"/>
</bean>
Tiles.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="REGEXP:(.+)" template="/WEB-INF/views/tiles/layouts/default.jsp">
<put-attribute name="meta" value="/WEB-INF/views/tiles/{1}/meta.jsp"/>
<put-attribute name="head" value="/WEB-INF/views/tiles/{1}/head.jsp"/>
<put-attribute name="header" value="/WEB-INF/views/tiles/{1}/header.jsp"/>
<put-attribute name="menu" value="/WEB-INF/views/tiles/{1}/menu.jsp"/>
<put-attribute name="body" value="/WEB-INF/views/tiles/{1}/body.jsp"/>
<put-attribute name="footer" value="/WEB-INF/views/tiles/{1}/footer.jsp"/>
</definition>
</tiles-definitions>
default.jsp:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%# taglib prefix="util" tagdir="/WEB-INF/tags/util"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<jsp:directive.page pageEncoding="UTF-8" />
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<tiles:insertAttribute name="meta" ignore="true" />
<util:load-scripts />
<tiles:insertAttribute name="head" ignore="true" />
<spring:message code="application_name" var="app_name" htmlEscape="false"/>
<title><spring:message code="welcome_h3" arguments="${app_name}" /></title>
</head>
<body class="tundra spring">
<div id="wrapper">
<div id="header"><tiles:insertAttribute name="header" ignore="true" /></div>
<div id="menu"><tiles:insertAttribute name="menu" ignore="true" /></div>
<div id="main">
<div id="body"><tiles:insertAttribute name="body"/></div>
<div id="footer"><tiles:insertAttribute name="footer" ignore="true"/></div>
</div>
</div>
</body>
</html>
/WEB-INF/views/tiles/secure/login/body.jsp:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib prefix="util" tagdir="/WEB-INF/tags/util"%>
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<div class="container">
<form name="login" class="form-signin" action='<spring:url value="/j_spring_security_check"/>' method="POST">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" class="form-control" name="j_username" placeholder="Email address" autofocus>
<input type="password" class="form-control" name="j_password" placeholder="Password">
<label class="checkbox"> <input type="checkbox" value="remember-me"> Remember me</label>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
I don't think there is anything specifically special in this configuration, so am not sure why I get into an infinite loop. If I replace the CompleteAutoTilesInitializer class with the BasicTilesInitializerClass and use Wildcards (ie: /) instead and replace {1} with {1}/{2}, then the view loads properly (no infinite loop).
Any ideas what I am doing wrong, or is this a Tiles problem?
Found a similar SO issue (https://stackoverflow.com/a/6617343/827480) that pointed out that the regexp is too greedy and tries to match all attributes as well forever, hence causing the StackOverflow. Work around at the moment is to use a less greedy regexp.
Getting some traction under https://issues.apache.org/jira/browse/TILES-572 and http://thread.gmane.org/gmane.comp.apache.tiles.user/612

Struts2 ajax theme problem in IE9

We have an running application that is developed in struts-2.0.14. In this we used Ajax theme for displaying contents.
This ajax theme is running properly in all browsers except IE9 browsers, In IE9 this ajax theme making problem . The result is showing in new tab instead of showing in target element(div) specified by targets property.
Parent Page
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<s:head theme="ajax" debug="false" />
</head>
<body>
<s:url id="changePwd" action="changePassword" />
<s:div theme="ajax" id="pwdDiv" executeScripts="true" href="%{changePwd}" loadingText="Loading..."/>
</body>
</html>
Inner page
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags" %>
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<s:form action="changePassword" id="resetPassword" name="resetPassword">
<s:textfield name="username" id="username" />
<s:password showPassword="true" name="newpassword" id="newpassword" value="%{newpassword}"/>
<s:password showPassword="true" name="confirmpassword" id="confirmpassword" value="%{confirmpassword}"/>
<s:submit value="Confirm" showLoadingText="false" onclick="clearMsg();" theme="ajax" targets="pwdDiv" executeScripts="true" cssClass="userbutton" />
</s:form>
When Submitting the form in Inner Page the resulting page is popup into new tab. actually it should replace the content of pwdDiv.
Note: the same working properly in other browsers including (IE7,8)
Update:
making showLoadingText as true making the request as Ajax but the values are passed as null (password,confirm password ect,. -all fields)
Adding type="button" in s:submit button solve the problem. Don't know what is behind this !
<s:submit **type="button"** value="Confirm" showLoadingText="false" onclick="clearMsg();" theme="ajax" targets="pwdDiv" executeScripts="true" cssClass="userbutton" />

Resources