简体   繁体   English

Spring MVC在重定向到默认(欢迎)页面时传递数据-模型不见了

[英]Spring MVC passing data when redirecting to default (welcome) page - model gone

I'm trying to get this done using Spring MVC: my welcome page is index.jsp located right in my /webapp folder so it can be set as welcome page. 我正在尝试使用Spring MVC做到这一点:我的欢迎页面是index.jsp,位于我的/ webapp文件夹中,因此可以将其设置为欢迎页面。 This page has a login form. 此页面具有登录表单。 When the login fails, I'd like to redirect to this welcome page ( redirect:/ ) and pass an error message which can then be shown using JSTL. 登录失败时,我想重定向到此欢迎页面( redirect:/ ),并传递一条错误消息,然后可以使用JSTL显示该错误消息。 The problem is, when I do the redirect the model is gone and the errormessage too. 问题是,当我执行重定向时,模型消失了,错误消息也消失了。 But I can't just return the viewname because the index.jsp is in the webapp folder and my View Resolver is mapped on /WEB-INF/pages/... 但是我不能只返回视图名称,因为index.jsp在webapp文件夹中,并且我的视图解析器映射在/ WEB-INF / pages /上。

Anyone know how I can get this to work? 有人知道我该如何使用它吗? This is what I have so far: 这是我到目前为止的内容:

web.xml web.xml

<web-app 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>TDD oefening</display-name>

    <servlet>
        <servlet-name>springservlet</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/springservlet-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springservlet</servlet-name>
        <url-pattern>/web/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

springservlet.xml springservlet.xml

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

    <context:component-scan base-package="be.kdg.prog4.tdd"/>
    <mvc:annotation-driven/>

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

    <mvc:resources mapping="/resources/**" location="/resources/"/>
</beans>

My controller method for handling the login 我的控制器方法来处理登录

@RequestMapping(value = "/login.do", method = RequestMethod.POST)
    public ModelAndView login(@RequestParam String username, @RequestParam String password) {
        boolean ok = service.checkLogin(username, password);
        ModelAndView mav;
        if (!ok) {
            mav = new ModelAndView("redirect:/");
            mav.addObject("error", "Wrong username or password");
            return mav;
        }

        mav = new ModelAndView("favorites");
        mav.addObject("username", username);
        mav.addObject("isRoot", username.equals("root"));
        return mav;
    }

I would like to display the message after redirect here: 我想在重定向后显示以下消息:

<span id="errormsg" name="errormsg" style="color: red;">${error}</span>

All the necessary taglibs are present at the top of the .jsp page and jstl and cglib and such are loaded in my pom.xml. 所有必需的标签库都位于.jsp页面的顶部,而jstl和cglib等已加载到我的pom.xml中。

Thanks in advance. 提前致谢。

To persist your model after redirecting, you need to use Flash attributes. 要在重定向后保留模型,您需要使用Flash属性。

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-flash-attributes http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-flash-attributes

Here is an example extracted from the JavaDoc: 这是从JavaDoc中提取的示例:

@RequestMapping(value = "/accounts", method = RequestMethod.POST)
 public String handle(Account account, BindingResult result, RedirectAttributes redirectAttrs) {
   if (result.hasErrors()) {
     return "accounts/new";
   }
   // Save account ...
   redirectAttrs.addAttribute("id", account.getId()).addFlashAttribute("message", "Account created!");
   return "redirect:/accounts/{id}";
 }

And the JavaDoc :) 和JavaDoc :)

http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html

EDIT -- To reflect the comment made by @M. 编辑-反映@M的评论。 Deinum Deinum

You could add a simple refresh meta in your index.jsp and add and map a login.jsp page inside your spring application. 您可以在index.jsp中添加一个简单的刷新元数据,并在spring应用程序中添加和映射一个login.jsp页面。 That will make sure that every navigation done by your users will be controlled by the Spring DispatchServlet. 这将确保您的用户完成的所有导航都将由Spring DispatchServlet控制。

A lil snipped to illustrate the idea. 一个小女孩偷偷地说明了这个想法。

index.jsp index.jsp

<meta http-equiv="refresh" content="0; url=/login" />

A request mapping defined somewhere in your controllers 在控制器中某处定义的请求映射

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String doLogin(Model model) {
    // This return sttm needs to reflect the right path to your login.jsp
    return "login";
}

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

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