konto usunięte
Temat: JSF Spring integracja
Witajcie, mam problem z moją aplikacją. Zintegrowałem JSF i Springa, Spring służy do wstrzykiwania beanów, JSF zarządza widokiem i nawigacją strony. Niestety, operacja zapisywania do bazy danych nie działa. Tworzy się jedynie baza danych i dwie zadane tabele, ale za każdym razem jak dodaję nowych userów, w w mojej bazie danych pusto.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" id="WebApp_ID" version="3.0">
<display-name>Proba gen</display-name>
<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>
<welcome-file-list>
<welcome-file>index.jsp</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>*.jsf</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>
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" default-autowire="byName"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/registertutorial" />
<property name="username" value="root" />
<property name="password" value="kwiatek" />
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>domain.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean name="UserDAO" class="dao.UserDAO">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
</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">
<managed-bean>
<managed-bean-name>UserBean</managed-bean-name>
<managed-bean-class>view.UserBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>save</from-outcome>
<to-view-id>/wyswietl.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
User.java:
package domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name="Users")
@SecondaryTable(name="Authorities")
public class User {
private String login;
private String password;
private String cfm_password;
private String imienazwisko;
private double ubezpieczenie;
private String authority="ROLE_USER";
@Id
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
@Column(nullable=false)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Transient
public String getCfm_password() {
return cfm_password;
}
public void setCfm_password(String cfm_password) {
this.cfm_password = cfm_password;
}
public String getImienazwisko() {
return imienazwisko;
}
public void setImienazwisko(String imienazwisko) {
this.imienazwisko = imienazwisko;
}
public double getUbezpieczenie() {
return ubezpieczenie;
}
public void setUbezpieczenie(double ubezpieczenie) {
this.ubezpieczenie = ubezpieczenie;
}
@Column(table="Authorities")
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
}
UserDAO:
package dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import domain.User;
public class UserDAO {
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory){
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public void save(User user){
hibernateTemplate.save(user);
}
@SuppressWarnings("unchecked")
public List<User> list(){
return hibernateTemplate.find("from Users");
}
public void update(User user){
hibernateTemplate.update(user);
}
public void delete(User user){
hibernateTemplate.delete(user);
}
}
UserBean (mój backing bean) :
package view;
import dao.UserDAO;
import domain.User;
public class UserBean {
private String login;
private String password;
private String password2;
private String imienazwisko;
private double ubezpieczenie;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
public String getImienazwisko() {
return imienazwisko;
}
public void setImienazwisko(String imienazwisko) {
this.imienazwisko = imienazwisko;
}
public double getUbezpieczenie() {
return ubezpieczenie;
}
public void setUbezpieczenie(double ubezpieczenie) {
this.ubezpieczenie = ubezpieczenie;
}
public String save(){
UserDAO userDAO = new UserDAO();
User user= new User();
user.setLogin(getLogin());
user.setPassword(password);
user.setImienazwisko(getImienazwisko());
user.setUbezpieczenie(getUbezpieczenie());
user.setAuthority("ROLE_USER");
userDAO.save(user);
return "success";
}
}
index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!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>
<f:view>
<h:form>
Login: <h:inputText value="#{UserBean.login}"/>
Haslo: <h:inputText value="#{UserBean.password}"/>
2Haslo: <h:inputText value="#{UserBean.password2}"/>
Imie i nazwisko: <h:inputText value="#{UserBean.imienazwisko}"/>
Ubezpieczenie: <h:inputText value="#{UserBean.ubezpieczenie}"/>
<h:commandButton value="Zapisz" action="save"/>
</h:form>
</f:view>
</body>
</html>
wyswietl.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!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>
<f:view>
Saved as: <h:outputText value="#{UserBean.login}"/>
</f:view>
</body>
</html>