简体   繁体   English

在从jsp重定向上调用Spring控制器方法

[英]Invoke Spring controller method on redirect FROM jsp

So let assume I have 2 jsp files: login.jsp and menu.jsp 因此,假设我有2个jsp文件:login.jsp和menu.jsp

Also I have two methods in the controller class: 我在控制器类中也有两个方法:

@RequestMapping("/login")
    public void login() {
        return "login";
    }


@RequestMapping("/menu")
    public void menu() {
        List<User> allUsers = userJpaRepository.findAll();
    return new ModelAndView("allUsers", "users", allUsers);
    }

In login.jsp login.jsp中

<%@page import="com.markovski.database.DataBase"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<form method="get" action="menu.jsp">
<% 
    String accname = request.getParameter("uname");
    String password = request.getParameter("pass");

    if(DataBase.checkForUserCreditinals(accname, password)){
        session.setAttribute("userId", 2);

        response.sendRedirect("menu.jsp");
    } else {
        //out.println("Error");
    }

%>
</form>

So what do I want ? 那我想要什么? When I log in I would like to be redirected to menu page, but it does not invoke menu(), which will pass all users to my menu.jsp. 当我登录时,我希望重定向到菜单页面,但它不会调用menu(),该菜单会将所有用户传递给我的menu.jsp。 So it is just opening an empty /menu page. 因此,它只是打开一个空的/菜单页面。 How can I redirect to menu.jsp AND INVOKE the controller method, which will give me the data that I want to display ? 如何重定向到menu.jsp并调用controller方法,该方法将为我提供要显示的数据?

As you are coding in Spring, I think we can stick to it than mixing jsp and Spring style. 当您在Spring中进行编码时,我认为我们可以坚持使用它,而不是将jsp和Spring样式混合使用。 Why don't you write a mapping for menu url and return menu.jsp from it. 为什么不为菜单URL编写映射并从中返回menu.jsp。 Also you can keep your database code as well there ( though not a good practice). 另外,您也可以将数据库代码保存在此处(尽管不是一个好习惯)。 You may also need to handle the login submissions as well. 您可能还需要处理登录提交。

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

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