autowiring an external bean does not work - spring

Im tryin to map the bean called "userVO" to the security controller with annotations, it works when I map it by xml configuration, but I keep getting the following error when I use annotations
WARNING: /login.xhtml #22,37 value="#{securityController.userVO.vc_name}": Target unreacheable, 'userVO' returned null
I must say UserVO is in a different project that I also own, and its dependency is handled with maven
The following is my spring-beans config file
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<import resource="mapper-beans.xml" />
<bean name="userVO" id="userVO" class="rst.core.security.UserVO"
scope="session" />
<context:annotation-config />
<context:component-scan base-package="rst.core.security" />
<context:component-scan base-package="rst.controller" />
</beans>
The following is the controller with the mapping to userVO that its not working:
#ManagedBean
#RequestScoped
#Controller
public class SecurityController {
public static Logger log;
private WebFacade webFacade;
#Autowired
private UserVO userVO;
public SecurityController() {
log = LoggerFactory.getLogger(this.getClass());
log.debug("Creating SecurityController.");
}
public String login() {
UserVO user= webFacade.validateUser(getUserVO()); // method to search the db
if (user!= null) {
return "accessGranted";
} else {
return "accessDenied";
}
}
//getters and setters....
}
Also this is the login screen that triggers the error:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1 /DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form id="form">
<p:panel id="panel" header="New Person">
<h:panelGrid columns="3">
<h:outputLabel for="name" value="Name: *" />
<p:inputText id="name" value="#{securityController.userVO.vc_name}"
label="name" required="true"/>
<h:outputLabel for="password" value="Password: *" />
<p:inputText id="password" value="#{securityController.userVO.vc_password}"
required="true" label="Password" type="password"/>
</h:panelGrid>
<p:commandButton id="btn" value="Login" update="panel"
action="#{securityController.login}"/>
</p:panel>
</h:form>
</h:body>
</html>
And the following is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee /web-app_3_0.xsd">
<display-name>rst-web</display-name>
<!---+++++++++++++++++++++++++++++++++++++++++++++++++Spring++++++++++++++++++++++++++++++++++++++++++-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-beans.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- ++++++++++++++++++++++++ JSF +++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/login.xhtml</welcome-file>
</welcome-file-list>
</web-app>

How is securityController resolved? It is marked with #ManagedBean which makes it visible to JSF, on the other hand JSF won't recognize spring's #Autowired when instantiating SecurityManager.
If you want to stick to spring, get rid of #ManagedBean, change #RequestScoped to #Scope("request") and make sure you have set up ELResolver correctly in faces-config:
<faces-config>
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
...
</application>
</faces-config>

Related

j-spring-security The requested resource is not available

