为什么我的 spring mvc 视图不会加载?

Why won't my spring mvc views load?

我是 spring mvc 的新手。我目前正在使用 netbeans 8.0.2。以及一个spring mvc插件。从我在项目的 \\'libraries\\' 部分下看到的 jar 文件中,我似乎使用的是 spring mvc 4.0.1。

我可以从 tomcat 服务器甚至 glassfish 加载 index.jsp 页面,但如果我应该在目录结构 WEB-INFO>jsp>response.jsp 下创建另一个 .jsp 文件作为示例。我从服务器收到错误 404。这可能是一个可笑的简单修复,但到目前为止没有成功。

我怀疑这与我的控制器和 [springapp]-servlet.xml 文件有关。我在网上看到的大多数示例都使用 spring mvc 3.x 并避免在控制器设置中使用注释方法,乍一看后者对我来说似乎更容易。然后这些示例继续向上述 xml 文件添加几行。但是,我认为通过使用注释控制器设置,spring mvc 会自动检测我的控制器。不推荐使用注解方式吗?

我的配置文件如下:

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>springapp</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <load-on-startup>2</load-on-startup>



  </servlet>

  <servlet-mapping>

    <servlet-name>springapp</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>

<?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/mvc 

              http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

              http://www.springframework.org/schema/context 

              http://www.springframework.org/schema/context/spring-context-40.xsd

              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">





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

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package controller;



import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

//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.bind.annotation.RequestParam;



/**

*

* @author macj7

*/



@Controller

public class HelloController {





  @RequestMapping( value ="/response", method = RequestMethod.GET)

  public String index( Model model) {

    String name ="World";

    model.addAttribute("name", name );

    return"response";

  }



}

<%-- 

  Document  : response

  Created on : Jul 31, 2015, 1:25:19 PM

  Author   : macj7

--%>



<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    Spring Web MVC 

  </head>

  <body>

    th:text="Hello, ${name}!" 

  </body>

</html>

<%--

Views should be stored under the WEB-INF folder so that

they are not accessible except through controller process.



This JSP is here to provide a redirect to the dispatcher

servlet but should be the only JSP outside of WEB-INF.

--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

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

<welcome-file-list>

  <welcome-file>/response</welcome-file>

</welcome-file-list>

<load-on-startup>1</load-on-startup>

<servlet-mapping>

  <servlet-name>springapp</servlet-name>

  <url-pattern>/*</url-pattern>

</servlet-mapping>

springapp-servlet.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>springapp</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <load-on-startup>2</load-on-startup>



  </servlet>

  <servlet-mapping>

    <servlet-name>springapp</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>

<?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/mvc 

              http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

              http://www.springframework.org/schema/context 

              http://www.springframework.org/schema/context/spring-context-40.xsd

              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">





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

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package controller;



import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

//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.bind.annotation.RequestParam;



/**

*

* @author macj7

*/



@Controller

public class HelloController {





  @RequestMapping( value ="/response", method = RequestMethod.GET)

  public String index( Model model) {

    String name ="World";

    model.addAttribute("name", name );

    return"response";

  }



}

<%-- 

  Document  : response

  Created on : Jul 31, 2015, 1:25:19 PM

  Author   : macj7

--%>



<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    Spring Web MVC 

  </head>

  <body>

    th:text="Hello, ${name}!" 

  </body>

</html>

<%--

Views should be stored under the WEB-INF folder so that

they are not accessible except through controller process.



This JSP is here to provide a redirect to the dispatcher

servlet but should be the only JSP outside of WEB-INF.

--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

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

<welcome-file-list>

  <welcome-file>/response</welcome-file>

</welcome-file-list>

<load-on-startup>1</load-on-startup>

<servlet-mapping>

  <servlet-name>springapp</servlet-name>

  <url-pattern>/*</url-pattern>

</servlet-mapping>

其他相关文件:

控制器:

<?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>springapp</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <load-on-startup>2</load-on-startup>



  </servlet>

  <servlet-mapping>

    <servlet-name>springapp</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>

<?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/mvc 

              http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

              http://www.springframework.org/schema/context 

              http://www.springframework.org/schema/context/spring-context-40.xsd

              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">





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

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package controller;



import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

//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.bind.annotation.RequestParam;



/**

*

* @author macj7

*/



@Controller

public class HelloController {





  @RequestMapping( value ="/response", method = RequestMethod.GET)

  public String index( Model model) {

    String name ="World";

    model.addAttribute("name", name );

    return"response";

  }



}

<%-- 

  Document  : response

  Created on : Jul 31, 2015, 1:25:19 PM

  Author   : macj7

--%>



<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    Spring Web MVC 

  </head>

  <body>

    th:text="Hello, ${name}!" 

  </body>

