简体   繁体   English

如何修复我的控制器在 spring mvc 中不起作用?

[英]How do i fix my controller not working in spring mvc?

I'm new to Spring MVC.我是 Spring MVC 的新手。 I want to get the logged in user's info by using session but why does my code return nothing in the view?我想通过使用会话获取登录用户的信息,但为什么我的代码在视图中没有返回任何内容? There's no error, the other views are fine.没有错误,其他视图都很好。 So maybe my controller is not working.所以也许我的控制器不工作。 Can somebody tell me what's wrong about the code?有人可以告诉我代码有什么问题吗? Here is the controller这是控制器

@RequestMapping(value = "manager/main/{userId}", params = "edit", method = RequestMethod.GET)
    public String getLoginUser (HttpSession ss, ModelMap model) {   
        
        if (ss.getAttribute("user") != null) {
            User tk = (User) ss.getAttribute("user");
            model.addAttribute("userInfo", tk);
            model.addAttribute("isOpenModalEditUser", true);
        }
        return "manage/manager/main";
    }

Here is my jsp这是我的jsp

<a href="manager/main/${userInfo.getUserId()}?edit" class="dropdown-item" data-toggle="modal"
data-target="#modal-lg">${userInfo.getEmployee().getFName()} ${userInfo.getEmployee().getLName()}</a>

Your Java code is right, but first you need to make sure that there is 'user' in the session, probably when logging in, the session is not being saved.您的 Java 代码是正确的,但首先您需要确保会话中有“用户”,可能在登录时,会话没有被保存。 when logging in, you need to set the 'user', like:登录时,您需要设置“用户”,例如:

ss.setAttribute("user", user);

after certifying that the backend is correct, then go to the frontend corrections, if necessary.确认后端正确后,如有必要,再去前端更正。

but according to my tests, the backend is ok, just need to make sure that the 'user' is being saved in the session.但根据我的测试,后端没问题,只需要确保“用户”被保存在会话中。

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

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