简体   繁体   English

示例中的Spring MVC无法正常工作

[英]Spring MVC from example not working

I am new in Spring i am trying to develop a spring application where in a jsp page some information about a student will be given and when we will press the submit button then the the information will back in another jsp page,this is my moto.I have done this with eclise ide.But when i try to run this example the 404 is coming 我是Spring的新手,我正在尝试开发一个spring应用程序,其中将在jsp页面中提供有关学生的一些信息,当我们按Submit按钮时,该信息将返回到另一个jsp页面中,这是我的工作。我已经用idelise ide做到了。但是当我尝试运行此示例时,404即将到来 日食中的项目结构

I am posting my full code here . 我在这里发布我的完整代码。

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 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 class and Controller under the same package com.tutorialspoint 相同的包com.tutorialspoint下的bean类和Controller

Student.java is the bean class 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 is the controller class 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";
   }
}

Here jsp folder under WEB-INF is the view we have two page there student.jsp & result.jsp 在WEB-INF下的jsp文件夹是视图,我们有两个页面,student.jsp和result.jsp

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

This is the full code what I have tried I have also added the Spring library.But when I try to run it I always get a 404 error.Someone please help 这是我尝试过的完整代码,我还添加了Spring库。但是当我尝试运行它时,我总是会收到404错误。请有人帮忙

In your HelloWeb-servlet.xml , you need to configure your MVC environment. HelloWeb-servlet.xml ,您需要配置MVC环境。 Do that with

<mvc:annotation-driven />

This configuration element registers any bean it finds which is a @Controller and has @RequestMapping methods as a handler for HTTP requests. 此配置元素将找到的所有bean注册为@Controller并具有@RequestMapping方法作为HTTP请求的处理程序。

You will need the appropriate XML namespace declarations. 您将需要适当的XML名称空间声明。

it 's due to you are intercepting jsp files as well using <url-pattern>/</url-pattern> so Resolution would be like this. 这是由于您还使用<url-pattern>/</url-pattern>拦截了jsp文件,因此Resolution就是这样。

  1. in web.xml use <url-pattern>*.htm</url-pattern> 在web.xml中使用<url-pattern>*.htm</url-pattern>
  2. in student.jsp <form:form method="POST" action="/HelloWeb/addStudent.htm"> 在student.jsp中<form:form method="POST" action="/HelloWeb/addStudent.htm">

I worked through all the examples on that site a few months ago and remember them well. 几个月前,我浏览了该站点上的所有示例,并牢记它们。

@Sotirios Delimanolis, I believe the annotation-driven tag isn't necessary as long as you are using xml based configuration and doing component scanning. @Sotirios Delimanolis,我相信注释驱动的标签不是必需的,只要您使用基于xml的配置并进行组件扫描即可。 The @Controller annotation should still be registered. @Controller批注仍应注册。 The annotation tag you suggested isn't in the original tutorial, or in my working example (still on my laptop) either, and it still runs. 您建议的注释标记既不在原始教程中,也不在我的工作示例中(仍在笔记本电脑上),并且仍在运行。 Also, it looks to me like all the XML namespace declarations from the tutorial XML are shown in the OP's [servlet-name]-servlet.xml file. 另外,在我看来,教程XML中的所有XML名称空间声明都显示在OP的[servlet-name] -servlet.xml文件中。

@Pankaj Sharma, the / notation in the web.xml is what's shown in the tutorial and works fine in my example, and my .jsp works with action="/HelloWeb/addStudent". @Pankaj Sharma,web.xml中的/表示法是本教程中显示的,并且在我的示例中可以正常使用,而我的.jsp可与action =“ / HelloWeb / addStudent”一起使用。 Anyway, that jsp action should only effect going to the 'result' screen and the OP isn't even getting that far. 无论如何,该jsp操作应该只影响进入“结果”屏幕,而OP甚至还没有达到那么远。

OP, first I would be sure you have all the jar files called for in the tutorial added in your build path. OP,首先,我确定您已将本教程中要求的所有jar文件添加到了构建路径中。 Also, I'm wondering if you are attempting to invoke your tutorial app correctly. 另外,我想知道您是否尝试正确调用教程应用程序。 From my MyEclipseBlue IDE, I can't get it to run even when left clicking and trying to run it as a 'MyEclipse server application'. 从MyEclipseBlue IDE,即使单击鼠标左键并尝试将其作为“ MyEclipse服务器应用程序”运行,我也无法使其运行。 The entire URL does not come up, it lacks the /student in the URL needed to match the pattern in the controller. 整个URL不会出现,它在/ student中缺少匹配控制器中模式所需的URL。 If you read the example closely, it suggests starting up your server and opening up a new browser window, then entering the URL exactly as shown in the original tutorial. 如果仔细阅读该示例,则建议启动服务器并打开新的浏览器窗口,然后完全按照原始教程中所示输入URL。 This works on my application, or else run it as a 'MyEclipse server application' and enter the full URL with /student appended. 这可以在我的应用程序上运行,也可以作为“ MyEclipse服务器应用程序”运行,然后输入完整的URL,并附加/ student。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM