2016-11-29 36 views
1

Project Structure請求的資源不可用Spring MVC中給出

Wlcome Page

點擊AddEmployee鏈接後下一個頁面打開時,我在提交點擊當時的下一頁開幕式那樣

save page

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="/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> 

web.xml文件

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

<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>*.html</url-pattern> 
</servlet-mapping> 
<welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
</welcome-file-list> 

彈簧servlet.xml中文件

<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" 
     xmlns="http://www.springframework.org/schema/beans" 
     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:component-scan base-package="com"></context:component-scan> 
    <tx:annotation-driven transaction-manager="hibernateTransactionManager"></tx:annotation-driven> 
    <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 class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> 

     <property name="driverClassName" value="org.postgresql.Driver"></property> 

     <property name="url" value="jdbc:postgresql://localhost:5433/databaseDb"></property> 
     <property name="username" value="postgres"></property> 
     <property name="password" value="postgres"></property> 
    </bean> 

    <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory"> 

     <property name="dataSource" ref="dataSource"></property> 
     <property name="annotatedClasses"> 

      <list> 
       <value>com.model.Employee</value> 
      </list> 

     </property> 
     <property name="hibernateProperties"> 
      <props> 

       <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</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 class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="hibernateTransactionManager"> 

     <property name="sessionFactory" ref="sessionFactory"></property> 

    </bean> 
</beans> 

EmployeeController類

package com.controller; 

import com.bean.EmployeeBean; 
import com.model.Employee; 
import com.service.EmployeeService; 
import com.sun.javafx.collections.MappingChange; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.propertyeditors.StringArrayPropertyEditor; 
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 javax.persistence.ManyToOne; 
import javax.persistence.OneToMany; 
import java.util.*; 

/** 
* Created by khan on 28/11/16. 
*/ 
@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 listEmployee() { 
     Map<String, Object> map = new HashMap<String, Object>(); 
     map.put("employees", prepareListofBean(employeeService.listEmployees())); 
     return new ModelAndView("employeesList", map); 
    } 

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

    @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) { 
     Map<String, Object> map = new HashMap<String, Object>(); 
     map.put("employee", null); 
     map.put("employees", prepareListofBean(employeeService.listEmployees())); 
     return new ModelAndView("addEmployee", map); 
    } 

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

    private EmployeeBean prepareEmployeebean(Employee employee) { 
     EmployeeBean employeeBean = new EmployeeBean(); 
     employeeBean.setAddress(employee.getEmpAddress()); 
     employeeBean.setAge(employee.getEmpAge()); 
     employeeBean.setSalary(employee.getEmpSalary()); 
     employeeBean.setName(employee.getEmpName()); 
     employeeBean.setId(employee.getEmpId()); 
     return employeeBean; 
    } 

    private Employee prepareModel(EmployeeBean employeeBean) { 
     Employee employee = new Employee(); 
     employee.setEmpAddress(employeeBean.getAddress()); 
     employee.setEmpAge(employeeBean.getAge()); 
     employee.setEmpName(employeeBean.getName()); 
     employee.setEmpSalary(employeeBean.getSalary()); 
     employee.setEmpId(null); 
     return employee; 
    } 

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

} 

的EmployeeService接口

package com.service; 

import com.dao.EmployeeDao; 
import com.model.Employee; 
import org.springframework.stereotype.Service; 

import java.util.List; 

/** 
* Created by khan on 28/11/16. 
*/ 
public interface EmployeeService { 
    public void addEmployee(Employee employee); 

    public List<Employee> listEmployees(); 

    public Employee getEmployee(int empid); 

    public void deleteEmployee(Employee employee); 
} 

EmployeeServiceImpl類

package com.serviceImpl; 

import com.dao.EmployeeDao; 
import com.model.Employee; 
import com.service.EmployeeService; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Propagation; 
import org.springframework.transaction.annotation.Transactional; 

import java.util.List; 

/** 
* Created by khan on 28/11/16. 
*/ 
@Service("EmployeeService") 
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true) 
public class EmployeeServiceImpl implements EmployeeService { 
    @Autowired 
    private EmployeeDao employeeDao; 

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false) 
    public void addEmployee(Employee employee) { 
     employeeDao.addEmployee(employee); 
    } 

    public List<Employee> listEmployees() { 
     return (List<Employee>) employeeDao.listEmployees(); 
    } 

    public Employee getEmployee(int empid) { 
     return employeeDao.getEmployee(empid); 
    } 

    public void deleteEmployee(Employee employee) { 
     employeeDao.deleteEmployee(employee); 

    } 
} 

EmployeeDao接口

package com.dao; 

import com.model.Employee; 

import java.util.List; 


