简体   繁体   English

如何在Spring MVC控制器内传递查询?

[英]how do I pass a query within a spring mvc controller?

assume you have something like this in normal servlet without using spring mvc: 假设您在普通的servlet中没有使用spring mvc就拥有这样的东西:

if(username == "X" && password =="Y"){
  response.sendRedirect("./user/welcomePage.jsp");
}else{
  response.sendRedirect("./login.jsp?wrongPass=true");
 }

and if the username && password is wrong, after redirection to login.jsp page, inform user that the username or password is wrong, somewhere in the login.jsp page in red color: 如果用户名&&密码错误,请在重定向到login.jsp页面后,在用户名和密码错误的情况下,在login.jsp页面中的红色部分告知用户:

<p style="color:red";
 <%= (request.getParameter("wrongPass")==null)? "visibility:hidden":"" %>;">
    Your username/password is wrong !</p>

My question is, is there any way to pass an argument like: 我的问题是,有什么办法可以传递这样的参数:

response.sendRedirect("./login.jsp?wrongPass=true");

inside Spring MVC controller ? 在Spring MVC控制器内部?

coz if we have a controller like this: 如果我们有一个像这样的控制器:

@Controller
@RequestMapping(value ="/")
public class Welcome{
public String welcome(@RequestParam("j_username") String username, 
        @RequestParam("j_password") String password){
       .
       .
       .
   return "login";

as log as we need to return a String to form a page & because we have InternalResourceViewResolver defined: 作为日志,因为我们需要返回一个String来形成页面&,因为我们定义了InternalResourceViewResolver:

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

if we pass the argument in our controller like: return "login?wrongPass=true" after using InternalResourceViewResolver that will create something like : 如果我们在控制器中传递参数,例如:使用InternalResourceViewResolver后返回“ login?wrongPass = true”,则会创建类似以下内容:

login?wrongPass=true.jsp   Which is totally wrong ...

now, what is your solution to solve this problem (without) adding that String to model inside our controller & fetch the parameter inside JSP ? 现在,解决这个问题的解决方案是(不)在我们的控制器内部添加String并在JSP中获取参数的解决方案? I want the browser url become something like: 我希望浏览器网址变成类似以下内容:

http://localhost:8080/myProject/login.jsp?wrongPass=true

You can use the redirect prefix and then you can use absolute URL 您可以使用重定向前缀,然后可以使用绝对URL

return "redirect:http://localhost:8080/myProject/login.jsp?wrongPass=true";

You can build the absolute url dynamically also using the methods of HttpServletRequest 您也可以使用HttpServletRequest的方法动态地构建绝对URL。

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

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