Java Web: How use JSP with JSF and Ajax? - ajax

i'm working in a project with jsp, i need to start to use ajax too.
i'm enjoying jsf , is possible work with these together (JSP, JSF and AJAX) ?
I'm asking this cause i could run jsf with ajax
<h:commandButton id="bt_save" value="Save" title="Save" action="#personMBean.clickSave()}">
<f:ajax execute="#form" render="lblMessage" />
</h:commandButton>
<br/>
<h:outputLabel id="lblMessage" value="#{personMBean.message}" />
But when i try input this code in my jsp page it doesn't work, even if i insert on my jsp page:
<%#page contentType="text/html"%>
<%#page pageEncoding="UTF-8"%>
<%#taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%#taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
Any ideas ?

Ensure that you're using JSF 2.x (the <f:ajax> doesn't exist in JSF 1.x) and that you've a <h:head> instead of <head> in the master template (so that JSF can auto-include necessary Ajax JavaScripts).
Said that, you should really consider Facelets as replacement of JSP. JSP is an outdated view technology and ill suited for templating and has been replaced by Facelets as per JSF 2.0 / Java EE 6.

Yes - try richfaces or primefaces

I don't see why you couldn't do this but certainly not on the same page.
To do this you need to map the *.jsp extension to the JSP servlet while mapping the *.jsf or *.xhtml to FacesServlet.

Related

AjaxRenderKitFactory is blocking AJAX in my web project

I am facing an issue related to Ajax in JSF 2.0
We have 2 web projects (WARs) in the same EAR. the first project is having the following in the faces-config.xml
<factory>
<faces-context-factory>
com.ibm.faces.context.AjaxFacesContextFactory
</faces-context-factory>
<render-kit-factory>
com.ibm.faces.renderkit.AjaxRenderKitFactory
</render-kit-factory>
</factory>
For some reason, the com.ibm.faces.renderkit.AjaxRenderKitFactory is blocking ajax in the other web project, if I removed this section, ajax works fine.
I mean by Ajax here, the rendering for
<h:form>
<h:selectOneMenu value="#{myBean.selected}">
<f:selectItem itemValue="#{null}" itemLabel="ABC" />
<f:selectItem itemValue="one" />
<f:selectItem itemValue="two" />
<f:selectItem itemValue="three" />
<f:ajax listener="#{myBean.listener}" render="result" />
</h:selectOneMenu>
<h:outputText id="result" value="#{myBean.selected} #{myBean.result}" />
but the following is working fine
<h:commandButton value="commandButton" action="#{myBean.submit}">
<f:ajax listener="#{myBean.listener}" render="result" />
</h:commandButton>
<h:outputText id="result" value="#{myBean.selected} #{myBean.result}" />
I am not having this com.ibm.faces.renderkit.AjaxRenderKitFactory in my faces-config so why it is affecting Ajax in my project?
The com.ibm.faces.renderkit.AjaxRenderKitFactory is designed to enable ajax support in jurassic JSF 1.x versions which didn't have built-in ajax support.
Since JSF 2.0 (Dec 2009), JSF got built-in ajax support with the new <f:ajax> tag. Therefore, external ajax libraries designed for JSF 1.x such as those from IBM becomes unnecessary and they would possibly even conflict, as you faced.
Just remove it altogether. In JSF 2.x and newer you don't need external libraries to use ajax. Moreover, you should upgrade any JSF 1.x component libraries to a JSF 2.x compatible one.

How to use Spring Webflow popup="true" with Primefaces?

