Project Folder structure我在Sring mvc应用程序上浏览了本站点上与 404资源未找到错误相关的大部分问题。我尝试了所有的解决方案,结果仍然没有超过两天的解决方案。我是Spring MVC的新手,并从链接中试用了一个示例应用程序 Simple Spring MVC app
我的代码完全如上面的网站所述。但无论我做出什么改变,仍然会收到404错误。
这是我的 web.xml 部分
@Controller
public class EmployeeController {
@RequestMapping(value = "/employee", method = RequestMethod.GET)
public ModelAndView employee() {
return new ModelAndView("employeeForm", "command", new Employee());
}
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public String addEmployee(@ModelAttribute("SpringWeb")Employee employee, ModelMap model) {
model.addAttribute("name", employee.getName());
model.addAttribute("age", employee.getAge());
model.addAttribute("empId", employee.getEmpId());
model.addAttribute("salary", employee.getSalary());
return "employeeDetail";
}
}
控制器文件, 的 EmployeeController.java
<form:form action="/addEmployee" method="POST">
<table><tbody>
<tr> <td><form:label path="empId">Employee :</form:label></td> <td><form:input path="empId"></form:input></td> </tr>
<tr> <td><form:label path="name">EmployeeName:/form:label></form:label></td> <td><form:input path="name"></form:input></td> </tr>
<tr> <td><form:label path="age">Employee Age:</form:label></td> <td><form:input path="age"></form:input></td> </tr>
<tr> <td><form:label path="salary">Employee Salary:</form:label></td> <td><form:input path="salary"></form:input></td> </tr>
<tr> <td colspan="2"><input type="submit" value="Submit"/> </td> </tr>
</tbody></table>
</form:form>
其中Employee是我的POJO类,带有setter和getter方法。
这是我的 employeeForm.jsp
<context:component-scan base-package="com.dineshonjava.emp.controller">
</context:component-scan>
<context:annotation-config/>
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
这是我的 sdnext-servlet.xml
posts.php
答案 0 :(得分:0)
你犯了一个根本性的错误。假设您的项目名称是bunbub-proj,因此要访问的网址是localhost:8080/bunbub-proj
。当您发布到/addEmployee
时,实际上是在点击localhost:8080/addEmployee
而不是localhost:8080/bunbub-proj/addEmployee
修复方法是简单地将.
附加到您的所有links/urls
。例如。 <form:form action="./addEmployee" method="POST">
注意:浏览器将localhost:8080视为基本URL或主机 网址。