我正在使用Maven在Spring MVC中实现一个演示(员工注册系统)。但是我遇到了与上下文根(URL格式)有关的错误。
该错误是由于上下文根在操作项之前不可用。
我的问题不是这个: How to use relative paths without including the context root name?
我有一个问题,何时将上下文根自动附加在URL中,何时不附加。阅读完我的文章后,我在JSP表单中使用了action =“ / saveEmployee”,action =“ / updateEmployee”。当我提交具有action =“ / saveEmployee”的表单时,形成的URL是:http://localhost:8080/DemoMvcCrud_JavaTPoint/saveEmployee
但是,当我提交具有action =“ / updateEmployee”的表单时,形成的URL是:
http://localhost:8080/updateEmployee。那是这里缺少上下文。如下图所示。
这是项目结构:
文件内容如下:
1. EmployeeController.java
package com.umang.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.umang.dao.EmployeeDao;
import com.umang.model.Employee;
@Controller
public class EmployeeController {
@Autowired
EmployeeDao employeeDao;
@RequestMapping("/addEmployeePage")
public String addEmployeePage(Model m) {
m.addAttribute("command",new Employee());
return "addEmployee";
}
@RequestMapping(value="/saveEmployee",method=RequestMethod.POST)
public String saveEmployee(@ModelAttribute("emp") Employee ob) {
employeeDao.save(ob);
return "redirect:/viewEmployees";
}
@RequestMapping("/updateEmployee")
public String updateEmployee(@ModelAttribute("emp") Employee emp)
{
employeeDao.update(emp);
return "redirect:/viewEmployees";
}
@RequestMapping("/viewEmployees")
public String viewEmployees(Model m) {
List<Employee> list=employeeDao.getEmployees();
m.addAttribute("list",list);
return "viewEmployees";
}
@RequestMapping(value="/editEmployee/{id}")
public String editEmployee(@PathVariable int id,Model m) {
Employee emp=employeeDao.getEmployeeById(id);
m.addAttribute("command",emp);
return "employeeEditForm";
}
@RequestMapping(value="/deleteEmployee/{id}")
public String deleteEmployee(@PathVariable int id) {
employeeDao.delete(id);
return "redirect:/viewEmployees";
}
}
2。 EmployeeDao.java:包含jdbc CRUD操作。
3。 Employee.java:包含员工模型。
4。 AddEmployee.jsp
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
<h1>Add New Employee</h1>
<form:form action="saveEmployee" method="post">
Name : <form:input path="name"/><br>
Salary : <form:input path="salary"/><br>
Designation: <form:input path="designation"/><br>
<input type="submit" value="SAVE Details"/>
</form:form>
</body>
</html>
5.EmployeeEditForm.jsp:
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Edit Employee Details</h1>
<form:form action="/updateEmployee" method="post">
<form:hidden path="id"/>
Name : <form:input path="name"/><br>
Salary : <form:input path="salary"/><br>
Designation : <form:input path="designation"/><br>
<input type="submit" value="Update Details">
</form:form>
</body>
</html>
6。 ViewEmployees.jsp:
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h1>Employees List</h1>
<table border="2" width="70%" cellpadding="2">
<tr><th>Id</th><th>Name</th><th>Salary</th><th>Designation</th><th>Edit</th><th>Delete</th></tr>
<c:forEach var="emp" items="${list}">
<tr>
<td>${emp.id}</td>
<td>${emp.name}</td>
<td>${emp.salary}</td>
<td>${emp.designation}</td>
<td><a href="editEmployee/${emp.id}">Edit</a>
<td><a href="deleteEmployee/${emp.id}">Delete</a>
</tr>
</c:forEach>
</table>
<br>
<a href="addEmployee">Add New Employee</a>
</body>
</html>
7。 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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.umang.controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/umang?useSSL=false"></property>
<property name="username" value="root"></property>
<property name="password" value="pass123"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="dao" class="com.umang.dao.EmployeeDao">
<property name="jdbcTemplate" ref="jdbcTemplate" ></property>
</bean>
</beans>
8.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"
xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"
version="2.5">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这是问题所在:
当我添加员工时,它被添加到数据库中。当我查看员工时,我可以查看它。
当我编辑Employee时,我会得到一个如下的employeeEditForm.jsp页面
最后,当我单击“更新”按钮时,URL为:
由于上下文根不存在,因此将引发异常。
而且我不知道为什么在/ updateEmployee之前没有附加上下文根
(missing-context-root)/ updateEmployee
对于其他操作,上下文根会自动添加到URL。
请帮助我解决此问题,并让我知道是否需要更多信息来遏制此问题。