英文:
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不太熟悉,确保查阅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
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 !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论