Replacement for spring form tag in facelets - spring

since I know I can't use the Spring tag library in Facelets, I wonder if anyone can tell me what should I use instead of
<sf:form method="POST" modelAttribute="spitter">
.....
</sf:form>
Where prefix sf refers to (in JSP only):
<%# taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
I really like the idea of this form, that it binds all properties directly to modelAttribute object.
Is there any possibility that <h:form>...</h:form> can do the same?
Or is there any other tag, that can handle it?
I can't use JSP because i want to use PrimeFaces.
I'm just a beginner in J2EE, so please be patient :)
Thank you in advance

Yes, <h:form> does a similar thing. Even though the JSF approach is a bit different, the end result is similar: With spring the object gets submitted, and with jsf the field values end up in your managed bean. You will just have to use
<h:inputText value="#{bean.property}" />
(where bean is a #ManagedBean-annotated class (for jsf2), or declared in faces-config (for jsf1))
This difference is that the spring form is submitted to the target url, and spring finds the target method based on the mapping, while here you specify which method of the managed bean to invoke in your <h:commandButton />

Related

How to extend the lifetime of a bean?

I need some JSF 2.1.29 advice. I have a bean:
public class PlayerCardBean{
private Integer playerId;
//GET, SET, Other fileds and methods
}
The facelet playerCard.xhtml for the bean contains <a> tag for redirecting to another page:
<a href="javascript://"
onclick="openPage('#{request.contextPath}/panels/playerExternalTransactionList.html?playerId=#{playerCardBean.playerId}');">
<h:outputText value="#msgs['playerCard.externalTransactionList.all']}" />
</a>
I need this bean stay alived when the user is on the playerCard.xhtml as well as we're redirecting from the playerCard.xhtml to by the link within the <a> tag.
The view scope is smaller. The bean is going to be destroyed after redirecting.
How can I keep that bean alive when we're redirecting between those two views? By the <a> tag and probably by the "back"-button in a browser.
I think that storing the fields within a session is not a good idea because they are not a true-session attributes.
You can use the #ConversationScope. This scope is started and ended programmatically.
http://www.byteslounge.com/tutorials/java-ee-cdi-conversationscoped-example
I can think of the following two solutions:
Use the conversation scope. It requires annotating your bean with the #ConversationScoped annotation and you will need to manage (begin and end) the conversation programatically. (This answer contains some useful sample code.)
Use the Flash.
There is no perfect solution in JSF 2.1 but you can create your own scope which will be good replacement for conversation scope.
More details:
https://blog.oio.de/2012/07/24/jsf-2-custom-scopes-without-3rd-party-libraries/

Is it possible to use Spring SpEL expression in c:set to instantiate new class?

I would like to do something like:
<c:set var="customer" value="${new com.test.Customer()}" />
but this doesn't work.
Is there something like this possible?
You can use the new spring:eval JSP tag that allows you to evaluate SpEL expressions from JSP pages, instead of c:set.

<form:form modelAttribute .. using two beans in JSP

This is a spring based application. My form definition in JSP is as below,
<form:form modelAttribute="article" enctype="multipart/form-data" method="POST"
action="${sendEmailUrl}" name="postAd" id="postAd">
I want to display some data from 'article' bean on my form so I've defined modelAttribute='article'. Till here it is all fine. But on submit of the form, I want to collect data in different bean than article. Since I can define modelAttribute only once in the form, can someone please advise how can I use two beans in JSP?
P.S. In case I am not clear, let me give more details. On submit of form, data entered by user will be collected in bean 'X' and email will be sent using java email. But bean 'Y' (article in this case) is holding some values which needs to be displayed on the form.
Hope I am clear.
You could create a FormBean class, containing the two beans you said. Use this new class as a modelAttribute in your form and you will be able to access to both object's properties.
public class FormBean {
public Article article;
public YourOtherObject yourOtherObject;
}

spring mvc form handling without using spring tag

Recently, I have been researching a new framework for the purpose of building a web-application. To this end, I wanted to try out Spring MVC. Of the many parameters for evaluating a framework, one is that I don't want to be bound to the tag libs associated with the framework to make use of the HTTP request parameter -> Java bean translation. The Spring MVC documentation repeatedly mentions that it is possible to do view related things with only JSTL and no Spring tags, however, I haven't found a way to get the Request-to-Bean translation feature [SimpleFormController] to work without Spring tags.
As of now, the only way seems to extract the request parameters one by one and set to my bean. Is there any way to perform this translation w/o using framework dependent tags?
I appreciate your inputs!
I use Spring Web MVC without Velocity templates (non-JSP templating). To answer your question, you need to understand how Spring performs data binding. Basically, it's all in the name you give your input elements. E.g
<input name="properytOne" value="1" type="hidden">
<input name="properytTwo" value="2" type="hidden">
<input name="rich.property3" value="3" type="hidden">
will bind values to an object like this
class CommandOne {
private String propertyOne;
private String popertyTwo;
private CommandTwo rich;
// Getters and setters
}
class CommandTwo {
private String propertyThree;
// Getters and setters
}
You also have to be sure to instantiate your command object, but that will be handled in your SimpleFormController.
Spring tags are completely optional.
Read chapter 15, 16, and 17 of the Spring Reference Document You can use annotations to retrieve request parameters with your controller (see section 15.3).
As per my understanding, what you are trying to achieve is Binding your form to your Bean Class, which is very nicely implemented in JSF. JSF works on component architecture and very easy to start with, plus it has many component builers available such as primefaces, omnifaces, icefaces, openfaces, etc. Reusability of self-designed components can help you a lot in specific projects. Try giving a chance to JSF. Thanks, hope this was helpful.

Get Request and Session Parameters and Attributes from JSF pages

I'm using JSF with facelets and I need to get the request and session parameters inside the JSF page. In JSP pages I got this parameter like that: "${requestScope.paramName}" or "${sessionScope.paramName}". But now after using JSF there are only beans and you can't get any value except bean attributes.
NOTE: The session attributes what I need is auto filled using acegi security so I can't get any access to them.
So what to do now?
You can get a request parameter id using the expression:
<h:outputText value="#{param['id']}" />
param—An immutable Map of the request parameters for this request, keyed by
parameter name. Only the first value for each parameter name is included.
sessionScope—A Map of the session attributes for this request, keyed by
attribute name.
Section 5.3.1.2 of the JSF 1.0 specification defines the objects that must be resolved by the variable resolver.
You can also use a bean (request scoped is suggested) and directly access the context by way of the FacesContext.
You can get the HttpServletRequest and HttpServletResposne objects by using the following code:
HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
HttpServletResponse res = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
After this, you can access individual parameters via getParameter(paramName) or access the full map via getParameterMap() req object
The reason I suggest a request scoped bean is that you can use these during initialization (worst case scenario being the constructor. Most frameworks give you some place to do code at bean initialization time) and they will be done as your request comes in.
It is, however, a bit of a hack. ;) You may want to look into seeing if there is a JSF Acegi module that will allow you to get access to the variables you need.
You can either use
<h:outputText value="#{param['id']}" /> or
<h:outputText value="#{request.getParameter('id')}" />
However if you want to pass the parameters to your backing beans, using f:viewParam is probably what you want. "A view parameter is a mapping between a query string parameter and a model value."
<f:viewParam name="id" value="#{blog.entryId}"/>
This will set the id param of the GET parameter to the blog bean's entryId field. See http://java.dzone.com/articles/bookmarkability-jsf-2 for the details.
You can like this:
#{requestScope["paramName"]} ,#{sessionScope["paramName"]}
Because requestScope or sessionScope is a Map object.
You can also use a tool like OcpSoft's PrettyFaces to inject dynamic parameter values directly into JSF Beans.
Assuming that you already put your object as attribute on the session map of the current instance of the FacesContext from your managed-bean, you can get it from the JSF page by :
<h:outputText value="#{sessionScope['yourObject'] }" />
If your object has a property, get it by:
<h:ouputText value="#{sessionScope['yourObject'].anyProperty }" />
Are you sure you can't get access to request / session scope variables from a JSF page?
This is what I'm doing in our login page, using Spring Security:
<h:outputText
rendered="#{param.loginFailed == 1 and SPRING_SECURITY_LAST_EXCEPTION != null}">
<span class="msg-error">#{SPRING_SECURITY_LAST_EXCEPTION.message}</span>
</h:outputText>
In the bean you can use session.getAttribute("attributeName");

Resources