获取404错误,请求的资源不可用

时间:2014-03-20 13:50:52

标签: html hibernate spring-mvc jsp tomcat7

我在tomcat,HTTP状态404 /sdnext/save.html

中收到错误

请求的资源不可用。我正在尝试使用hibernate执行示例应用程序来学习spring mvc itegration。这是我为样本

尝试的链接

http://www.dineshonjava.com/2012/12/spring-mvc-with-hibernate-crud-example.html#.UyihQ_ldWuI

在此,我们没有save.html或add.html或delete.html,但控制器类中提到了这些内容。在示例链接中,这是我使用过的控制器类

package com.spring.hibernate.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;

import com.spring.hibernate.bean.EmployeeBean;
import com.spring.hibernate.model.Employee;
import com.spring.hibernate.service.EmployeeService;

@Controller
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value="/save",method=RequestMethod.POST)
    public ModelAndView saveEmployee(@ModelAttribute("command")EmployeeBean employeeBean, BindingResult result)
    {
        Employee employee=prepareModel(employeeBean);
        employeeService.addEmployee(employee);
        return new  ModelAndView("redirect:/add.html");
    }

    @RequestMapping(value="/employees",method=RequestMethod.GET)
    public ModelAndView listEmployees()
    {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("employees", prepareListofBean(employeeService.listEmployeess()));
        return new ModelAndView("employeeList",model);
    }

    @RequestMapping(value="/add",method=RequestMethod.GET)
    public ModelAndView addEmployee(@ModelAttribute("command")EmployeeBean employeeBean, BindingResult result)
    {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("employees", prepareListofBean(employeeService.listEmployeess()));
        return new ModelAndView("addEmployee",model);
    }

    @RequestMapping(value="/index",method=RequestMethod.GET)
    public ModelAndView welcome()
    {
        return new ModelAndView("index");
    }

    @RequestMapping(value="/delete",method=RequestMethod.GET)
    public ModelAndView editEmployee(@ModelAttribute("command")EmployeeBean employeeBean,BindingResult result)
    {
        employeeService.deleteEmployee(prepareModel(employeeBean));
        Map<String, Object> model = new HashMap<String,Object>();
        model.put("employee",null);
        model.put("employees",prepareListofBean(employeeService.listEmployeess()));
        return new ModelAndView("addEmployee",model);
    }

    @RequestMapping(value="/edit",method=RequestMethod.GET)
    public ModelAndView deleteEmployee(@ModelAttribute("command")EmployeeBean employeeBean, BindingResult result)
    {
        Map<String, Object> model=new HashMap<String,Object>();
        model.put("employee",prepareEmployeeBean(employeeService.getEmployee(employeeBean.getId())));
        model.put("employees",prepareListofBean(employeeService.listEmployeess()));
        return new ModelAndView("addEmployee",model);
    }

    private Employee prepareModel(EmployeeBean employeeBean)
    {
        Employee employee=new Employee();
        employee.setEmpId(employeeBean.getId());
        employee.setEmpName(employeeBean.getName());
        employee.setEmpAge(employeeBean.getAge());
        employee.setSalary(employeeBean.getSalary());
        employee.setEmpAddress(employeeBean.getAddress());
        employeeBean.setId(null);

        return employee;


    }

    private List<EmployeeBean> prepareListofBean(List<Employee> employees){  
          List<EmployeeBean> beans = null;  
          if(employees != null && !employees.isEmpty()){  
           beans = new ArrayList<EmployeeBean>();  
           EmployeeBean bean = null;  
           for(Employee employee : employees){  
            bean = new EmployeeBean();  
            bean.setName(employee.getEmpName());  
            bean.setId(employee.getEmpId());  
            bean.setAddress(employee.getEmpAddress());  
            bean.setSalary(employee.getSalary());  
            bean.setAge(employee.getEmpAge());  
            beans.add(bean);  
           }  
          }  
          return beans;  
         }  

         private EmployeeBean prepareEmployeeBean(Employee employee){  
          EmployeeBean bean = new EmployeeBean();  
          bean.setAddress(employee.getEmpAddress());  
          bean.setAge(employee.getEmpAge());  
          bean.setName(employee.getEmpName());  
          bean.setSalary(employee.getSalary());  
          bean.setId(employee.getEmpId());  
          return bean;  
}
}