i had created login authentication using the spring security framework..
when i include the <!-- Spring Security --> below entries in the web.xml the index page is not shown, also if i don't include the tag (<!-- Spring Security -->), the index page is shown, when i enter the userid/password and click on the submit button, i get the error
error without adding <!-- spring security --> tag
----------------------------------------------------
http://sgv09946224.gbl.ad.hedani.net:8080/SpringMVCExample/j_spring_security_check
HTTP Status 404 - The requested resource is not available.
error while adding <!-- spring security --> tag
----------------------------------------------------
http://sgv09946224.gbl.ad.hedani.net:8080/SpringMVCExample/
HTTP Status 404 - /SpringMVCExample/
type Status report
message /SpringMVCExample/
description The requested resource is not available.
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>SpringMVCExample</display-name>
<!--
- Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener.
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/application-config.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
- Servlet that dispatches request to registered handlers (Controller implementations).
-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml /WEB-INF/mvc-security.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring Security -->
<!--
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
-->
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
</web-app>
mvc-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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Uncomment and your base-package here:
<context:component-scan
base-package="org.springframework.samples.web"/> -->
<mvc:resources mapping="/SpringMVCExample/resources/**" location="SpringMVCExample/resources/" />
<mvc:resources mapping="/images/**" location="/WEB-INF/images/" />
<mvc:annotation-driven />
<context:component-scan base-package="com.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
mvc-security.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:security="http://www.springframework.org/schema/security"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<security:http auto-config="true">
<security:intercept-url pattern="/index*" access="ROLE_USER" />
<security:form-login login-page="/index" default-target-url="/showMessage"
authentication-failure-url="/fail2login" />
<security:logout logout-success-url="/logout" />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="l1" password="l1" authorities="ROLE_USER" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
index.jsp
<!DOCTYPE html>
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<img src="/SpringMVCExample/images/SupportDashboard.jpg"/>
<c:if test="${not empty error}">
Your login attempt was not successful, try again
</c:if>
<form name='f' action="<c:url value='j_spring_security_check' />" method='POST'>
<table>
<tr>
<td>User:</td>
<td><input type='text' name='j_username' value=''>
</td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='j_password' />
</td>
</tr>
<tr>
<td colspan='2'><input name="submit" type="submit"
value="submit" />
</td>
</tr>
<tr>
<td colspan='2'><input name="reset" type="reset" />
</td>
</tr>
</table>
</form>
<c:url value="/showMessage.html" var="messageUrl" />
Click to enter
</body>
</html>
showmessage.jsp
<!DOCTYPE html>
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
spring/application-context.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Uncomment and add your base-package here:
<context:component-scan
base-package="org.springframework.samples.service"/> -->
<!-- hibernate configuration and mappings
<import resource="mvc-security.xml"/> -->
</beans>
maincontroller.java
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.validation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;
/*
* author: Crunchify.com
*
*/
#Controller
public class MainController {
#RequestMapping("/showMessage")
public ModelAndView helloWorld() {
System.out.println("inisde showmessage method");
String message = "<br><div style='text-align:center;'>" +
"<h3>********** Welcome to LDO Support Landing page **********<h3> </div><br><br>";
return new ModelAndView("showMessage", "message", message);
}
#RequestMapping("/index")
public ModelAndView index() {
System.out.println("inisde index method");
String message = "<br><div style='text-align:center;'>" +
"<h3>********** Welcome to LDO Support Landing page **********<h3> </div><br><br>";
return new ModelAndView("showMessage", "message", message);
}
#RequestMapping(value="/fail2login", method = RequestMethod.GET)
public String loginerror(ModelMap model) {
System.out.println("inisde fail2login method");
model.addAttribute("error", "true");
return "login";
}
#RequestMapping(value="/logout", method = RequestMethod.GET)
public String logout(ModelMap model) {
System.out.println("inisde logout method");
return "login";
}
}
modified web.xml which worked for me
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/mvc-*.xml,
</param-value>
</context-param>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

When Session time-out occurred, preRenderView of previous screen is executed

