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

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:

Related

Spring mvc - form:checkboxes dont bring me items

I am using form:checkboxes and isn't working. I can check the items in the html and when i put submit, debbuging, i see the items i had checked arent in the list of my form, the list is empty
Form
public class PresupuestoForm {
private long id;
private List<ItemVenta> elementos;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public List<ItemVenta> getElementos() {
return elementos;
}
public void setElementos(List<ItemVenta> elementos) {
this.elementos = elementos;
}
Controller
#RequestMapping(value = "/manageStock", method = RequestMethod.GET)
public ModelAndView controlarStockParaPasarAVenta(#RequestParam("getItem") long id){
try{
Presupuesto p = this.presupuestoService.getPresupuesto(id);
PresupuestoForm form = new PresupuestoForm();
form.setId(p.getId());
List<ItemVenta> elementos = new ArrayList<ItemVenta>();
getItemsFromPresupuesto(p, elementos); // Method that fill the list elementos with some items
ModelAndView m = new ModelAndView("manageStock");
m.addObject("command",form);
m.addObject("elementos",elementos);
return m;
}catch(Exception e){
...
}
}
#RequestMapping(value = "/manageStockForm", method = RequestMethod.POST)
public Object manageStockPresupuestoForm(#ModelAttribute("manageStockForm") PresupuestoForm form, BindingResult result){
Presupuesto p = null;
try{
p = this.presupuestoService.getPresupuesto(form.getId());
for(ItemVenta i : form.getElementos()){
}
return "redirect:becomeVenta.html?getItem="+form.getId();
}catch(BussinesException e){
...
}catch(Exception e){
...
}
}
HTML
<table>
<tr style="display: none;">
<td><form:input path="id"/></td>
</tr>
<tr>
<td><div class="texto">
<form:checkboxes path="elementos" items="${elementos}" itemLabel="itemName" />
</div></td>
</tr>
<tr>
<td>
<input class="linkboton" type="submit" value="Submit"/>
</td>
</tr>
</table>
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" 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>SpringTiles</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
View Resolver
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context3.0.xsd">
<context:component-scan base-package="controllers" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
</beans>
ItemVenta Mapping
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD3.0//EN"
"http://hibernate.sourceforge.n/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field" default-cascade="save-update">
<class name="ventas.ItemVenta">
<id column="id" name="id">
<generator class="native" />
</id>
<property name="precio" />
<property name="nombre" />
<property name="cantidad" />
<property name="idItem" />
<property name="tipo" />
</class>
</hibernate-mapping>
My form:checkboxes had to go over a list of entitys (ItemVenta) and save in the form's list elementos the entitys i checked. i googled answers and i found that i needed to add the method equals to my entity. It didn´t work. So intead of save entitys in the form´s list, now am i saving the ids of those entitys and it worked. I still go over a list of entitys.

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>

HTTP 500 can´t create bean with primefaces

I have a list with #controller and it´s works, but when i want to use primefaces and change EmpresaController by other EmpresaBean, i have the next error and i don´t know the solution. Can help me please? Sorry my english, i´m learning it. Thx!
javax.servlet.ServletException: No se puede crear el bean administrado empresaMB. Se han encontrado los problemas siguientes:
- No existe la propiedad empresasService para el bean administrado empresaMB.
javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
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_2_5.xsd"
version="2.5">
<!-- 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>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<!-- welcome page -->
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<!-- 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>
</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">
<!-- JSF and Spring are integrated -->
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
<!-- configuration of navigation rules -->
<navigation-rule>
<from-view-id>index.xhtml</from-view-id>
</navigation-rule>
</faces-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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Enable autowire -->
<context:annotation-config />
<!-- Enable component scanning -->
<context:component-scan base-package="com.atorres.springapp" />
<!-- Data Source Declaration -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<!-- JDBC Data Source. It is assumed you have MySQL running on localhost port 3306 with
username root and blank password. Change below if it's not the case -->
<bean id="myDataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
p:validationQuery="${jdbc.validationQuery}" />
<!-- Hibernate Session Factory -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan">
<array>
<value>com.atorresbr.springapp</value>
</array>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Hibernate Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<!-- Activates annotation based transaction management -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
index.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>My Team</title>
</h:head>
<h:body>
<h2>My Team</h2>
<hr/>
<h:outputText value="Hello JSF"/>
<p:dataTable var="empresa" value="#{empresaMB.empresas}">
<p:column headerText="Clave">
<h:outputText value="#{empresa.clave}" />
</p:column>
<p:column headerText="Nombre">
<h:outputText value="#{empresa.nombre}" />
</p:column>
</p:dataTable>
</h:body>
</html>
EmpresaManagedBean.java
package com.atorres.springapp;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import org.springframework.dao.DataAccessException;
import com.atorres.springapp.Empresa;
import com.atorres.springapp.EmpresasServiceImpl;
#ManagedBean(name="empresaMB")
#ViewScoped
public class EmpresasManagedBean implements Serializable {
private static final long serialVersionUID = 1L;
//Spring User Service is injected...
#ManagedProperty(value="#{empresasService}")
EmpresasServiceImpl empresasService;
List<Empresa> empresaList;
public List<Empresa> getUserList() {
empresaList = new ArrayList<Empresa>();
empresaList.addAll(empresasService.findAll());
return empresaList;
}
}
#ManagedProperty(value="#{empresasService}") instructs the framework to look for JSF artifact named empresasService, and EmpresasService probably isn't a JSF artifact. The simplest solution would be to remove the annotation and instantiate the service class yourself.
EmpresasServiceImpl empresasService = new EmpresasServiceImpl();
Or, inject it in some other way. If you are still using Spring, you can combine it with JSF, and use #Autowired for service injection (there are articles online on how to configure Spring to work with JSF), or just annotate EmpresasServiceImpl with #ManagedBean and see if that works (I think you'll have to have a setter for empresasService field in this case).

autowiring an external bean does not work

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>

spring and rest

I created the code below but I seem to get the following error "No mapping found for HTTP request with URI [/restspring/service/employees/1] in DispatcherServlet with name 'restspring'" when I access the service using "http://localhost:8080/restspring/service/employees/1". Did I miss something?
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_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>restspring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>restspring</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>
restspring-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>spring3.rest.bean.Employees</value>
</list>
</property>
</bean>
<bean id="employees" class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg ref="jaxbMarshaller" />
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>
employees.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table border=1>
<thead><tr>
<th>FirstName</th>
<th>LastName</th>
</tr></thead>
<c:forEach var="employee" items="${employees.employees}">
<tr>
<td>${employee.firstName}</td>
<td>${employee.lastName}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
EmployeesController.java
package spring3.rest.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import spring3.rest.bean.Employees;
#Controller
#RequestMapping("/employees")
public class EmployeesController {
#RequestMapping(method=RequestMethod.GET, value="/{id}")
public ModelAndView getEmployee(#PathVariable("id") String id){
Employees e = new Employees();
e.setFirstName("Eugene");
e.setLastName("Anthony");
return new ModelAndView("employees", "employees", e);
}
}
Employees.java
package spring3.rest.bean;
public class Employees {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Your help is kindly appreciated.
Thank You.
I think you are missing some steps as given in this document.
You need to define two beans
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
and then add a component scan for the controller path
ex
<context:component-scan base-package="org.springframework.samples.petclinic.web"/>
What is the name of the resulting war file? By default, that constitutes your context root and should be added to your request before the /service/employees/1 in your request url. The <servlet-name> of your web.xml is just a logical name for mapping a request (identified by the <url-pattern>) to a specific servlet, it is not a part of the url. Try omitting it when you make a request.
I don't see a component scan to detect your controller . If you have missed it , put it in . Sample
<context:component-scan base-package="org.myProject.controller"/>
This detects the #Controller annotation and also auto enables the <context:annotation-config> to process the annotations.
Check the link at http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-controller for more info .

Resources