我需要知道.html文件,它是如何工作的?在web.xml文件中,提到的URL模式是* .html,我的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_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
  <servlet-name>sdnext</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/config/sdnext-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>sdnext</servlet-name>
  <url-pattern>*.html</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

请给我一些解决此错误的建议。

我的控制台:

Mar 20, 2014 9:29:35 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Riya/software_downloads/eclipse/jre/bin/server;C:/Riya/software_downloads/eclipse/jre/bin;C:/Riya/software_downloads/eclipse/jre/lib/amd64;C:\Program Files\Java\jdk1.7.0_09\bin;C:Program Files\apache-maven-3.2.1\bin;;C:\Riya\software_downloads\eclipse;;.
Mar 20, 2014 9:29:35 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SpringHibernateApp' did not find a matching property.
Mar 20, 2014 9:29:36 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8086"]
Mar 20, 2014 9:29:36 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 20, 2014 9:29:36 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 508 ms
Mar 20, 2014 9:29:36 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 20, 2014 9:29:36 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.34
log4j:WARN No appenders could be found for logger (org.springframework.web.servlet.DispatcherServlet).
log4j:WARN Please initialize the log4j system properly.
Mar 20, 2014 9:29:38 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'sdnext'
Mar 20, 2014 9:29:39 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8086"]
Mar 20, 2014 9:29:39 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 20, 2014 9:29:39 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3660 ms
Hibernate: select this_.EMPID as EMPID0_0_, this_.ADDRESS as ADDRESS0_0_, this_.EMPAGE as EMPAGE0_0_, this_.EMPNAME as EMPNAME0_0_, this_.SALARY as SALARY0_0_ from EMPLOYEE this_
Hibernate: select this_.EMPID as EMPID0_0_, this_.ADDRESS as ADDRESS0_0_, this_.EMPAGE as EMPAGE0_0_, this_.EMPNAME as EMPNAME0_0_, this_.SALARY as SALARY0_0_ from EMPLOYEE this_

我的JSP文件:

addEmployee.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
    pageEncoding="ISO-8859-1"%>  
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
<%@ 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=ISO-8859-1">  
  <title>Spring MVC Form Handling</title>  
 </head>  
 <body>  
  <h2>Add Employee Data</h2>  
  <form:form method="POST" action="/sdnext/save.html">  
      <table>  
       <tr>  
           <td><form:label path="id">Employee ID:</form:label></td>  
           <td><form:input path="id" value="${employee.id}" readonly="true"/></td>  
       </tr>  
       <tr>  
           <td><form:label path="name">Employee Name:</form:label></td>  
           <td><form:input path="name" value="${employee.name}"/></td>  
       </tr>  
       <tr>  
           <td><form:label path="age">Employee Age:</form:label></td>  
           <td><form:input path="age" value="${employee.age}"/></td>  
       </tr>  
       <tr>  
           <td><form:label path="salary">Employee Salary:</form:label></td>  
           <td><form:input path="salary" value="${employee.salary}"/></td>  
       </tr>  

       <tr>  
           <td><form:label path="address">Employee Address:</form:label></td>  
                    <td><form:input path="address" value="${employee.address}"/></td>  
       </tr>  
          <tr>  
         <td colspan="2"><input type="submit" value="Submit"/></td>  
        </tr>  
   </table>   
  </form:form>  

  <c:if test="${!empty employees}">  
  <h2>List Employees</h2>  
 <table align="left" border="1">  
  <tr>  
   <th>Employee ID</th>  
   <th>Employee Name</th>  
   <th>Employee Age</th>  
   <th>Employee Salary</th>  
   <th>Employee Address</th>  
           <th>Actions on Row</th>  
  </tr>  

  <c:forEach items="${employees}" var="employee">  
   <tr>  
    <td><c:out value="${employee.id}"/></td>  
    <td><c:out value="${employee.name}"/></td>  
    <td><c:out value="${employee.age}"/></td>  
    <td><c:out value="${employee.salary}"/></td>  
    <td><c:out value="${employee.address}"/></td>  
    <td align="center"><a href="edit.html?id=${employee.id}">Edit</a> | <a href="delete.html?id=${employee.id}">Delete</a></td>  
   </tr>  
  </c:forEach>  
 </table>  