When forward method in the filter executed,
preRenderView of previous screen is executed if a screen transfer in the session time-out state.
ex)Flow of processing
Session time-out: Login -> Start(session time-out) -> Error
(Default:Login -> Start -> End)
When Start transition to Error, preRenderView of Start is executed.
Isn't preRenderView of Error called in usual?
I think the SpringBeanFacesELResolver class is the cause.
Is it right to do such behavior?
Please tell me the cause and a plan to avoid.
It's being checked in the following state (excerpt).
Spring 3.2.5-RELEASE
JDK 1.7.0_76
JSF 2.1
WebLogic 12.1.3
LoginAction.java
#org.springframework.stereotype.Component
#org.springframework.context.annotation.Scope("request")
public class LoginAction implements java.io.Serializable {
public LoginAction() {
}
public String login() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
// The STATUS attribute is set in a session.
session.setAttribute("STATUS", "LOGIN");
return "/Sample/start.xhtml";
}
}
StartAction.java
#org.springframework.stereotype.Component
#org.springframework.context.annotation.Scope("request")
public class StartAction implements java.io.Serializable {
public StartAction() {
}
public void preRenderView(javax.faces.event.ComponentSystemEvent event)
throws javax.faces.event.AbortProcessingException {
// Some process
}
public void preRenderViewOnSystemError(javax.faces.event.ComponentSystemEvent event)
throws AbortProcessingException, IOException {
// Some process
}
public String toEnd() {
return "toEnd";
}
}
TestFilter.java
public class TestFilter implements Filter {
#Override
public void init(FilterConfig config) throws ServletException {
}
#Override
public void destroy() {
}
#Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if(request instanceof HttpServletRequest) {
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpSession session = httpRequest.getSession();
// When the STATUS attribute can't get a session, a forward is executed.
String status = (String)session.getAttribute("STATUS");
if (status == null || "".equals(status)) {
RequestDispatcher rd = request.getRequestDispatcher("/faces/Sample/error.xhtml");
rd.forward(request, response);
}
}
chain.doFilter(request, response);
}
}
start.xhtml
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>Sample Start</title>
</h:head>
<h:body>
<f:event listener="#{startAction.preRenderView}" type="preRenderView"/>
<h:form action="" id="form1" method="post" enctype="multipart/form-data">
<h:commandButton action="#{startAction.toEnd}" id="toEnd" type="submit" value="End"></h:commandButton>
::
</h:form>
</h:body>
</html>
error.xhtml
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>Sample Error</title>
</h:head>
<h:body>
<f:event listener="#{startAction.preRenderViewOnSystemError}" type="preRenderView"/>
<h:form action="" id="form1" method="post">
Error
</h:form>
</h:body>
</html>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/componentScanSettings.xml
/WEB-INF/datasource-context.xml
</param-value>
</context-param>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/Sample/Sample-faces-config.xml, /WEB-INF/faces-config.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<filter>
<filter-name>multipartFilter</filter-name>
<filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>multipartFilter</filter-name>
<url-pattern>/faces/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>Test</filter-name>
<filter-class>com.xxx.filter.TestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Test</filter-name>
<url-pattern>/faces/Sample/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- The expiration date of a session is made short. -->
<session-config>
<session-timeout>1</session-timeout>
</session-config>
Sample-faces-config.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<navigation-rule>
<description>Start</description>
<display-name>Start</display-name>
<from-view-id>/Sample/start.xhtml</from-view-id>
<navigation-case>
<from-outcome>toEnd</from-outcome>
<to-view-id>/Sample/end.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
faces-config.xml
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
componentScanSettings.xml
<?xml version="1.0" encoding="UTF-8"?>
<bean
xsi:schemaLocation='http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd'
xmlns:context='http://www.springframework.org/schema/context'
xmlns='http://www.springframework.org/schema/beans'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<context:annotation-config />
<context:component-scan base-package='com.xxx' />
</bean>
datasource-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<tx:annotation-driven proxy-target-class="true" order="100" />
<bean id="targetDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<property name="targetDataSource" ref="targetDataSource" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="filterMultipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>100000</value>
</property>
<property name="maxInMemorySize">
<value>10240</value>
</property>
</bean>
</beans>

Getting correct html form action attribute with JSF + Spring 3

