【问题标题】:Request resource is not available HTTP Status 404 in spring mvcspring mvc中的请求资源不可用HTTP状态404
【发布时间】:2016-12-04 10:47:17
【问题描述】:

我正在尝试使用 Hibernate 开发 Crud Spring mvc webapp,我阅读了许多解决此问题的解决方案,但我没有得到解决的想法。请协助解决这个问题。

项目结构。

异常页面

web.xml 文件

<web-app 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"
     version="2.5">

<display-name>Sample Spring Maven Project</display-name>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

spring-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:mvc="http://www.springframework.org/schema/mvc"
       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/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

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

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://localhost:5433/databaseDb"/>
        <property name="username" value="postgres"/>
        <property name="password" value="postgres"/>
    </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="annotatedClasses">
            <list>
                <value>com.model.Employee</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
            </props>
        </property>
    </bean>

    <bean id="txManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="persistenceExceptionTranslationPostProcessor"
          class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

    <bean id="viewResolver"

          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"

                  value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean id="dataDaoImpl" class="com.daoImpl.DataDaoImpl"/>
    <bean id="dataServiceImpl" class="com.serviceImpl.DataServiceImpl"/>

</beans>

index.jspfile

    <%response.sendRedirect("form");%>
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

form.jsp 文件

    <%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Being Java Guys | Registration Form</title>
</head>
<body>
 <center>

  <div style="color: teal; font-size: 30px">Being Java Guys |
   Registration Form</div>



  <c:url var="userRegistration" value="saveUser.html" />
  <form:form id="registerForm" modelAttribute="employee" method="post"
   action="register">
   <table width="400px" height="150px">
    <tr>
     <td><form:label path="firstName">First Name</form:label>
     </td>
     <td><form:input path="firstName" />
     </td>
    </tr>
    <tr>
     <td><form:label path="lastName">Last Name</form:label>
     </td>
     <td><form:input path="lastName" />
     </td>
    </tr>
    <tr>
     <td><form:label path="email">Email</form:label>
     </td>
     <td><form:input path="email" />
     </td>
    </tr>
    <tr>
     <td><form:label path="phone">Phone</form:label>
     </td>
     <td><form:input path="phone" />
     </td>
    </tr>
    <tr>
     <td></td>
     <td><input type="submit" value="Register" /></td>
    </tr>
   </table>
  </form:form>


  <a href="list">Click Here to see User List</a>
 </center>
</body>
</html>

list.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>Being Java Guys | User Details</title>
</head>
<body>
 <center>

  <div style="color: teal; font-size: 30px">Being Java Guys | User
   Details</div>

  <c:if test="${!empty employeeList}">
   <table border="1" bgcolor="black" width="600px">
    <tr
     style="background-color: teal; color: white; text-align: center;"
     height="40px">

     <td>First Name</td>
     <td>Last Name</td>
     <td>Email</td>
     <td>Phone</td>
     <td>Edit</td>
     <td>Delete</td>
    </tr>
    <c:forEach items="${employeeList}" var="user">
     <tr
      style="background-color: white; color: black; text-align: center;"
      height="30px">

      <td><c:out value="${user.firstName}" />
      </td>
      <td><c:out value="${user.lastName}" />
      </td>
      <td><c:out value="${user.email}" />
      </td>
      <td><c:out value="${user.phone}" />
      </td>
      <td><a href="edit?id=${user.id}">Edit</a></td>
      <td><a href="delete?id=${user.id}">Delete</a></td>
     </tr>
    </c:forEach>
   </table>
  </c:if>


  <a href="form">Click Here to add new User</a>
 </center>
</body>
</html>

edit.jsp 文件

    <%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Being Java Guys | Edit User Details</title>
</head>
<body>
 <center>

  <div style="color: teal; font-size: 30px">Being Java Guys |
   Edit Details</div>



  <c:url var="userRegistration" value="saveUser.html" />
  <form:form id="registerForm" modelAttribute="employee" method="post"
   action="update">
   <table width="400px" height="150px">
    <tr>
     <td><form:label path="firstName">First Name</form:label></td>
     <td><form:input path="firstName" value="${employeeObject.firstName}" /></td>
    </tr>
    <tr>
     <td><form:label path="lastName">Last Name</form:label></td>
     <td><form:input path="lastName" value="${employeeObject.lastName}"/></td>
    </tr>
    <tr>
     <td><form:label path="email">Email</form:label></td>
     <td><form:input path="email" value="${employeeObject.email}"/></td>
    </tr>
    <tr>
     <td><form:label path="phone">Phone</form:label></td>
     <td><form:input path="phone" value="${employeeObject.phone}"/></td>
    </tr>
    <tr>
     <td></td>
     <td><input type="submit" value="Update" />
     </td>
    </tr>
   </table>
  </form:form>



 </center>
</body>
</html>

DataController.java 控制器类

    package com.controller;

import com.model.Employee;
import com.service.DataService;
import com.sun.javafx.sg.prism.NGShape;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by khan on 01/12/16.
 */
@Controller
public class DataController {
    @Autowired
    DataService dataService;


    @RequestMapping("form")
    public ModelAndView getForm(@ModelAttribute Employee employee) {
        return new ModelAndView("form");
    }

    @RequestMapping("register")
    public ModelAndView registerUser(@ModelAttribute Employee employee) {
        dataService.insertRow(employee);
        return new ModelAndView("redirect:list");
    }