Until now we were using Spring Webflow 2.3, which brought the "sf" namespace and Spring.js, which could be used to display view-states in a popup.
Webflow 2.4 deprecated Spring.js and removed the taglib with the following statement:
Previous releases of Spring Web Flow shipped with a component library which provided Ajax and client-side validation capabilities for JSF 1.2 environments. Applications using these components will need to switch to a 3rd party JSF component library such as PrimeFaces or RichFaces.
See: http://docs.spring.io/spring-webflow/docs/current/reference/htmlsingle/#spring-faces-upgrade-from-swf23-components
Now i have added a dependency to Primefaces 5.2 to my pom.xml and everything seems to work fine, except for SWF Popups, which simply redirect to the new view-state.
First page:
<ui:composition template="#{templatePath}"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<ui:define name="content">
<h:form id="formDashboard">
<p:commandButton id="addWidget" value="Hinzufügen" action="add-widget"/>
</h:form>
</ui:define>
</ui:composition>
Second page (should be rendered in a popup):
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<ui:fragment id="popupFragment">
abc
</ui:fragment>
</ui:composition>
Flow definition:
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow.xsd">
<view-state id="start" view="dashboard.xhtml">
<transition on="add-widget" to="popup-addWidget"/>
</view-state>
<view-state id="popup-addWidget" view="popup-addWidget.xhtml" popup="true">
<on-render>
<render fragments="popupFragment"/>
</on-render>
</view-state>
</flow>
Spring configuration:
<bean class="org.springframework.faces.webflow.JsfFlowHandlerAdapter">
<property name="flowExecutor" ref="flowExecutor"/>
</bean>
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="flowRegistry" ref="flowRegistry"/>
</bean>
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
<webflow:flow-execution-listeners>
<webflow:listener ref="facesContextListener"/>
</webflow:flow-execution-listeners>
</webflow:flow-executor>
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices"/>
<!-- Populates the flowRegistry on startup. Removing this and manually adding all flows changes nothing. -->
<bean class="config.FlowRegisteringBeanPostProcessor"/>
<faces:flow-builder-services id="flowBuilderServices"/>
<faces:resources/>
<bean id="facesContextListener" class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener"/>
I'm not including the web.xml, since there is nothing special.
Using Mojarra 2.2, Primefaces 5.2, Spring 4.2 and Webflow 2.4.1.
Using Firebug i can see that the server POST response issues to redirect, which JSF/Primefaces honors and does a full redirect to the new view-state instead of showing a popup:
<?xml version='1.0' encoding='UTF-8'?>
<partial-response id="j_id1"><redirect url="/Demo/spr/dashboard?execution=e1s2"></redirect></partial-response>
If anybody managed to get popup="true" working with these versions, with or without Spring.js, i'd be happy to hear about it.
We ended up using Primefaces' p:dialog and p:confirmDialog, instead of Webflow popups.
This doesn't really answer my question, but i can't see an answer to this seeing that Webflow sends the same response no matter if popup is set to true or false.

spring security authentication state check

i have not casual issue with the Spring MVC+Spring security.
There was emergency electricity switch-off, and as i worked with intellij IDEA on my proj, some random opened files was lost.
I recover everything, but now having a really strange issue
This is how i check authentication state on my index.jsp page
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%# taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<sec:authorize access="hasAnyAuthority('ROLE_ADMIN', 'ROLE_USER')" var="isAuthenticated"/>
${isAuthenticated}
<c:choose>
<c:when test="${isAuthenticated}">
**do smth**
</c:when>
<c:otherwise>
**do smth**
</c:otherwise>
</c:choose>
So, the problem is:
After successful login, i redirected to user page, i can use 'SecurityContextHolder.getContext().getAuthentication().getName()'
username from context, etc, everything is ok.
But for some reason i can't receive an authentication state,
${isAuthenticated}
always have 'false' value.
What should i look for? i don't get any errors.
Authenticated use have role Admin/user.

spring framework mvc localization

I am trying to add Internationalization and Localization support to our Spring MVC application.
I made encoding like this in *-servlet.xml
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
But I found wrong character like below
I cannot figure out what problem I should fix it. If possible, please let me know.
I've already added in jsp page like this:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
But it doesn't work.
The defaultEncoding property of ReloadableResourceBundleMessageSource is used to
Set the default charset to use for parsing properties files. Used if
no file-specific charset is specified for a file.
It has no bearing on how the client is reading the response. If you are generating your response with a jsp, you can give it this line at the start
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
so that the client knows that you are providing data encoded with the UTF-8 charset.
If you are not using a jsp, there are other ways to set the content-type or content-encoding, directly from HttpServletResponse or from a returned ResponseEntity object.
update your default encoding with:
<property name="defaultEncoding" value="ISO-8859-1" />
and this should work for rendering characters with accents.
At least it works on my spring projects (french and european languages/users)
If this is not an option for you (bigger audience of targetted users) try to add this in your jsp:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
...
</head>

Spring security with pages in velocity

How can I use Spring security when my pages are not in JSP but Velocity?
is there something like this tag with JSP:
<%# taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<sec:authorize access="hasRole('frmwrk.user.role.administrator')">
...
</sec:authorize>

Resources