I am trying to mix Spring MVC with Java Server Faces. I have a Spring 3.2 #Controller class, which returns a ModelAndView that is resolved to a JSF view. That view contains a <h:form> tag. The problem that I am having is that, on the rendered HTML, the form action attribute combines the original request URL with the name to resolve the view, creating a strange meaningless URL to which the form is POSTed. What I want is just the view name, without the original request URL.
Here is my controller class (org.my.test.MainController):
#Controller
#RequestMapping("/items")
public class MainController
{
#RequestMapping(value="/{itemId}", method=RequestMethod.GET)
public ModelAndView retrieveItem( #PathVariable long itemId ) {
/* Retrieve item */
ModelAndView mav = new ModelAndView();
mav.addObject ("itemName", "A retrieved item");
mav.addObject ("itemId", itemId);
mav.setViewName ("/items");
return mav;
}
}
Here is my JSF template (/items.xhtml)
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Edit item</title>
</h:head>
<h:body>
<h:form>
<h3>Edit item</h3>
<p>
Item name: <h:inputText name="itemName" value="#{itemName}" />
</p>
<p>
Item id: <h:inputText name="itemId" value="#{itemId}" />
</p>
<p>
<h:commandButton value="Submit" />
</p>
</h:form>
</h:body>
</html>
When I request the page http://localhost:8081/items/12345, what is served is this:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Edit item</title></head><body><form id="j_id_6" name="j_id_6" method="post" action="/items/12345/items.xhtml" enctype="application/x-www-form-urlencoded">
<h3>Edit item</h3>
<p>
Item name: <input id="j_id_6:j_id_8" name="j_id_6:j_id_8" type="text" value="A retrieved item" />
</p>
<p>
Item id: <input id="j_id_6:j_id_a" name="j_id_6:j_id_a" type="text" value="12345" />
</p>
<p><input id="j_id_6:j_id_c" name="j_id_6:j_id_c" type="submit" value="Submit" />
</p><input type="hidden" name="j_id_6_SUBMIT" value="1" /><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="oyc7wGGKZrPGinPPmrv9PmTDy0GBlI3c+pjWpdK0KuY69faJ" /></form></body>
</html>
My problem is the bit that says action="/items/12345/items.xhtml" What I want is action="/items" or action="/items.xhtml"
My question has two parts: why is my setup combining the request URL with the view ID like this, and how do I make it stop?
Here is web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Minimal JSF + Spring test</display-name>
<!-- - Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener. -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/spring/application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<!-- - Servlet that dispatches request to registered handlers (Controller
implementations). -->
<servlet>
<servlet-name>ui</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/ui-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ui</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Just here so the JSF implementation can initialize, *not* used at runtime -->
<servlet>
<servlet-name>faces</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Just here so the JSF implementation can initialize -->
<servlet-mapping>
<servlet-name>faces</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>
Here is Spring config ui-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:faces="http://www.springframework.org/schema/faces"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="org.my.test" />
<mvc:annotation-driven />
<bean id="faceletsViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.faces.mvc.JsfView" />
<property name="prefix" value="" />
<property name="suffix" value=".xhtml" />
</bean>
</beans>
Here is main Spring config application-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:faces="http://www.springframework.org/schema/faces"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="org.my.test" />
</beans>
And here is faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
This is all running in embedded Jetty 9.0.4. I have tried two different JSF implementations, Apache My-Faces 2.1.11 and Mojarra 2.2.1, both with the same effect. Spring version is 3.2.3.
Changing the url-mapping in web.xml from "/" to "/*" corrected the problem.
Everything is in fact behaving according to the Servlet spec (Java Servlet Specification Version 3.0 Rev a, Section 12.2, page 116):
A string beginning with a / character and ending with a /* suffix is used for path mapping.
...
A string containing only the / character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
(Thank you to this post on Coderanch: http://www.coderanch.com/t/366340/Servlets/java/servlet-mapping-url-pattern)
(Java Servlet 3.0 spec available at http://download.oracle.com/otndocs/jcp/servlet-3.0-mrel-eval-oth-JSpec/)

Spring mvc + JSF on GAE

I have a problem with implementing spring mvc + primeFaces on GAE, i think that all works fine except when i try to modify values of my bean by sending post, values remain the same as before. Here is code:
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/main-servlet.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.enableThreading</param-name>
<param-value>false</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>
com.remote.control.web.ApiKeyInitializer
</listener-class>
</listener>
<servlet>
<servlet-name>main</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<url-pattern>/home</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
</security-constraint>
<servlet>
<servlet-name>SystemServiceServlet</servlet-name>
<servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value/>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SystemServiceServlet</servlet-name>
<url-pattern>/_ah/spi/*</url-pattern>
</servlet-mapping>
</web-app>
main-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<context:component-scan base-package="com.remote.control.controller" />
<context:component-scan base-package="com.remote.control.service" />
<mvc:annotation-driven />
<mvc:resources mapping="/res/**" location="/WEB-INF/res/" />
<bean
id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property
name="prefix"
value="/WEB-INF/jsp/" />
<property
name="suffix"
value=".xhtml" />
</bean>
</beans>
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config >
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
HomeController.java
#Controller
public class HomeController {
private Logger logger = Logger.getLogger(getClass().getName());
#Autowired
Bean bean;
#RequestMapping(value="/home", method=RequestMethod.GET)
public ModelAndView homeGet() {
ModelAndView mv=new ModelAndView("index");
bean.setParam1("111111111");
bean.setParam2("22222222222");
bean.setParam3("3333333333");
mv.addObject("task",bean);
return mv;
}
#RequestMapping(value="/home", method=RequestMethod.POST)
public ModelAndView homePost() {
ModelAndView mv=new ModelAndView("index");
logger.warning("param1"+ bean.getParam1());
logger.warning("param2" + bean.getParam2());
logger.warning("param3" + bean.getParam3());
mv.addObject("task",bean);
}
}
Bean.java
#Component
#Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class Bean implements Serializable {
private String param1;
private String param2;
private String param3;
public String getParam1() {
return param1;
}
public void setParam1(String param1) {
this.param1 = param1;
}
public String getParam2() {
return param2;
}
public void setParam2(String param2) {
this.param2 = param2;
}
public String getParam3() {
return param3;
}
public void setParam3(String param3) {
this.param3 = param3;
}
}
index.xhtml
<html>
<ui:composition template="baseLayout.xhtml">
<ui:define name="content">
<form action="home" method="post">
<h:panelGrid style="margin-top:20px;" columns="2" cellpadding="10">
<p:outputPanel autoUpdate="true">
<p:inputText value="#{task.param1}" />
<p:inputText value="#{task.param2}" />
<p:inputTextarea value="#{task.param3}" id="text" rows="10" cols="50" />
</p:outputPanel>
</h:panelGrid>
<button value="submit" />
</form>
</ui:define>
</ui:composition>
</html>
Just turn
<form action="home" method="post">
<h:panelGrid style="margin-top:20px;" columns="2" cellpadding="10">
<p:outputPanel autoUpdate="true">
<p:inputText value="#{task.param1}" />
<p:inputText value="#{task.param2}" />
<p:inputTextarea value="#{task.param3}" id="text" rows="10" cols="50" />
</p:outputPanel>
</h:panelGrid>
<button value="submit" />
</form>
Into
<h:form>
<h:panelGrid style="margin-top:20px;" columns="2" cellpadding="10">
<p:outputPanel autoUpdate="true">
<p:inputText value="#{task.param1}" />
<p:inputText value="#{task.param2}" />
<p:inputTextarea value="#{task.param3}" id="text" rows="10" cols="50" />
</p:outputPanel>
</h:panelGrid>
<p:commandButton value="submit" action="#{task.myMethod}"/>
</h:form>
with the following method
public void myMethod(){
System.out.println(param1);
System.out.println(param2);
System.out.println(param3);
}

Integration of JSF 2, Spring 3.1 and Hibernate 4.1 in Eclipse

According to my previous question integration of jsf2.0 and spring 3.1 and hibernate 4.1
I think my code has problem. I am really confused. I did as other advised but still getting error 404:Description The requested resource (/jsfspringhiber/default.xhtml) is not available. I don't use maven.
Customer.java
package main;
public class Customer{
public long customerId;
public String name;
public String address;
public String createdDate;
public long getCustomerId() {
return customerId;
}
public void setCustomerId(long customerId) {
this.customerId = customerId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCreatedDate() {
return createdDate;
}
public void setCreatedDate(String createdDate) {
this.createdDate = createdDate;
}
}
CustomerBean.java
package main;
import java.io.Serializable;
import java.util.List;
import main.CustomerBo;
import main.Customer;
public class CustomerBean implements Serializable{
CustomerBo customerBo;
public String name;
public String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void setCustomerBo(CustomerBo customerBo) {
this.customerBo = customerBo;
}
public CustomerBo getCustomerBo() {
return customerBo;
}
//get all customer data from database
public List<Customer> getCustomerList(){
return customerBo.findAllCustomer();
}
//add a new customer data into database
public String addCustomer(){
Customer cust = new Customer();
cust.setName(getName());
cust.setAddress(getAddress());
customerBo.addCustomer(cust);
clearForm();
return "";
}
//clear form values
private void clearForm(){
setName("");
setAddress("");
}
}
CustomerBo.java
import java.util.List;
import main.Customer;
public interface CustomerBo{
void addCustomer(Customer customer);
List<Customer> findAllCustomer();
}
CustomerBoImpl.java
public class CustomerBoImpl implements CustomerBo{
CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
public void addCustomer(Customer customer){
customerDao.addCustomer(customer);
}
public List<Customer> findAllCustomer(){
return customerDao.findAllCustomer();
}
}
CustomerDao.java
public interface CustomerDao{
void addCustomer(Customer customer);
List<Customer> findAllCustomer();
}
CustomerDaoImpl.java
public class CustomerDaoImpl implements CustomerDao{
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void addCustomer(Customer customer){
getSessionFactory().getCurrentSession().save
(customer);
}
public List<Customer> findAllCustomer(){
List list = getSessionFactory().getCurrentSession
().createQuery("from Customer").list();
return list;
}
}
Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 27, 2012 1:01:10 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="main.Customer" table="CUSTOMER">
<id name="customerId" type="long">
<column name="CUSTOMER_ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
<property name="createdDate" type="java.lang.String">
<column name="CREATED_DATE" />
</property>
</class>
</hibernate-mapping>
CustomerBean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1xsd">
<bean id="customerBo"
class="main.CustomerBoImpl" >
<property name="customerDao" ref="customerDao" />
</bean>
<bean id="customerDao"
class="main.CustomerDaoImpl" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
DataSource.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#Mohsen-PC:1521:mydb" />
<property name="username" value="system" />
<property name="password" value="123" />
</bean>
</beans>
HibernateSessionFactory.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>main/Customer.hbm.xml</value>
</list>
</property>
</bean>
</beans>
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- Database Configuration -->
<import resource="main/DataSource.xml"/>
<import resource="main/HibernateSessionFactory.xml"/>
<!-- Beans Declaration -->
<import resource="main/CustomerBean.xml"/>
</beans>
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
<managed-bean>
<managed-bean-name>customer</managed-bean-name>
<managed-bean-class>main.CustomerBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>customerBo</property-name>
<value>#{customerBo}</value>
</managed-property>
</managed-bean>
</faces-config>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>jsfspringhiber</display-name>
<!-- Add Support for Spring -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>default.xhtml</welcome-file>
</welcome-file-list>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
default.xhtml
<<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
<h:head>
<h:outputStylesheet library="css" name="table-style.css" />
</h:head>
<h:body>
<h1>JSF 2.0 + Spring + Hibernate Example</h1>
<h:dataTable value="#{customer.getCustomerList()}" var="c"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>
<h:column>
<f:facet name="header">
Customer ID
</f:facet>
#{c.customerId}
</h:column>
<h:column>
<f:facet name="header">
Name
</f:facet>
#{c.name}
</h:column>
<h:column>
<f:facet name="header">
Address
</f:facet>
#{c.address}
</h:column>
<h:column>
<f:facet name="header">
Created Date
</f:facet>
#{c.createdDate}
</h:column>
</h:dataTable>
<h2>Add New Customer</h2>
<h:form>
<h:panelGrid columns="3">
Name :
<h:inputText id="name" value="#{customer.name}"
size="20" required="true"
label="Name" >
</h:inputText>
<h:message for="name" style="color:red" />
Address :
<h:inputTextarea id="address" value="#{customer.address}"
cols="30" rows="10" required="true"
label="Address" >
</h:inputTextarea>
<h:message for="address" style="color:red" />
</h:panelGrid>
<h:commandButton value="Submit" action="#{customer.addCustomer()}" />
</h:form>
</h:body>
</html>
the structure of my project:
Lets do one thing, Here is the code for a basic skeleton for integrating Spring and JSF. If you are able to see the login page with this code then we will build the solution out of this.
Create a new Dynamic Web Project with the following files:
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"
version="3.0">
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
</web-app>
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
applicationContext.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.examples" />
</beans>
Test pages: index.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Welcome</title>
</h:head>
<h:body>
<h:form>
<h3>Please enter your name and password.</h3>
<table>
<tr>
<td>Name:</td>
<td><h:inputText value="#{userBean.name}"/></td>
</tr>
<tr>
<td>Password:</td>
<td><h:inputSecret value="#{userBean.password}"/></td>
</tr>
</table>
<p><h:commandButton value="Login" action="welcome"/></p>
</h:form>
</h:body>
</html>
welcome.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Welcome</title>
</h:head>
<h:body>
<h3>Spring integration with JavaServer Faces Successful, #{userBean.name}!</h3>
</h:body>
</html>
And finally the backing bean: UserBean.java
package com.examples;
import java.io.Serializable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
#Component
#Scope("request")
public class UserBean implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String password;
public String getName() { return name; }
public void setName(String newValue) { name = newValue; }
public String getPassword() { return password; }
public void setPassword(String newValue) { password = newValue; }
}
That's it, nothing more. Just copy and paste this code into a sample project and lets see what we can do later. One more thing I noticed is that your lib folder under WEB-INF seems empty. You will need the following jars to run this project:
Download Latest GA release of Spring Framework from here and include the jars in the dist folder of the zip file.
Download JSF jars from here, you can grab the latest 2.1.10.
Include commons-logging jar from here
i use maven. the correct answer is here.
the structure is:

Resources