    @RequestMapping("list")
    public ModelAndView getList() {
        List employeeList = dataService.getList();
        return new ModelAndView("list", "employeeList", "employeeList");
    }

    @RequestMapping("delete")
    public ModelAndView deleteUser(@RequestParam int id) {
        dataService.deleteRow(id);
        return new ModelAndView("redirect:list");
    }

    @RequestMapping("edit")
    public ModelAndView editUser(@RequestParam int id, @ModelAttribute Employee employee) {
        Employee employee1 = dataService.getRowById(id);
        return new ModelAndView("edit", "employee1", "employee1");
    }

    @RequestMapping("update")
    public ModelAndView updateUser(@ModelAttribute Employee employee) {
        dataService.updateRow(employee);
        return new ModelAndView("redirect:list");
    }
}

DataDao.java接口

    package com.dao;

import com.model.Employee;

import java.security.spec.ECFieldF2m;
import java.util.List;

/**
 * Created by khan on 01/12/16.
 */
public interface DataDao {
    public int insertRow(Employee employee);

    public List getList();

    public Employee getRowById(int id);

    public int updateRow(Employee employee);

    public int deleteRow(int id);
}

DataDaoImpl.java 类

    package com.daoImpl;

import com.dao.DataDao;
import com.model.Employee;
import org.dom4j.io.ElementModifier;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.transform.impl.InterceptFieldCallback;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * Created by khan on 01/12/16.
 */
@Repository
public class DataDaoImpl implements DataDao {
    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public int insertRow(Employee employee) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        session.saveOrUpdate(employee);
        transaction.commit();
        Serializable id = session.getIdentifier(employee);
        session.close();
        return (Integer) id;

    }

    public List getList() {
        Session session = sessionFactory.openSession();
        @SuppressWarnings("unchecked")
        List employeeList = session.createQuery("from Emp").list();
        session.close();
        return employeeList;
    }

    public Employee getRowById(int id) {
        Session session = sessionFactory.openSession();
        return (Employee) session.load(Employee.class, id);
    }

    public int updateRow(Employee employee) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        session.saveOrUpdate(employee);
        transaction.commit();
        Serializable id = session.getIdentifier(employee);
        session.close();
        return (Integer) id;
    }

    public int deleteRow(int id) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Employee employee = (Employee) session.load(Employee.class, id);
        session.delete(employee);
        transaction.commit();
        Serializable sid = session.getIdentifier(employee);
        return (Integer) sid;
    }
}

Employee.java

    package com.model;

import org.springframework.beans.factory.annotation.Qualifier;

import javax.persistence.*;

/**
 * Created by khan on 01/12/16.
 */
@Entity
@Table(name = "Emp")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String fistName;
    private String lastName;
    private String email;
    private String phone;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFistName() {
        return fistName;
    }

    public void setFistName(String fistName) {
        this.fistName = fistName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

DataService.java 接口

    package com.service;

import com.model.Employee;

import java.util.List;

/**
 * Created by khan on 01/12/16.
 */
public interface DataService {
    public int insertRow(Employee employee);

    public List getList();

    public Employee getRowById(int id);

    public int updateRow(Employee employee);

    public int deleteRow(int id);

}

DataServiceImpl.java

   package com.serviceImpl;

import com.dao.DataDao;
import com.model.Employee;
import com.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by khan on 01/12/16.
 */
@Service("DataService")
public class DataServiceImpl implements DataService {
    @Autowired
    private DataDao dataDao;

    public int insertRow(Employee employee) {
        return dataDao.insertRow(employee);
    }

    public List getList() {
        return dataDao.getList();
    }

    public Employee getRowById(int id) {
        return dataDao.getRowById(id);
    }

    public int updateRow(Employee employee) {
        return dataDao.updateRow(employee);
    }

    public int deleteRow(int id) {
        return dataDao.deleteRow(id);
    }
}

pom.xml 文件

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com</groupId>
  <artifactId>crud</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>crud Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
  <spring.version>4.0.5.RELEASE</spring.version>
  <hibernate.version>4.3.5.Final</hibernate.version>
  <log4j.version>1.2.17</log4j.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>${hibernate.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>9.3-1100-jdbc4</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>crud</finalName>
  </build>
</project>

在此先感谢您协助解决该问题。

【问题讨论】:

    标签: jsp spring-mvc servlets


    【解决方案1】:

    您似乎使事情复杂化了,您可以直接使用http://localhost:8080/crud/form 直接访问您的form.jsp(因为您的控制器方法在访问/form 时会为您提供form.jsp)。

    或者第二个选项是您可以将index.jsp 作为welcome-file 添加到web.xml 中,以便您可以访问http://localhost:8080/crud/ url:

     <welcome-file-list>  
       <welcome-file>index.jsp</welcome-file>  
      </welcome-file-list>  
    

    另外,请仔细检查您的项目上下文根目录是否为crud(即,如果您使用的是 Tomcat,您的crud 项目应该可用/部署在webapps 文件夹下) p>

    【讨论】:

    • 我应用了这两种方法仍然无法正常工作。在发布这个问题之前,我尝试了这些方法。而且我知道您的解决方案是正确的,但我不知道在这个项目中不起作用请再次检查我在哪里做错了谢谢
    • 我也尝试将 index.jsp 放入 jsp 文件夹并使用welcome-file-list 进行映射但不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 2017-03-23
    • 2012-01-21
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    相关资源
    最近更新 更多