So i have this command to call a jsp:
<jsp:include page="some.jsp" >
<jsp:param name="myValue" value="false" />
</jsp:include>
Cool. Now if I use that value in the jsp it's like this:
<c:if test="${param.myValue != 'false'}">
...
</c:if>
Ok. How do I send this to the jsp from a spring controller?
model.addAttribute("myValue",false);
return new ModelAndView("some", model);
I could not find out how to do this.
In the end I just had
model.put("someWord", false);
and in the jsp
<c:if test="${param.myValue != null}">
<c:set var="someWord" val="${param.myValue}"/>
</c:if>
<c:if test="${someWord != 'false'}">
...
</c:if>
Related
How can I read the principal object in jsp based on authentication type? Or maybe check the type of object?
Issue is Spring SAML sets username in Authentication.principal, and the UserDetails object in Authentication.details
So how can I switch between principal and details in jsp to fetch the user data?
I figured out the following:
<sec:authentication var="user" property="principal"/>
<c:if test="${user.getClass().simpleName == 'String'}">
<sec:authentication var="user" property="details"/>
<label>${user.userCompany}</label>
</c:if>
<c:if test="${user.getClass().simpleName != 'String'}">
<sec:authentication var="user" property="principal"/>
<label>${user.userCompany}</label>
</c:if>
But in this case I will have to repeat html tags, which I don't want to as it will lead to a lot of html duplicity.
Or how can I read the entire Authentication object in jsp?
Any suggestions?
After a lot of r&d, found the solution as below:
<sec:authentication var="userDetail" property="principal"/>
<c:choose>
<c:when test="${userDetail.getClass().simpleName == 'String'}">
<sec:authentication var="user" property="details"/>
</c:when>
<c:otherwise>
<sec:authentication var="user" property="principal"/>
</c:otherwise>
</c:choose>
I think this will do, until someone tells something better.
I need to add an additional navigation to my oracle webcenter portal project. This additional navigation should show the children of the selected node in navigation bar. Also, this additional navigation is should be in a left sidebar. How can i do this ?
<af:panelGroupLayout layout="vertical">
<c:forEach var="node" varStatus="vs" items="#{navigationContext.currentModel.listModel['startNode=/, includeStartNode=false']}">
<c:if test="${node.selected}">
<c:set value="${node.children}" var="childNodes" scope="session"/>
</c:if>
</c:forEach>
<c:if test="${childNodes ne null}">
<c:set var="childNodes" value="${navigationContext.currentModel.currentSelection.parent.children}"/>
</c:if>
<c:forEach items="#{childNodes}" var="node2">
<af:commandImageLink id="cil3" text="#{node2.title}"
actionListener="#{navigationContext.processAction}"
action="pprnav"
icon="#{node2.attributes[pageFlowScope.tnBean.iconKey]}"
disabled="#{not node2.navigable}"
inlineStyle="#{node2.onSelectedPath ? 'font-weight:bold;' : ''}">
<f:attribute name="node" value="#{node2}"/>
</af:commandImageLink>
</c:forEach>
</af:panelGroupLayout>
I'm using Spring Roo, and I have changed the list.tagx file to add a title at the top. This title is the value of a property of the first item. The problem is that I want to specify this property in a generic way, as an attribute of the list.tagx. Something like that:
<jsp:directive.attribute name="titleValue" type="java.lang.String"
required="false" rtexprvalue="true"
description="The value to be shown in the title" />
...
<c:when test="${not empty items}">
<c:if test="${not empty titleValue}">
<c:set var="variable" value="${items[0].titleValue}" />
</c:if>
...
There is a problem, because it tries to get the value of a property called 'titleValue' into the object items[0]. For instance, if I set the value of 'titleValue' as 'firstName', I want to get the value of items[0].firstName
Is it possible to do that?
Thanks in advance...
it is possible by using the spring:eval tag from the namespace xmlns:spring="http://www.springframework.org/tags". Try this:
<jsp:directive.attribute name="titleValue" type="java.lang.String"
required="false" rtexprvalue="true"
description="The value to be shown in the title" />
...
<c:when test="${not empty items}">
<c:if test="${not empty titleValue}">
<c:set var="variable">
<spring:eval expression="items[0].${titleValue}" />
</c:set>
</c:if>
...
EDIT: fixed typo, sorry. It wasn't ${items[0]}.${..} but items[0].${..}
Trying to do a very simple form in Spring/Hibernate. It should be adding an entry to the database. This is based off an example which worked for me, so it's weird that I would be getting this error. But it is what it is.
Here's the page with the form:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head><title>Add Owned Game</title></head>
<body>
<h1>Add Owned Game</h1>
<br />
<br />
<c:url var="saveGameeUrl" value="/games/save.html" />
<form:form modelAttribute="game" method="POST" action="${saveGameUrl}">
<form:label path="title">Game Title:</form:label>
<form:input path="title" />
<br />
<input type="submit" value="Add Game" />
</form:form>
</body>
</html>
And here's the relevant controller method:
#RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveGame(#ModelAttribute("game") Game game,
BindingResult result) {
gameService.addOwnedGame(game);
return new ModelAndView("redirect:/games/owned.html");
}
If you need to see anything else, let me know.
It looks like your are posting to the HTML page (which should be static) instead of the /save page which would route to a controller. Also, is it a typo? The c:url is named saveGameeUrl with two Es whereas the action only has one e on Game.
<c:url var="saveGameeUrl" value="/games/save.html" />
<form:form modelAttribute="game" method="POST" action="${saveGameUrl}">
Hi I am now using spring security. It works fine. But if login failed, no error message display. I am wondering how can I display error message?
I have configured the ResourceBundleMessageSource in my applicationContext.xml
<!-- Spring security error message config -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
And my security-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:David="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-2.0.xsd">
<David:http auto-config="true" access-denied-page="/accessDenied.html">
<!-- Don`t set any role restriction on login.jsp -->
<David:intercept-url pattern="/login.jsp"
access="IS_AUTHENTICATED_ANONYMOUSLY" />
<!-- Restrict access to All other pages -->
<David:intercept-url pattern="/admin.jsp"
access="ROLE_ADMIN" />
<!-- Set the login page and what to do if login fails -->
<David:form-login login-page="/login.jsp"
default-target-url="/"/>
<David:logout logout-success-url="/" />
</David:http>
<!-- Specify login examnination strategy -->
<David:authentication-provider>
<David:password-encoder hash="md5" />
<David:jdbc-user-service
data-source-ref="dataSource"
users-by-username-query="select username, password, status as enabled from user where username=?"
authorities-by-username-query="select u.username,r.name as authority
from user u
join user_role ur
on u.id=ur.user_id
join role r
on r.id=ur.role_id
where u.username=?" />
</David:authentication-provider>
</beans>
My jsp page:
<%# page contentType="text/html;charset=utf-8"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<%# page
import="org.springframework.security.ui.AbstractProcessingFilter"%>
<%# page
import="org.springframework.security.ui.webapp.AuthenticationProcessingFilter"%>
<%# page import="org.springframework.security.AuthenticationException"%>
<form id="myform" class="cmxform" method="post"
action="${pageContext.request.contextPath}/j_spring_security_check">
<fieldset>
<legend>
Please input correct username and password to login
</legend>
<p>
<label for="user">
Username:
</label>
<input id="user" name="j_username" />
</p>
<p>
<label for="pass">
Password:
</label>
<input type="password" name="j_password" id="password" />
</p>
<p>
<input type="submit" class="submit" value="Login" />
</p>
</fieldset>
</form>
Any suggestions? Any help will be appreciated.
<c:if test="${not empty param.login_error}">
<div class="error">
Your login attempt was not successful, try again.<br />
Reason: #{sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}
</div>
</c:if>
This works
<c:if test="${param.error != null}">
Error
</c:if>
Where is the jsp to actually display the error? Something like
<c:if test="${not empty param.error}">
<!-- Display error message -->
</c:if>
If you want to customize this message, you should also look at this
Maybe you can try this...put
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
for c tag on jsp page, and then put something like below for error msg display
<c:when test="${param.error == 1}">
<h3 style="font-size:20; color:#FF1C19;">Wrong id or password!</h3>
</c:when>
"param.error == 1" can get return value from Spring Security(security-config.xml) like
<beans:bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler">
<beans:property name="exceptionMappings">
<beans:props>
<beans:prop key="org.springframework.security.core.userdetails.UsernameNotFoundException">/login.action?error=1</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
This worked for me :
<c:if test="${not empty sessionScope.SPRING_SECURITY_LAST_EXCEPTION}">
<div class="error">
Your login attempt was not successful, try again.<br />
Reason: ${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}
</div>
</c:if>
I am using spring-security v 4.0.3.RELEASE
Using the not empty did not work for me. However checking for null worked for me.
<c:if test="${ param.error ne null}">
Display error message
</c:if>