/** 
* Created by khan on 28/11/16. 
*/ 
public interface EmployeeDao { 
    public List<Employee> listEmployees(); 

    public void addEmployee(Employee employee); 

    public Employee getEmployee(int empid); 

    public void deleteEmployee(Employee employee); 
} 

EmployeDaoImpl類

package com.daoImpl; 

import com.bean.EmployeeBean; 
import com.dao.EmployeeDao; 
import com.model.Employee; 

import org.hibernate.Criteria; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 

import java.util.List; 


/** 
* Created by khan on 28/11/16. 
*/ 
@Repository 
public class EmployeeDaoImpl implements EmployeeDao { 
    @Autowired 
    private SessionFactory sessionFactory; 

    @SuppressWarnings("unchecked") 
    public List<Employee> listEmployees() { 
     Transaction transaction = sessionFactory.getCurrentSession().beginTransaction(); 
     Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Employee.class); 
     List<Employee> list = (List<Employee>) criteria.list(); 
     transaction.commit(); 
     return list; 
    } 

    public void addEmployee(Employee employee) { 
     Transaction transaction = sessionFactory.getCurrentSession().beginTransaction(); 
     sessionFactory.getCurrentSession().saveOrUpdate(employee); 
     transaction.commit(); 
    } 

    public Employee getEmployee(int empid) { 
     return (Employee) sessionFactory.getCurrentSession().get(Employee.class, empid); 
    } 

    public void deleteEmployee(Employee employee) { 
     Transaction transaction = sessionFactory.getCurrentSession().beginTransaction(); 
     sessionFactory.getCurrentSession() 
       .createQuery("Delete from Employee where id=" + employee.getEmpId()).executeUpdate(); 
     transaction.commit(); 

    } 
} 

Employee類

package com.model; 

import javax.persistence.*; 

/** 
* Created by khan on 28/11/16. 
*/ 
@Entity 
@Table(name = "Employee") 
public class Employee { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "empid") 
    private Integer empId; 
    @Column(name = "empage") 
    private Integer empAge; 
    @Column(name = "empname") 
    private String empName; 
    @Column(name = "empaddress") 
    private String empAddress; 
    @Column(name = "empsalary") 
    private Long empSalary; 

    public Integer getEmpId() { 
     return empId; 
    } 

    public void setEmpId(Integer empId) { 
     this.empId = empId; 
    } 

    public Integer getEmpAge() { 
     return empAge; 
    } 

    public void setEmpAge(Integer empAge) { 
     this.empAge = empAge; 
    } 

    public String getEmpName() { 
     return empName; 
    } 

    public void setEmpName(String empName) { 
     this.empName = empName; 
    } 

    public String getEmpAddress() { 
     return empAddress; 
    } 

    public void setEmpAddress(String empAddress) { 
     this.empAddress = empAddress; 
    } 

    public Long getEmpSalary() { 
     return empSalary; 
    } 

    public void setEmpSalary(Long empSalary) { 
     this.empSalary = empSalary; 
    } 
} 

引用EmployeeBean類

package com.bean; 

import org.omg.CORBA.INTERNAL; 

/** 
* Created by khan on 28/11/16. 
*/ 
public class EmployeeBean { 
    private Integer id; 
    private Integer age; 
    private String name; 
    private Long salary; 
    private String address; 

    public Integer getId() { 
     return id; 
    } 

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

    public Integer getAge() { 
     return age; 
    } 

    public void setAge(Integer age) { 
     this.age = age; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public Long getSalary() { 
     return salary; 
    } 

    public void setSalary(Long salary) { 
     this.salary = salary; 
    } 

    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 
} 

請幫助我,我嘗試,但沒有得到如何解決這個問題。
預先感謝

+0

你缺少的重要組成部分。 .LOGS ..? – Jobin

+0

你得到的例外是什麼 – dimitrisli

+0

@Jobin我添加了快照LOGS請找出第3張圖片。 –

回答

1

每一個哥們改變你的表單動作

<form:form action="${pageContext.request.contextPath}/save.html" method="POST"> 

.. 

</form:form> 
+0

謝謝我將我的代碼更新到您給定的答案 –

+0

感謝您給定的代碼正常工作,但仍然收到異常,當我點擊編輯鏈接........... [link](http:// localhost:8080/waseem/edit.html?id = 4) –

+0

請求處理失敗;嵌套異常是org.hibernate.HibernateException:如果沒有活動事務,get無效 說明服務器遇到內部錯誤,導致它無法完成此請求。 異常 org.springframework.web.util.NestedServletException:請求處理失敗;嵌套的異常是org.hibernate.HibernateException:如果沒有活動事務,get無效 \t org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894) –

相關問題