繁体   English   中英

Spring MVC 3.1重定向

[英]Spring MVC 3.1 Redirect

我正在尝试将Spring作为新手使用,并遇到以下问题。 我尝试通过表单进行重定向。 但是,当我单击重定向提交按钮时,什么都没有发生,日志中没有错误消息,并且页面停留在那里。 可用的答案之一建议我添加新的ViewResolver,但是我遇到的问题仍然存在。 重定向后还添加了一个/。 index.jsp和final.jsp在WEB-INF文件夹中。 正确显示带有引导启动模板的index.jsp。 但是单击重定向按钮没有任何作用。 我究竟做错了什么。 谢谢。

package com.telenal.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class BaseController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome(ModelMap model) {

    model.addAttribute("message",
            "Maven Web Project + Spring 3 MVC - welcome()");
    System.out.println("returning index1");
    // Spring uses InternalResourceViewResolver and return back index.jsp
    return "index1";

}

@RequestMapping(value = "/redirect", method = RequestMethod.POST)
public String welcome1(ModelMap model) {

    model.addAttribute("message",
            "Maven Web Project + Spring 3 MVC - welcome()");

    // Spring uses InternalResourceViewResolver and return back index.jsp
    return "redirect:/final";

}

@RequestMapping(value = "/welcome/{name}", method = RequestMethod.GET)
public String welcomeName(@PathVariable String name, ModelMap model) {

    model.addAttribute("message", "Maven Web Project + Spring 3 MVC - "
            + name);
    return "index";

}

@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String welcomeName1(@PathVariable String name, ModelMap model) {

    model.addAttribute("message", "Maven Web Project + Spring 3 MVC - "
            + name);
    return "redirect:index";

}
}

我的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      version="2.5">    

<display-name>Counter Web Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:appContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>

    <url-pattern>/</url-pattern>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.png</url-pattern>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
</web-app>

我的index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">

<title>Starter Template for Bootstrap</title>

<!-- Bootstrap core CSS -->
<link href="dist/css/bootstrap.min.css" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="stylish-portfolio.css" rel="stylesheet">

<!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
<!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
<script src="../../assets/js/ie-emulation-modes-warning.js"></script>

<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>

<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse"
                data-target=".navbar-collapse">
                <span class="sr-only">Toggle navigation</span> <span
                    class="icon-bar"></span> <span class="icon-bar"></span> <span
                    class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Project name</a>
        </div>
        <div class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#about">About</a></li>
                <li><a href="#contact">Contact</a></li>
            </ul>
        </div>
        <!--/.nav-collapse -->
    </div>
</div>

<div class="container">
    <br>
    <br>

    <div class="starter-template">
        <h1>Bootstrap starter template</h1>
        <p class="lead">
            Use this document as a way to quickly start any new   project.<br>
            All you get is this text and a mostly barebones HTML document.
        </p>
    </div>

</div>
<!-- /.container -->
<h2>Spring Page Redirection</h2>
<p>Click below button to redirect the result to new page</p>
<form:form method="GET" action="/Guestbook/redirect">
<table>
<tr>
  <td>
   <input type="submit" value="Redirectt Page"/>
  </td>
 </tr>
</table>  
</form:form>


<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">

</script>
<script src="dist/js/bootstrap.min.js">

</script>
</body>
</html>

我的servelet.xml

    <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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">


    <mvc:default-servlet-handler/>
    <context:component-scan base-package="com.telenal.controller" />

    <bean

        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property> 
    </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
    <property name="basenames" value="views" />
    <property name="order" value="1" />
</bean>   
<!-- UrlBasedViewResolver to Handle Redirects & Forward -->
<bean id="urlViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
    <property name="order" value="2" />
</bean>       
</beans>

您提交的html类型为GET ,但您的控制器期望POST

修复您的HTML表单:

<form:form method="POST" action="/Guestbook/redirect">

您的调度程序Servlet映射看起来很奇怪,在映射/时不需要此*.xxx


另一件事是/Guestbook部分。 我想知道,因为您的控制器已映射到上下文根( @Controller @RequestMapping("/") )。

因此,我建议使用c:url 它将应用程序上下文URL放在前面,因此您只需要在应用程序上下文中指定URL。

<c:url value="/redirect" var="form_url" />
<form action="${form_url}" method="POST">....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM