2016-12-04 129 views
-1

我想使用休眠開發Crud Spring mvc webapp,我讀了很多解決方案來解決這個問題,但我沒有想法解決。請協助解決這個問題。請求資源不可用在春天mvc HTTP狀態404

項目結構。

Project Structure

異常頁面

enter image description here

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> 

彈簧-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.javaController類

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> 

在此先感謝幫助這個問題。

回答

0

好像你的事情複雜,你可以簡單地訪問您的form.jsp直接使用http://localhost:8080/crud/form(因爲你的控制器的方法讓你在訪問/formform.jsp)。

或第二選擇是,你可以添加index.jspwelcome-fileweb.xml,這樣就可以訪問http://localhost:8080/crud/網址:

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

此外,仔細檢查您的項目上下文根爲crud(即如果你正在使用Tomcat,你的crud項目應該可用/部署在webapps文件夾下)

+0

我應用這兩種方法,它仍然不工作。發佈這個問題之前,我嘗試了這種方法。我知道你的解決方案是正確的,但我不知道在這個項目不工作,請再次你可以檢查我的錯誤,謝謝你 –

+0

我也嘗試把index.jsp放入jsp文件夾並映射到welcome-file-list但不工作。 –

相關問題