Spring Controller servlet重定向不会发生

时间:2012-11-26 02:53:51

标签: spring

我创建了一个简单的Spring框架,其中controller(springapp dispatcherservlet重定向到视图)但它没有重定向到视图..

我得到了index1.jsp的视图,无法获取Redirect.htm / jsp的视图

这是我的代码..

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->

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

  <servlet>
    <servlet-name>SpringApp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>SpringApp</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>


  <welcome-file-list>
    <welcome-file>
      index1.jsp
    </welcome-file>
  </welcome-file-list>

</web-app>

SpringApp-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!--

1. This file will be used up by the DispatcherServlet and which contains the bean definition
2. The file will be picked up by the specification in the WEB-INF/web.xml using <servlet>spring</servlet>
3. hello controller is responsible for handling the request for the particular page of the website and known 
as the page controller.
-->

<beans xmlns="http://www.springframework.org/schema/beans"
       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-2.5.xsd">

  <!-- the application context definition for the springapp DispatcherServlet -->

  <bean name="Redirect.htm" class="HelloController"/>

</beans>

redirect.jsp中

<%-- 
    Document   : index
    Created on : Nov 23, 2012, 11:55:53 AM
    Author     : gopc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello Controller redirect</title>
    </head>
    <body>
        <h1>This is redirect from the HelloController!</h1>
    </body>
</html>

Redirect.hml

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title>Hello Controller redirect</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <div>This is redirect from the HelloController!</div>
    </body>
</html>

index1.jsp

<%-- 
    Document   : index
    Created on : Nov 23, 2012, 11:55:53 AM
    Author     : gopc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

HelloController.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author gopc
 */
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;

public class HelloController implements Controller {

    protected final Log logger = LogFactory.getLog(getClass());


    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        logger.info("Returning hello view");
        System.out.println("inside controller");
        return new ModelAndView("Redirect.jsp");
    }

}

1 个答案:

答案 0 :(得分:1)

首先,我将回答您为什么将index1.jsp视为视图的问题。它将在您运行应用程序时向您显示index1.jsp的视图,为什么,因为您已在web.xml中的欢迎文件列表中指定了index1.jsp。     您将无法获取Redirect.jsp,因为您未在 SpringApp-servlet.xml 中配置视图解析程序。 将以下bean添加到 SpringApp-servlet.xml

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

在prefix属性中,您必须提供Redirect.jsp文件所在的路径(我假设在/ WEB-INF / jsp文件夹中)。 在后缀属性中,您必须指定要解析的视图的扩展名。

更改声明

return new ModelAndView("Redirect.jsp"); 

return new ModelAndView("Redirect");

运行您的应用程序并使用请求/Redirect.htm

访问您的应用程序

现在应该可以了。希望这会有所帮助。

请务必阅读http://static.springsource.org/spring/docs/2.5.x/reference/view.html

上的弹簧参考手册