</html>

<%--

Views should be stored under the WEB-INF folder so that

they are not accessible except through controller process.



This JSP is here to provide a redirect to the dispatcher

servlet but should be the only JSP outside of WEB-INF.

--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

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

<welcome-file-list>

  <welcome-file>/response</welcome-file>

</welcome-file-list>

<load-on-startup>1</load-on-startup>

<servlet-mapping>

  <servlet-name>springapp</servlet-name>

  <url-pattern>/*</url-pattern>

</servlet-mapping>

response.jsp:

<?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>springapp</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <load-on-startup>2</load-on-startup>



  </servlet>

  <servlet-mapping>

    <servlet-name>springapp</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>

<?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/mvc 

              http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

              http://www.springframework.org/schema/context 

              http://www.springframework.org/schema/context/spring-context-40.xsd

              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">





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

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package controller;



import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

//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.bind.annotation.RequestParam;



/**

*

* @author macj7

*/



@Controller

public class HelloController {





  @RequestMapping( value ="/response", method = RequestMethod.GET)

  public String index( Model model) {

    String name ="World";

    model.addAttribute("name", name );

    return"response";

  }



}

<%-- 

  Document  : response

  Created on : Jul 31, 2015, 1:25:19 PM

  Author   : macj7

--%>



<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    Spring Web MVC 

  </head>

  <body>

    th:text="Hello, ${name}!" 

  </body>

</html>

<%--

Views should be stored under the WEB-INF folder so that

they are not accessible except through controller process.



This JSP is here to provide a redirect to the dispatcher

servlet but should be the only JSP outside of WEB-INF.

--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

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

<welcome-file-list>

  <welcome-file>/response</welcome-file>

</welcome-file-list>

<load-on-startup>1</load-on-startup>

<servlet-mapping>

  <servlet-name>springapp</servlet-name>

  <url-pattern>/*</url-pattern>

</servlet-mapping>

redirect.jsp:

<?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>springapp</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <load-on-startup>2</load-on-startup>



  </servlet>

  <servlet-mapping>

    <servlet-name>springapp</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>

<?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/mvc 

              http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

              http://www.springframework.org/schema/context 

              http://www.springframework.org/schema/context/spring-context-40.xsd

              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">





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

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package controller;



import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

//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.bind.annotation.RequestParam;



/**

*

* @author macj7

*/



@Controller

public class HelloController {





  @RequestMapping( value ="/response", method = RequestMethod.GET)

  public String index( Model model) {

    String name ="World";

    model.addAttribute("name", name );

    return"response";

  }



}

<%-- 

  Document  : response

  Created on : Jul 31, 2015, 1:25:19 PM

  Author   : macj7

--%>



<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    Spring Web MVC 

  </head>

  <body>

    th:text="Hello, ${name}!" 

  </body>

</html>

<%--

Views should be stored under the WEB-INF folder so that

they are not accessible except through controller process.



This JSP is here to provide a redirect to the dispatcher

servlet but should be the only JSP outside of WEB-INF.

--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

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

<welcome-file-list>

  <welcome-file>/response</welcome-file>

</welcome-file-list>

<load-on-startup>1</load-on-startup>

<servlet-mapping>

  <servlet-name>springapp</servlet-name>

  <url-pattern>/*</url-pattern>

</servlet-mapping>

如果有此问题的解决方案或更多信息

需要请告诉我。我也想解释任何答案

在我尝试学习时给出,而不仅仅是复制和粘贴解决方案。

这是我第一次尝试使用 java 创建 Web 应用程序,我必须说仅配置似乎相当令人生畏。

提前感谢您的时间和耐心。


好的,所以我找到的解决方案是将在 springapp-servlet.xml 中处理请求的控制器定义为 bean,并在控制器本身中重新定义该方法,以便它返回一个 ModelAndView (import org. springframework.web.servlet.ModelAndView;)。

我的页面现在呈现。

我应该注意到我已经重写了模型,但没有使用注释形式来编写控制器,但我很快也会以这种形式对其进行测试。现在我只是简单地实现了控制器接口。

感谢所有回复的人。感谢您的意见。


我认为在 web.xml 文件的欢迎文件列表中,您应该提供 Spring 控制器请求映射而不是 jsp 文件名,如下所示。

<?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>springapp</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <load-on-startup>2</load-on-startup>



  </servlet>

  <servlet-mapping>

    <servlet-name>springapp</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>

<?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/mvc 

              http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

              http://www.springframework.org/schema/context 

              http://www.springframework.org/schema/context/spring-context-40.xsd

              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">





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

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package controller;



import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

//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.bind.annotation.RequestParam;



/**

*

* @author macj7

*/



