【问题标题】:Spring MVC from example not working示例中的 Spring MVC 不起作用
【发布时间】:2014-01-29 18:06:11
【问题描述】:

我是 Spring 的新手,我正在尝试开发一个 Spring 应用程序,其中在 jsp 页面中将提供有关学生的一些信息,当我们按下提交按钮时,信息将返回另一个 jsp 页面,这是我的摩托车。我已经用 ecrise ide 完成了这个。但是当我尝试运行这个例子时,404 即将到来

我在这里发布我的完整代码。

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>HelloWeb</display-name>

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

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

HelloWeb-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<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="com.tutorialspoint" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix" value="/WEB-INF/jsp/" />
     <property name="suffix" value=".jsp" />
   </bean>

 </beans>

bean类和Controller在同一个包com.tutorialspoint下

Student.java 是 bean 类

public class Student {
private Integer age;
private String name;
private Integer id;

public void setAge(Integer age) {
    this.age = age;
}

public Integer getAge() {
    return age;
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setId(Integer id) {
    this.id = id;
}

public Integer getId() {
    return id;
}
}

StudentController.java 是控制器类

import org.springframework.stereotype.Controller;
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 org.springframework.ui.ModelMap;

   @Controller
   public class StudentController {

     @RequestMapping(value = "/student", method = RequestMethod.GET)
      public ModelAndView student() {
      return new ModelAndView("student", "command", new Student());
    }

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
    public String addStudent(@ModelAttribute("SpringWeb")Student student, 
    ModelMap model) {
      model.addAttribute("name", student.getName());
      model.addAttribute("age", student.getAge());
      model.addAttribute("id", student.getId());

      return "result";
   }
}

WEB-INF下的jsp文件夹是我们有两个页面的视图student.jsp & result.jsp

student.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Student Information</h2>
<form:form method="POST" action="/HelloWeb/addStudent">
 <table>
   <tr>
    <td><form:label path="name">Name</form:label></td>
    <td><form:input path="name" /></td>
</tr>
<tr>
    <td><form:label path="age">Age</form:label></td>
    <td><form:input path="age" /></td>
</tr>
<tr>
    <td><form:label path="id">id</form:label></td>
    <td><form:input path="id" /></td>
</tr>
<tr>
    <td colspan="2">
        <input type="submit" value="Submit"/>
      </td>
  </tr>
</table>  
  </form:form>
   </body>
  </html>

result.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
  <title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Submitted Student Information</h2>
  <table>
<tr>
    <td>Name</td>
    <td>${name}</td>
</tr>
<tr>
    <td>Age</td>
    <td>${age}</td>
</tr>
<tr>
    <td>ID</td>
    <td>${id}</td>
 </tr>
   </table>  
   </body>
   </html>

这是我尝试过的完整代码,我还添加了 Spring 库。但是当我尝试运行它时,总是出现 404 错误。请大家帮忙

【问题讨论】:

  • 哪个请求给你一个 404?
  • 当我右键单击项目并单击运行时,它会给出

标签: java spring jsp spring-mvc servlets


【解决方案1】:

在您的HelloWeb-servlet.xml 中,您需要配置您的 MVC 环境。这样做

<mvc:annotation-driven />

此配置元素注册它找到的任何 bean,它是一个 @Controller 并具有 @RequestMappingmethods 作为 HTTP 请求的处理程序。

您将需要适当的 XML 命名空间声明。

【讨论】:

  • 我正在发回正确的 HelloWeb-servlet.xml,请检查
  • @Subho It's not working 绝不是您应该单独发布的内容。什么不工作?它怎么不起作用?它在做什么出乎你的意料?
  • 当我尝试运行项目时,它会给出 404 错误,但它应该显示 student.jsp 页面,我将在其中输入值
  • @subho 您尝试访问哪个 URL 会导致 404?
  • @Subho 使用 URL localhost:8080/HelloWeb/student,因为这是您的 Controller 处理程序方法映射到的内容。
【解决方案2】:

这是因为你正在使用&lt;url-pattern&gt;/&lt;/url-pattern&gt;拦截jsp文件 所以分辨率应该是这样的。

  1. 在 web.xml 中使用&lt;url-pattern&gt;*.htm&lt;/url-pattern&gt;
  2. 在 student.jsp &lt;form:form method="POST" action="/HelloWeb/addStudent.htm"&gt;

【讨论】:

  • 默认情况下,Tomcat(我假设 OP 正在使用它)为扩展 *.jsp 注册一个 JspServlet。这应该在匹配DispatcherServlet/ 之前匹配转发到JSP 的请求。
【解决方案3】:

几个月前,我浏览了该网站上的所有示例,并牢牢记住了它们。

@Sotirios Delimanolis,我相信只要您使用基于 xml 的配置并进行组件扫描,注释驱动的标签就不是必需的。 @Controller 注释仍应注册。您建议的注释标签不在原始教程中,也不在我的工作示例中(仍在我的笔记本电脑上),它仍然运行。此外,在我看来,教程 XML 中的所有 XML 命名空间声明都显示在 OP 的 [servlet-name]-servlet.xml 文件中。

@Pankaj Sharma,web.xml 中的 / 符号是本教程中显示的内容,在我的示例中工作正常,我的 .jsp 与 action="/HelloWeb/addStudent" 一起使用。 无论如何,该 jsp 操作应该只会影响到“结果”屏幕,而 OP 甚至还没有走那么远。

OP,首先我会确保您已将教程中调用的所有 jar 文件添加到您的构建路径中。另外,我想知道您是否尝试正确调用您的教程应用程序。在我的 MyEclipseBlue IDE 中,即使左键单击并尝试将其作为“MyEclipse 服务器应用程序”运行,我也无法运行它。整个 URL 没有出现,它缺少匹配控制器中的模式所需的 URL 中的 /student。 如果您仔细阅读该示例,它会建议您启动服务器并打开一个新的浏览器窗口,然后输入与原始教程中完全相同的 URL。这适用于我的应用程序,或者将其作为“MyEclipse 服务器应用程序”运行并输入附加 /student 的完整 URL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    相关资源
    最近更新 更多