【问题标题】:Maven Spring WebApp HTTP Status 404Maven Spring WebApp HTTP状态404
【发布时间】:2015-11-10 22:20:08
【问题描述】:

尝试运行 student_list.jsp,运行后我得到 HTTP 状态 404。 在这里,我发送了我在 Netbeans 中设置的 maven 项目的照片和代码。请检查一切是否设置正确。我是Spring的初学者。

谢谢

代码:

student_list.jsp

<%@page import="java.util.List"%>
<%@page import="cz.webapp.student.entity.Student"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="s" uri="http://www.springframework.org/tags"%>

 
<div>
<h2>List of Persons</h2>
 
<table title="List Of Persons" border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
 
<%
List<Student> studentList = (List)request.getAttribute("students");
for(Student student: studentList){
out.println("<tr>");
out.println("<td>" + student.getId() + "</td>");
out.println("<td>" + student.getName() + "</td>");
out.println("<td>" + student.getAge() + "</td>");
out.println("</tr>");
}
 
%>
 
</table>

</div>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

   <display-name>Spring MVC Application</display-name>

   <servlet>
      <servlet-name>StudentWebApp</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>StudentWebApp</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
 
</web-app>

StudentWebApp-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   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">

   <context:component-scan base-package="cz.webapp.student" />
 
   
   
   
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

学生控制器

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.webapp.student.controllers;

import cz.webapp.student.entity.Student;
import cz.webapp.student.service.StudentService;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Jenda
 */

@Controller
public class StudentController {
    
    @Autowired
    StudentService studentServiceImpl;
    
    
    
@RequestMapping(value="/students", method=GET)
public String showAllStudent(Map<String, Object> model){
List<Student> studentList = studentServiceImpl.findAll();

model.put("students", studentList);


return "student_list";
    
}
}

【问题讨论】:

  • 你能打印堆栈跟踪吗?它们确实有助于发现问题。

标签: spring spring-mvc http-status-code-404


【解决方案1】:

我想我可能发现了一个问题。在您的 servlet.xml 文件中,您的基本包似乎是错误的。试试这个:<context:component-scan base-package="cz.webapp.student.*" />

因为我认为当您将包设置为 cz.webapp.student.controllers 时,Spring 正在寻找一个名为 cz.webapp.student 的包

导致 404 错误的原因有很多,错误的基础包就是其中之一,所以我不确定这是否能解决它,但这是一个好的开始。

【讨论】:

    猜你喜欢
    • 2015-11-25
    • 2015-10-09
    • 2019-10-27
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多