@Controller

public class HelloController {





  @RequestMapping( value ="/response", method = RequestMethod.GET)

  public String index( Model model) {

    String name ="World";

    model.addAttribute("name", name );

    return"response";

  }



}

<%-- 

  Document  : response

  Created on : Jul 31, 2015, 1:25:19 PM

  Author   : macj7

--%>



<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    Spring Web MVC 

  </head>

  <body>

    th:text="Hello, ${name}!" 

  </body>

</html>

<%--

Views should be stored under the WEB-INF folder so that

they are not accessible except through controller process.



This JSP is here to provide a redirect to the dispatcher

servlet but should be the only JSP outside of WEB-INF.

--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

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

<welcome-file-list>

  <welcome-file>/response</welcome-file>

</welcome-file-list>

<load-on-startup>1</load-on-startup>

<servlet-mapping>

  <servlet-name>springapp</servlet-name>

  <url-pattern>/*</url-pattern>

</servlet-mapping>

根本原因:

  • 根据 web.xmlDispatcherServlet 查找以 *.htm 结尾的 url,它不接受对 response.jsp 的请求
  • 注意:

    在启动时加载 DispatcherServlet 作为优先级 1

    <?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>springapp</servlet-name>
    
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    
        <load-on-startup>2</load-on-startup>
    
    
    
      </servlet>
    
      <servlet-mapping>
    
        <servlet-name>springapp</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>
    
    <?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/mvc 
    
                  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    
                  http://www.springframework.org/schema/context 
    
                  http://www.springframework.org/schema/context/spring-context-40.xsd
    
                  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">
    
    
    
    
    
      <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>
    
    /*
    
    * To change this license header, choose License Headers in Project Properties.
    
    * To change this template file, choose Tools | Templates
    
    * and open the template in the editor.
    
    */
    
    package controller;
    
    
    
    import org.springframework.stereotype.Controller;
    
    import org.springframework.ui.Model;
    
    //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.bind.annotation.RequestParam;
    
    
    
    /**
    
    *
    
    * @author macj7
    
    */
    
    
    
    @Controller
    
    public class HelloController {
    
    
    
    
    
      @RequestMapping( value ="/response", method = RequestMethod.GET)
    
      public String index( Model model) {
    
        String name ="World";
    
        model.addAttribute("name", name );
    
        return"response";
    
      }
    
    
    
    }
    
    <%-- 
    
      Document  : response
    
      Created on : Jul 31, 2015, 1:25:19 PM
    
      Author   : macj7
    
    --%>
    
    
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    
    <!DOCTYPE html>
    
    <html>
    
      <head>
    
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
        Spring Web MVC 
    
      </head>
    
      <body>
    
        th:text="Hello, ${name}!" 
    
      </body>
    
    </html>
    
    <%--
    
    Views should be stored under the WEB-INF folder so that
    
    they are not accessible except through controller process.
    
    
    
    This JSP is here to provide a redirect to the dispatcher
    
    servlet but should be the only JSP outside of WEB-INF.
    
    --%>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    
    <% response.sendRedirect("index.htm"); %>
    
    <welcome-file-list>
    
      <welcome-file>/response</welcome-file>
    
    </welcome-file-list>
    
    <load-on-startup>1</load-on-startup>
    
    <servlet-mapping>
    
      <servlet-name>springapp</servlet-name>
    
      <url-pattern>/*</url-pattern>
    
    </servlet-mapping>

    解决方案:

    更改将检查每个请求的 DispatcherServlet 的映射。

    <?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>springapp</servlet-name>
    
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    
        <load-on-startup>2</load-on-startup>
    
    
    
      </servlet>
    
      <servlet-mapping>
    
        <servlet-name>springapp</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>
    
    <?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/mvc 
    
                  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    
                  http://www.springframework.org/schema/context 
    
                  http://www.springframework.org/schema/context/spring-context-40.xsd
    
                  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">
    
    
    
    
    
      <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>
    
    /*
    
    * To change this license header, choose License Headers in Project Properties.
    
    * To change this template file, choose Tools | Templates
    
    * and open the template in the editor.
    
    */
    
    package controller;
    
    
    
    import org.springframework.stereotype.Controller;
    
    import org.springframework.ui.Model;
    
    //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.bind.annotation.RequestParam;
    
    
    
    /**
    
    *
    
    * @author macj7
    
    */
    
    
    
    @Controller
    
    public class HelloController {
    
    
    
    
    
      @RequestMapping( value ="/response", method = RequestMethod.GET)
    
      public String index( Model model) {
    
        String name ="World";
    
        model.addAttribute("name", name );
    
        return"response";
    
      }
    
    
    
    }
    
    <%-- 
    
      Document  : response
    
      Created on : Jul 31, 2015, 1:25:19 PM
    
      Author   : macj7
    
    --%>
    
    
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    
    <!DOCTYPE html>
    
    <html>
    
      <head>
    
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
        Spring Web MVC 
    
      </head>
    
      <body>
    
        th:text="Hello, ${name}!" 
    
      </body>
    
    </html>
    
    <%--
    
    Views should be stored under the WEB-INF folder so that
    
    they are not accessible except through controller process.
    
    
    
    This JSP is here to provide a redirect to the dispatcher
    
    servlet but should be the only JSP outside of WEB-INF.
    
    --%>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    
    <% response.sendRedirect("index.htm"); %>
    
    <welcome-file-list>
    
      <welcome-file>/response</welcome-file>
    
    </welcome-file-list>
    
    <load-on-startup>1</load-on-startup>
    
    <servlet-mapping>
    
      <servlet-name>springapp</servlet-name>
    
      <url-pattern>/*</url-pattern>
    
    </servlet-mapping>

    要访问 response.jsp,您需要输入根据控制器中定义的 RequestMapping 映射的 url www.domain/context-root/response

    阅读更多关于 Web MVC 框架 - DispatcherServlet


相关推荐

  • Spring部署设置openshift

    Springdeploymentsettingsopenshift我有一个问题让我抓狂了三天。我根据OpenShift帐户上的教程部署了spring-eap6-quickstart代码。我已配置调试选项,并且已将Eclipse工作区与OpehShift服务器同步-服务器上的一切工作正常,但在Eclipse中出现无法消除的错误。我有这个错误:cvc-complex-type.2.4.a:Invali…
    2025-04-161
  • 检查Java中正则表达式中模式的第n次出现

    CheckfornthoccurrenceofpatterninregularexpressioninJava本问题已经有最佳答案,请猛点这里访问。我想使用Java正则表达式检查输入字符串中特定模式的第n次出现。你能建议怎么做吗?这应该可以工作:MatchResultfindNthOccurance(intn,Patternp,CharSequencesrc){Matcherm=p.matcher…
    2025-04-161
  • 如何让 JTable 停留在已编辑的单元格上

    HowtohaveJTablestayingontheeditedcell如果有人编辑JTable的单元格内容并按Enter,则内容会被修改并且表格选择会移动到下一行。是否可以禁止JTable在单元格编辑后转到下一行?原因是我的程序使用ListSelectionListener在单元格选择上同步了其他一些小部件,并且我不想在编辑当前单元格后选择下一行。Enter的默认绑定是名为selectNext…
    2025-04-161
  • Weblogic 12c 部署

    Weblogic12cdeploy我正在尝试将我的应用程序从Tomcat迁移到Weblogic12.2.1.3.0。我能够毫无错误地部署应用程序,但我遇到了与持久性提供程序相关的运行时错误。这是堆栈跟踪:javax.validation.ValidationException:CalltoTraversableResolver.isReachable()threwanexceptionatorg.…
    2025-04-161
  • Resteasy Content-Type 默认值

    ResteasyContent-Typedefaults我正在使用Resteasy编写一个可以返回JSON和XML的应用程序,但可以选择默认为XML。这是我的方法:@GET@Path("/content")@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})publicStringcontentListRequestXm…
    2025-04-161
  • 代码不会停止运行,在 Java 中

    thecodedoesn'tstoprunning,inJava我正在用Java解决项目Euler中的问题10,即"Thesumoftheprimesbelow10is2+3+5+7=17.Findthesumofalltheprimesbelowtwomillion."我的代码是packageprojecteuler_1;importjava.math.BigInteger;importjava…
    2025-04-161
  • Out of memory java heap space

    Outofmemoryjavaheapspace我正在尝试将大量文件从服务器发送到多个客户端。当我尝试发送大小为700mb的文件时,它显示了"OutOfMemoryjavaheapspace"错误。我正在使用Netbeans7.1.2版本。我还在属性中尝试了VMoption。但仍然发生同样的错误。我认为阅读整个文件存在一些问题。下面的代码最多可用于300mb。请给我一些建议。提前致谢publicc…
    2025-04-161
  • Log4j 记录到共享日志文件

    Log4jLoggingtoaSharedLogFile有没有办法将log4j日志记录事件写入也被其他应用程序写入的日志文件。其他应用程序可以是非Java应用程序。有什么缺点?锁定问题?格式化?Log4j有一个SocketAppender,它将向服务发送事件,您可以自己实现或使用与Log4j捆绑的简单实现。它还支持syslogd和Windows事件日志,这对于尝试将日志输出与来自非Java应用程序…
    2025-04-161