【问题标题】:404 error on a simple spring mvc aplication一个简单的 spring mvc 应用程序上的 404 错误
【发布时间】:2016-02-15 20:56:42
【问题描述】:

Project Folder structure我已经解决了这个网站上关于 Sring mvc 应用程序的 404 resource not found error 的大部分问题。我尝试了所有解决方案,但两天多后仍未解决。我是 Spring MVC 的新手,并从链接中试用了一个示例应用程序 Simple Spring MVC app

我的代码与上面网站上提到的完全一样。但是无论我做什么更改,仍然会出现 404 错误。

这是我的 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>abc</display-name>
  <welcome-file-list>
    <welcome-file>/</welcome-file>
  </welcome-file-list>

  <servlet>  
        <servlet-name>sdnext</servlet-name>  
        <servlet-class>  
            org.springframework.web.servlet.DispatcherServlet  
        </servlet-class>  
      <init-param>  
            <param-name>contextConfigLocation</param-name><param-value>/WEB-INF/config/sdnext-servlet.xml</param-value></init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>sdnext</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>
</web-app>

控制器文件, EmployeeController.java

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

其中 Employee 是我的带有 setter 和 getter 方法的 POJO 类。

这是我的 employeeForm.jsp

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

这是我的 sdnext-servlet.xml

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

【问题讨论】:

  • 根据你的配置我想给你提示,你必须请求调用你的控制器的 url 至少是http://localhost:8080/&lt;APPLICATIONNAME&gt;/sdnext/employee。 (您没有发布完整的控制器) - 但我想说的是,如果您的应用程序名称也是sdnext,那么您的网址中将有两个sdnext! --- 第二:当您遇到 404 问题时,请至少发布您尝试请求的网址!
  • 请检查我发布的答案。如果它解决了您的问题,请接受它。
  • @Ralph 我已经相应地编辑了我的帖子以包含完整的控制器。我尝试访问的 URL 是 URL: 正在尝试访问 @987654323 @ 其中 sdnext 是来自 web.xml 的我的 servlet 名称,我也尝试使用 localhost:8080/abc/sdnext/employee 访问其中 abc 是我的应用程序名称我仍然收到 404 错误
  • 请使用此 URL localhost:8080/abc/employee 访问应用程序

标签: spring spring-mvc model-view-controller http-status-code-404


【解决方案1】:

你犯了一个根本性的错误。假设您的项目名称是 bunbub-proj,那么访问的 url 是localhost:8080/bunbub-proj。当您发布到/addEmployee 时,您实际上是在点击localhost:8080/addEmployee 而不是localhost:8080/bunbub-proj/addEmployee

解决方法是简单地将. 附加到所有links/urls。例如。 &lt;form:form action="./addEmployee" method="POST"&gt;

注意:浏览器将 localhost:8080 视为基本 url 或主机 网址。

【讨论】:

  • URL: 正在尝试访问 localhost:8080/sdnext/employee 其中 sdnext 是我来自 web.xml 的 servlet 名称,我也尝试使用 localhost:8080/abc/sdnext/employee 其中 abc 是我的应用程序名称我仍然收到 404 错误
  • 我也尝试过附加一个 .正如你所建议的,但它似乎不起作用
  • 请在您的问题中完整粘贴 web.xml。现在会修复它。
  • 我在控制台中收到此错误 在 DispatcherServlet 中找不到带有 URI [/abc/sdnext/employee] 的 HTTP 请求的映射,名称为 'sdnext'
  • 项目名称是 abc,如果没有请在评论中告诉我。访问 localhost:8080/abc/employee 的 URL 然后做我在答案中建议的更改。
猜你喜欢
  • 2019-03-28
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
相关资源
最近更新 更多