2014-01-29 106 views
0

我是Spring中的新成员我正在尝试开发一个Spring应用程序,其中在jsp页面中将提供有关学生的一些信息,并且当我们按下提交按钮时,信息将返回另一个jsp页面,这是我的moto.I已经完成了eclise ide.But,但是当我尝试运行这个例子时,404即将到来project structure in eclipse来自示例不工作的Spring MVC

我在这里发布我的完整代码。相同的包com.tutorialspoint

Student.java下

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类和控制器是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是控制器类

在WEB-INF
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"; 
    } 
} 

这里的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错误。有人请帮助

+0

哪个要求给你404? –

+0

当我右键点击该项目,然后单击运行,然后它给 – Subho

回答

0

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

<mvc:annotation-driven /> 

此配置元件寄存器任何豆它发现它是一个@Controller并且具有@RequestMapping方法作为HTTP请求的处理程序。

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

+0

我发布回正确的HelloWeb-servlet.xml,你请检查 – Subho

+0

它不工作 – Subho

+0

@Subho'不工作'是永远不应该你应该单独发布的东西。什么不起作用?它怎么不起作用?你不期望的是什么? –

0

这是因为你正在拦截jsp文件以及使用<url-pattern>/</url-pattern> 所以Resolution就是这样的。

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

默认情况下,Tomcat(我认为是OP是什么使用)为扩展名* .jsp注册'JspServlet'。这应该匹配请求以在匹配“DispatcherServlet”的'/'之前转发到JSP。 –

0

我通过在该网站上所有的例子在几个月前的工作,并记住他们。

@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中的/学生。 如果您仔细阅读示例,它会建议您启动服务器并打开一个新的浏览器窗口,然后输入完全按照原始教程中所示的URL。这适用于我的应用程序,或者将其作为“MyEclipse服务器应用程序”运行,然后输入带有/ student附加的完整URL。