</c:if>  
 </body>  
</html>  

employeeList.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
    pageEncoding="ISO-8859-1"%>  
<%@ 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>  
<title>All Employees</title>  
</head>  
<body>  
<h1>List Employees</h1>  
<h3><a href="add.html">Add More Employee</a></h3>  

<c:if test="${!empty employees}">  
 <table align="left" border="1">  
  <tr>  
   <th>Employee ID</th>  
   <th>Employee Name</th>  
   <th>Employee Age</th>  
   <th>Employee Salary</th>  
   <th>Employee Address</th>  
  </tr>  

  <c:forEach items="${employees}" var="employee">  
   <tr>  
    <td><c:out value="${employee.id}"/></td>  
    <td><c:out value="${employee.name}"/></td>  
    <td><c:out value="${employee.age}"/></td>  
    <td><c:out value="${employee.salary}"/></td>  
    <td><c:out value="${employee.address}"/></td>  
   </tr>  
  </c:forEach>  
 </table>  
</c:if>  
</body>  
</html>  

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
    pageEncoding="ISO-8859-1"%>  
<!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>Spring3MVC with Hibernate3 CRUD Example using Annotations</title>  
  </head>  
  <body>  
    <h2>Spring3MVC with Hibernate3 CRUD Example using Annotations</h2>  
    <h2>1. <a href="employees.html">List of Employees</a></h2>  
    <h2>2. <a href="add.html">Add Employee</a></h2>  
  </body>  
</html>  

Spring配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:tx="http://www.springframework.org/schema/tx" 
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.0.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.0.xsd  
http://www.springframework.org/schema/tx  
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  

<context:property-placeholder location="classpath:resources/database.properties"/>  

<context:component-scan base-package="com.spring.hibernate">  
</context:component-scan>  

<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>  


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="jspViewResolver">  
 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>  
 <property name="prefix" value="/WEB-INF/views/"></property>  
 <property name="suffix" value=".jsp"></property>  
</bean>  

<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">  
 <property name="driverClassName" value="${app.jdbc.driverClassName}"></property>  
 <property name="url" value="${app.jdbc.url}"></property>  
 <property name="username" value="${app.jdbc.username}"></property>  
 <property name="password" value="${app.jdbc.password}"></property>  
</bean>  

<bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" id="sessionFactory">  
 <property name="dataSource" ref="dataSource"></property>  
 <property name="annotatedClasses">  
  <list>  
   <value>com.spring.hibernate.model.Employee</value>  
  </list>  
 </property>  
 <property name="hibernateProperties">  
 <props>  
  <prop key="hibernate.dialect">${hibernate.dialect}</prop>  
  <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>  
  <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}  </prop>      
        </props>  
      </property>  
</bean>  

  <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="hibernateTransactionManager">  
 <property name="sessionFactory" ref="sessionFactory"></property>  
  </bean>  
</beans> 

1 个答案:

答案 0 :(得分:0)

您的问题不是很明确,但如果我理解正确,您希望识别所有网址模式,而不仅仅是html版本。

要做到这一点,请像这样替换web.xml中的url模式。

<url-pattern>/*</url-pattern>
如果url与注释中提到的模式匹配,则@RequestMapping将仅将请求映射到该特定方法。在您的代码中,因为您有

之类的语句
return new  ModelAndView("redirect:/add.html");

它表示将请求重定向到特定视图add.html,因此您必须在web-inf文件夹中包含该html文件。

spring处理视图分辨率的方式是在views文件夹中查找匹配的返回视图名称,并选择要渲染的相应视图。

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

这是在弹簧配置的“View Resolver bean”中配置的。我建议你从Spring in action-3rd edition , manning和MVC模型中学习一下spring,了解更多视图分辨率的工作原理。