Domanda

After trying to understand what am I doing wrong for almost the whole day I am giving up. Seems like everything I found on stackoverflow I tried and yet I can't get it all working. I have a simple SpringMVC+Maven+Hibernate tutorial set up and ready in eclipse, the only thing that is left is actually running it and seeing the result :( I keep getting HTTP 404 error whatever links I try.

screen1

screen2

screen3

Here is my setup:

web.xml

<?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>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

servlet-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:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 

    <!-- Enable @Controller annotation support -->
    <mvc:annotation-driven />

    <!-- Map simple view name such as "test" into /WEB-INF/test.jsp -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- Scan classpath for annotations (eg: @Service, @Repository etc) -->
    <context:component-scan base-package="com.tutorial.pizzashop"/>

    <!-- 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">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/pizzashop"/> 
        <property name="username" value="root"/>
        <property name="password" value="discover"/>
        <property name="validationQuery" value="SELECT 1"/>
    </bean>

    <!-- Hibernate Session Factory -->
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="packagesToScan">
        <array>
            <value>com.tutorial.pizzashop</value>
        </array>
        </property>
        <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.MySQLDialect
        </value>
        </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.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    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>Pizzashop</title>
</head>
<body>
    <h1>List of all Pizzas</h1>
    <ul>
        <c:forEach var="p" items="${pizzas}">
            <li>${p.id} - ${p.name} : ${p.price}</li>
        </c:forEach>
    </ul>
</body>
</html>

PizzaController.java

package com.tutorial.pizzashop;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class PizzaController {
    @Autowired
    private PizzaDAO pizzaDAO;

    /**
     * This handler method is invoked when http://localhost:8080/pizzashop is
     * requested. The method returns view name "index" which will be resolved
     * into /WEB-INF/index.jsp. See src/main/webapp/WEB-INF/servlet-context.xml
     */
    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
        List<Pizza> pizzas = pizzaDAO.findAll();
        model.addAttribute("pizzas", pizzas);
        return "index";
    }
}

PizzaDAO.java

package com.tutorial.pizzashop;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@SuppressWarnings({ "unchecked", "rawtypes" })
public class PizzaDAO {
    @Autowired
    private SessionFactory sessionFactory;

    /**
     * @Transactional annotation below will trigger Spring Hibernate transaction
     *                manager to automatically create a hibernate session. See
     *                src/main/webapp/WEB-INF/servlet-context.xml
     */
    @Transactional
    public List<Pizza> findAll() {
        Session session = sessionFactory.getCurrentSession();
        List pizzas = session.createQuery("from Pizza").list();
        return pizzas;
    }
}

Pizza.java

package com.tutorial.pizzashop;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "PIZZAS")
public class Pizza {
    @Id
    @GeneratedValue
    private long id;
    private String name;
    private double price;

//getters and setters...
}

Tutorial I followed: http://gerrydevstory.com/2013/06/29/spring-mvc-hibernate-mysql-quick-start-from-scratch/

È stato utile?

Soluzione 3

I found the reason, as it appears I didn't have any .java files in scr/main/java directory. Instead I had my .javas in src/test/java for some reason. What a stupid mistake. After I moved my .java files to src/main/java, compiled and packaged the .war, I moved my .war to Tomcat webapps directory, restarted the server and was able to run it:)

enter image description here

Altri suggerimenti

Your app doesn't appear to be deployed on the root context (/).

Try the URL:

http://localhost:8080/Pizzashop/

The / that you refer to in your web.xml is the root of the context to which your app is deployed.

I have created spring mvc project with tomcat plugin in github, try this.

https://github.com/mohansaravanan/spring

https://github.com/mohansaravanan/spring/tree/master/springmvc-3.2.2

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top