重定向在Spring控制器中不起作用。

huangapple go评论72阅读模式
英文:

redirect doesn't work in the Spring controller

问题

这里有一个注册字段,在注册后应该转到菜单页面,但实际上它跳转到了主页面。

@Controller
@RequestMapping("/main")
public class SignInPageController {

    private UserDao userDao;

    @Autowired
    public SignInPageController(UserDao userDao){
        this.userDao = userDao;
    }

    @GetMapping("/menu")
    public String mainMenuPage(){
        return "main/menu";
    }

    @GetMapping("/signup")
    public String signUpPage(Model model){
        model.addAttribute("newuser", new User());
        return "main/signup";
    }

    @PostMapping("/signup")
    public String signUp(@RequestParam("name") String name,
                         @RequestParam("password") String password,
                         @RequestParam("email") String email,
                         @RequestParam("nativeLanguage") String nativeLanguage){
        User user = new User(name, password, email, nativeLanguage);
        userDao.signUpUser(user);
        return "redirect:/main/menu";
    }
}

请问问题出在哪里?

英文:

Hhere is a registration field, after registration it should go to the menu page, but it goes to main

@Controller
@RequestMapping("/main")
public class SignInPageController {

    private UserDao userDao;
@Autowired
    public SignInPageController(UserDao userDao){this.userDao = userDao;}

@GetMapping("/menu")
    public String mainMenuPage(){
        return "main/menu";
    }
@GetMapping("/signup")
    public String signUpPage(Model model){
        model.addAttribute("newuser", new User());
        return "main/signup";
    }
@PostMapping("/signup")
    public String signUp(@RequestParam("name") String name,
                             @RequestParam("password") String password,
                             @RequestParam("email") String email,
                             @RequestParam("nativeLanguage") String nativeLanguage){
        User user = new User(name, password, email, nativeLanguage);
        userDao.signUpUser(user);
        return "redirect:/main/menu";
    }

can you tell me what the problem is?

答案1

得分: 0

看起来您的 SignInPageController 类上缺少了 @Controller Spring 注解。

所以您应该得到类似这样的代码:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
// 其他导入语句...

@Controller
@RequestMapping("/main")
public class SignInPageController {

    private UserDao userDao;

    @Autowired
    public SignInPageController(UserDao userDao) {
        this.userDao = userDao;
    }

    @GetMapping("/menu")
    public String mainMenuPage() {
        return "main/menu";
    }

    @GetMapping("/signup")
    public String signUpPage(Model model) {
        model.addAttribute("newuser", new User());
        return "main/signup";
    }

    @PostMapping("/signup")
    public String signUp(@RequestParam("name") String name,
                         @RequestParam("password") String password,
                         @RequestParam("email") String email,
                         @RequestParam("nativeLanguage") String nativeLanguage) {
        User user = new User(name, password, email, nativeLanguage);
        userDao.signUpUser(user);
        return "redirect:/main/menu";
    }
}
英文:

It looks like you are missing the @Controller Spring annotation on your SignInPageController class.

So you should get something like this:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
// And your other imports...

@Controller
@RequestMapping("/main")
public class SignInPageController {

    private UserDao userDao;

    @Autowired
    public SignInPageController(UserDao userDao){this.userDao = userDao;}

    @GetMapping("/menu")
    public String mainMenuPage(){
        return "main/menu";
    }

    @GetMapping("/signup")
    public String signUpPage(Model model){
        model.addAttribute("newuser", new User());
        return "main/signup";
    }

    @PostMapping("/signup")
    public String signUp(@RequestParam("name") String name,
                             @RequestParam("password") String password,
                             @RequestParam("email") String email,
                             @RequestParam("nativeLanguage") String nativeLanguage){
        User user = new User(name, password, email, nativeLanguage);
        userDao.signUpUser(user);
        return "redirect:/main/menu";
    }
}

答案2

得分: 0

按照我在评论中所说的:

你应该从控制器的方法中返回一个ModelAndView类型,返回语句如下:

return new ModelAndView("redirect:/main/menu", model);

正如你提到的,你还遇到了一个错误的重定向问题,尝试使用预定义的常量来避免拼写错误 重定向在Spring控制器中不起作用。

如果你对Spring不太熟悉,确保查阅RedirectAttributes,添加flashAttributes,并查阅Baeldung的文档,非常详尽。

祝你好运!

英文:

So as I was saying in the comments :

You should be returning a ModelAndView type from your controller's method, with a return such as :

return new ModelAndView("redirect:/main/menu", model);

As you mentionned you also encoutered an issue with a incorrect redirection, try to use defined CONSTANTS to avoid typos 重定向在Spring控制器中不起作用。

If you're new to Spring, make sure to look into RedirectAttributes, adding flashAttributes, and check the documentation on Baeldung it is really complete.

Good luck !

huangapple
  • 本文由 发表于 2020年10月2日 02:58:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64161670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定