在Spring MVC中重定向期间传递模型属性,并在URL中避免相同 [英] Passing model attribute during redirect in spring MVC and avoiding the same in URL

查看:182
本文介绍了在Spring MVC中重定向期间传递模型属性,并在URL中避免相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring的新手
我也在stackoverflow中搜索了网页和相关帖子。我没有找到我真正需要的那个。

我的目标是在重定向期间将模型属性从控制器传递到jsp页面,并避免在URL中显示属性。
以下是验证使用jdo从数据存储区登录。

I am a newbie to Spring
I have searched web and related posts in stackoverflow too. I didn't find the one that i actually need.
My objective is to pass model attributes from controller to jsp page during a redirect and avoid the attribute being displayed in URL.
The below is validating login from datastore using jdo.

我的控制器:

 @Controller
 public class LoginController {
int count;
PersistenceManager pm = PMF.get().getPersistenceManager();

//Instance of data class
    User user;
ModelAndView modelAndView=new ModelAndView();

@RequestMapping(value="/Login",method = RequestMethod.POST)
public ModelAndView loginValidate(HttpServletRequest req){

         //Getting login values
    String uname=req.getParameter("nameLogin");
    String pswd1=req.getParameter("pswdLogin");
    count=0;


    user=new User();

            //Generating Query
    Query q = pm.newQuery(User.class);
    q.setFilter("userName == userNameParam");
    q.declareParameters("String userNameParam");

    try{
        List<User> results = (List<User>) q.execute(uname);  
        for (User u: results) {

            String userName=u.getUserName();

                if(userName.equals(uname)){

                    System.out.println(u.getPassword());

                    if(u.getPassword().equals(pswd1)){
                        count=count+1;
                            modelAndView.setViewName("redirect:welcome");
                        modelAndView.addObject("USERNAME",uname);
                        return modelAndView;

                    }
     //rest of the logic 
  }

JSP:

  <h1>Welcome ${USERNAME} </h1>

我的网址是/ welcome?USERNAME = robin

我的目标是显示它作为/ welcome

此外,我的页面应该显示Welcome robin,而它只显示Welcome。请让我知道如何解决它。

提前致谢!

My URL is /welcome?USERNAME=robin
My objective is to show it as /welcome
Also, my page is supposed to display "Welcome robin" whereas it displays only Welcome. Please let me know how can i resolve it.
Thanks in advance!

推荐答案

您应该使用 flash属性工具Spring MVC。如果希望在重定向后可以访问数据,则不要将其添加到模型中(模型 ModelMap ModelAndView )但是你在控制器方法中作为参数得到的 RedirectAttributes

You should use the flash attribute facility of Spring MVC. When you want data to be accessible after a redirect, you do not add it to the model (Model, ModelMap or ModelAndView) but to a RedirectAttributes that you get as parameter in your controller method.

@RequestMapping(value="/Login",method = RequestMethod.POST)
public ModelAndView loginValidate(HttpServletRequest req, RedirectAttributes redir){
...

    modelAndView.setViewName("redirect:welcome");
    redir.addFlashAttribute("USERNAME",uname);
    return modelAndView;
}

这些 flash属性通过会话传递(并在使用后立即销毁 - 有关详细信息,请参阅Spring参考手册。这有两个兴趣:

Those flash attributes are passed via the session (and are destroyed immediately after being used - see Spring Reference Manual for details). This has two interests :


  • 它们在URL中不可见

  • 您不限于String ,但可以传递任意对象。

这篇关于在Spring MVC中重定向期间传递模型属性,并在URL中避免相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