Tutorial Spring 3.1 z hibernate w Netbeans.

0

Witam,

Czy zna ktoś trywialny tutorial (konfiguracja, jedna tabela, wyświetlenie danych na stronie), który łączy Spring 3.1 z Hibernate najlepiej w Netbeans?

Pozdrawiam

0

Prosze: http://netbeans.org/kb/docs/web/quickstart-webapps-spring.html
Tu z hibernate i po polsku //jaceklaskowski.pl/wiki/Tworzenie_samodzielnej_aplikacji_ze_Spring_Framework_i_Hibernate_w_NetBeans_IDE_6.9

0

Witam i dziękuję za linki.

Gdyby, ktoś spotkał się z tutorialem Spring 3.1 MVC z Hibernate to poproszę o link.

Zależy mi szczególnie na konfiguracji samego Spring'a, połączenie z hibernate (konfiguracja), kontrolera, oraz samego wyświetlenia informacji na stronie.


Może, ktoś ma pomysł jak połączyć poniższą aplikację z hibernate?

3 strony jsp:

  1. index.jsp
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>TEST</title>
    </head>
    <body>
        <!--<jspforward page="contacts"></jspforward>-->
        <p><a href="contacts.htm">contacts</a></p>
            <tt>web.xml</tt>.</p>
    </body>
</html>
  1. contacts.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Contacts</title>
    </head>
    <body>
        <h1>Contacts</h1>
        <p></p>
        <p>Poniżej Forma</p>
        <form:form action="output.htm" commandName="contact">
            <form:input path="firstname" />
            <input type="submit" value="Submit" />
        </form:form>
    </body>
</html>

3.output.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Output</h1>
        <table aligh="center">
            <tr>
                <core:out value="${contact.firstname}" /> <!-- Działa -->
            </tr>
            <!--<tr>
                <coreout value="{contactList.contacts}" /></tr> <!--Nic nie wyświetla-->
        </table>
        
    <li><a href="contacts.htm">Powrót</a></li>
    </body>
</html>

Następnie pliki konfiguracyjne:

dispatcher-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:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       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-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd" xmlns:mvc="http://www.springframework.org/schema/mvc">

    <context:annotation-config />
    <context:component-scan base-package="com.controllers" />
    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>
  1. 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:p="http://www.springframework.org/schema/p"
       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-2.5.xsd"
>

       <!--http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"-->
       
       
    <!--bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.url}"
    p:username="${jdbc.username}"
    p:password="${jdbc.password}" /-->

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->

</beans>
  1. 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">
    <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>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>
  1. Kontroler ContactController.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controllers;

import com.form.Contact;
import com.form.ContactList;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author User
 */
@Controller
//@SessionAttributes
public class ContactController {
    
    private static List<Contact> contacts = new ArrayList<Contact>();
    
    @RequestMapping(value="/contacts.htm", method = RequestMethod.GET)
    public void addContact(/*Contact contact*/Model model){
    
        model.addAttribute(new Contact());
        
        //return "redirect:contacts";
    }
    
    @RequestMapping(value="/output.htm", method=RequestMethod.POST)
    public void simple(@ModelAttribute Contact contact, Model model){
    
        model.addAttribute("contact", contact);
    }
}
  1. Klasa Contact.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.form;

/**
 *
 * @author User
 */
public class Contact {
    
    private String firstname;

    public Contact() {
    }

    public Contact(String firstname) {
        this.firstname = firstname;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    
    
}

Dziękuję za wszelką pomoc.

Pozdrawiam

0

Witam,

Dotychczas udało mi się tyle, że projekt się kompiluje (bez błędów), ale nie wyświetla tabel.

Myślę, że jest to problem z konfiguracją Spring z Hibernate.

Od początku,

konfigurację hibernate zrobiłem tak jak w JSF i próbowałem połączyć ze Spring przez plik konfiguracyjny:

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:p="http://www.springframework.org/schema/p"
       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-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <!--bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.url}"
    p:username="${jdbc.username}"
    p:password="${jdbc.password}" /-->

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
    
    <!--TEST-->
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/postgres"/>
        <property name="username" value="postgres"/>
        <property name="password" value="a"/>
    </bean>
    
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="mappingResources">
            <list>
                <value>Test.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.HSQLDialect
            </value>
        </property>
    </bean>
    <!--TEST KONIEC-->

</beans>

Niestety, z powodu braku

class="org.apache.commons.dbcp.BasicDataSource"

projekt nie chciał się uruchomić, dodałem więc plik commons-dbcp-all-1.3.jar do biblioteki i poszło.

Pozostałe pliki konfiguracyjne (dispatcher-servlet.xml i web.xml) w niezmienionej postaci.

Plik TestController.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.TestDAO;
import java.util.Map;
import model.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author User
 */

@Controller
public class TestController {
    
    @Autowired
    private TestDAO testDAO;

    @RequestMapping("/index")
    public String listTest(Map<String, Object> map){
    
        map.put("test", new Test());
        map.put("testList", testDAO.listTest());
        
        return "test";
    }
    
}

Plik index.jsp (nie wyświetla danych)

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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=UTF-8">
        <title>Welcome to Spring Web MVC project</title>
    </head>

    <body>
        <p>Spring 3.1 + Hibernate</p>
        <h3>Test</h3>
        <!--<cif test="{!empty testList}"> : $-->
            <table class="data">
            <c:forEach items="${testList}" var="test">
            <tr>
                <td>${test.id}</td>
            </tr>
            </c:forEach>
            </table>
        <!--</cif> : -->
    </body>
</html>

Mam jeszcze dodatkowe pytanie.

  1. Czy hibernate integrujemy ze Springiem w pliku ApplicationContext.xml, czy w pliku dispatcher-servlet.xml? Wydaj mi się, że w ApplicationContext.xml.

  2. Czy konieczne jest pisanie kodu w ten sposób jak w

http://viralpatel.net/blogs/spring3-mvc-hibernate-maven-tutorial-eclipse-example/

: Klasa DAO i DAOImpl następnie klasy Service i ServiceImpl?

Pozdrawiam

ps. struktura projektu w załączniku.

1 użytkowników online, w tym zalogowanych: 0, gości: 1