我想学习使用Hibernate的NetBeans 8.0.2上的Spring MVC,坦率地说我被困住了。任何帮助表示赞赏。我只是想做一个非常简单的“你好世界”类型的网站。需要关于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”/> so index.htm gets changed到/WEN-INF/jsp/index.jsp并且该页面出现。
2)此时index.jsp被调出。这个文件中唯一的一个是带有提交按钮的表单。目的只是让某人按下按钮,并从屏幕返回来自数据库的信息。我将Hibernate作为此Web项目的一部分,并且基于数据库表“Team”创建了一个Java类“Team.java”。它被填充了几条记录。 目前我所看到的是,一旦index.jsp的出现,我点击提交,这是给一个404
这是形式 显示的网址“主机:端口/ HelloWebFour/index.htm的” 如果我做了一个视图源,它显示为index.jsp。当我点击提交时,它给出了一个404这个URL “host:port/HelloWebFour/team?” btw,“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>
“TeamController。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>