【问题标题】:Need help on Spring MVC hello world在 Spring MVC hello world 上需要帮助
【发布时间】:2015-02-04 02:01:08
【问题描述】:

我正在尝试使用 Hibernate 在 NetBeans 8.0.2 上学习 Spring MVC,坦率地说我被卡住了。任何帮助表示赞赏。我只是想做一个非常简单的“hello world”类型的网站。

有人会在第一页点击提交按钮,结果页面将包含来自数据库的值列表。听起来很简单吧? 我在这里包含了 5 个非常短的文件,如果你愿意的话,希望能对你有所帮助。 “web.xml”、“dispatcher-servlet.xml”、“index.jsp”、“TeamController.java”、“secondView.jsp”

使用我的文件,我理解 Spring MVC 应该如何工作的方式如下......

1) 从 NetBeans 运行项目,会调出 index.jsp 文件。 发生这种情况是因为咨询了 Web.xml,其中包含以下行...

"<welcome-file>redirect.jsp</welcome-file>"

一旦查询了redirect.jsp,我们就会看到它有以下...

"<% response.sendRedirect("index.htm"); %>" 

因此,通过该重定向,我们返回到 web.xml,其中包含以下内容...

"<servlet-name>dispatcher</servlet-name>" 
"<url-pattern>*.htm</url-pattern>" 

因此,随着该请求以 index.htm 的形式出现,它将由调度程序 servlet 处理。 在调度器 servlet 的配置文件中,视图解析器将添加以下内容... p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> 因此 index.htm 被更改为 /WEN-INF/jsp/index.jsp 并且该页面被调出。

2) 此时 index.jsp 已启动。这个文件中唯一的东西是一个带有提交按钮的表单。目的只是让某人按下按钮并在屏幕上返回来自数据库的信息。我将 Hibernate 作为该 Web 项目的一部分,并且基于 DB 表“Team”创建了一个 java 类“Team.java”。它填充了一些记录。 目前我看到的是,一旦 index.jsp 出现并且我点击提交,它就会给出 404。

这是为表单显示的 URL “主机:端口/HelloWebFour/index.htm” 如果我做一个视图源,它会显示为 index.jsp。当我点击提交时,它会给出一个带有这个 url 的 404 “主机:端口/HelloWebFour/团队?” 顺便说一句,“HelloWebFour”是我在 NetBeans 中的项目名称。 我不确定发生了什么,如果我的理解是正确的,或者我是否需要添加任何内容。任何帮助表示赞赏... 非常短的代码文件如下...

“Web.xml”

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>/WEB-INF/applicationContext.xml</param-value> 
</context-param> 
<listener> 
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<servlet> 
<servlet-name>dispatcher</servlet-name> 
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
<load-on-startup>2</load-on-startup> 
</servlet> 
<servlet-mapping> 
<servlet-name>dispatcher</servlet-name> 
<url-pattern>*.htm</url-pattern> 
</servlet-mapping> 
<session-config> 
<session-timeout> 
30 
</session-timeout> 
</session-config> 
<welcome-file-list> 
<welcome-file>redirect.jsp</welcome-file> 
</welcome-file-list> 

</web-app> 

“调度程序-servlet.xml”

<?xml version='1.0' encoding='UTF-8' ?> 
<!-- was: <?xml version="1.0" encoding="UTF-8"?> --> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-4.0.xsd"     xmlns:context="http://www.springframework.org/schema/context"> 


<context:component-scan base-package="testnew1" /> 

<bean     class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> 

<!-- 
Most controllers will use the ControllerClassNameHandlerMapping above, but 
for the index controller we are using ParameterizableViewController, so we must 
define an explicit mapping for it. 
--> 
<bean id="urlMapping" 
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
<property name="mappings"> 
<props> 
<prop key="index.htm">indexController</prop> 
</props> 
</property> 
</bean> 

<bean id="viewResolver" 
class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
p:prefix="/WEB-INF/jsp/" 
p:suffix=".jsp" /> 

<!-- 
The index controller. 
--> 
<bean name="indexController" 
class="org.springframework.web.servlet.mvc.ParameterizableViewController" 
p:viewName="index" /> 

</beans> 

“index.jsp”

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>index.jsp - the submit page</title> 
</head> 

<body> 

<h2>Hit submit to get DB information</h2> 
<form:form method="GET" action="/HelloWebFour/team"> 
<table> 
<tr> 
<td> 
<input type="submit" value="Submit"/> 
</td> 
</tr> 
</table> 
</form:form> 
</body> 
</html> 

“团队控制器.java”

package testnew1; 

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


@Controller 
public class TeamController { 

@RequestMapping(value = "/team", method = RequestMethod.GET) 
public String getTeam(@ModelAttribute("SpringWeb") Team team, 
ModelMap model) { 
model.addAttribute("city", team.getCity()); 
model.addAttribute("state", team.getState()); 
model.addAttribute("nickname", team.getNickname()); 
return "secondView"; 
} 


} 

"secondView.jsp"

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>secondView.jsp - the results page</title> 
</head> 
<body> 
<h2>Database Information</h2> 
<table> 
<tr> 
<td>City</td> 
<td>${city}</td> 
</tr> 
<tr> 
<td>State</td> 
<td>${state}</td> 
</tr> 
<tr> 
<td>Nickname</td> 
<td>${nickname}</td> 
</tr> 
</table> 
</body> 


</html>

【问题讨论】:

    标签: spring hibernate jsp spring-mvc servlets


    【解决方案1】:

    据我所知,您正在提交到"/HelloWebFour/team,但您唯一的请求处理程序方法映射到/team,请尝试仅提交到/team。我没有在 Dispatcher 映射上使用任何前缀,因此可能是 url 上缺少的 .htm 与未知请求 url /HelloWebFour/team 相结合。

    然后,如果上述解决方案之一有效,您的方法可能会失败,因为您期望一个名为“SpringWeb”且类型为 Team 的 modelAttribute,但您的表单实际上不发布任何数据,并且它的 modelAttribute 映射到“command”,默认值,因此删除它可能会在发生时消除进一步的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      • 2013-04-18
      • 2015-03-25
      相关资源
      最近更新 更多