java - 请求的资源在Spring MVC中不可用-LMLPHP

java - 请求的资源在Spring MVC中不可用-LMLPHP

单击AddEmployee链接后,打开下面的页面

java - 请求的资源在Spring MVC中不可用-LMLPHP

当我点击提交,然后像这样的下一页打开

java - 请求的资源在Spring MVC中不可用-LMLPHP

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>




spring-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();

    }
}


员工阶层

  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;
    }
}


请帮助我,我尝试但未解决该问题。
提前感谢每个朋友

最佳答案

将您的表单操作更改为

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

..

</form:form>

09-25 19:48