Java 在我的Spring MVC web应用程序中为HTTP路径映射的不明确处理程序方法
在对我的Java 在我的Spring MVC web应用程序中为HTTP路径映射的不明确处理程序方法,java,xml,spring,hibernate,spring-mvc,Java,Xml,Spring,Hibernate,Spring Mvc,在对我的appconfig security.xml文件进行一些更改后,我出现了此错误 appconfig security.xml ` ` 我正在使用SpringHibernate。请任何人帮我找到可能的错误所在。谢谢 HomeController package com.trygym.solution.web; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stere
appconfig security.xml
文件进行一些更改后,我出现了此错误
appconfig security.xml
`
`
我正在使用SpringHibernate。请任何人帮我找到可能的错误所在。谢谢
HomeController
package com.trygym.solution.web;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
@Controller
class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
private String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
package com.trygym.solution.web;
import com.trygym.solution.model.User;
import com.trygym.solution.service.SecurityService;
import com.trygym.solution.service.UserService;
import com.trygym.solution.validator.UserValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private SecurityService securityService;
@Autowired
private UserValidator userValidator;
@RequestMapping(value = "/forget_password", method = RequestMethod.POST)
public String forget_password(Model model) {
//model.addAttribute("userForm", new User());
return "forget_password";
}
@RequestMapping(value = "/registration", method = RequestMethod.GET)
public String registration(Model model) {
model.addAttribute("userForm", new User());
return "registration";
}
@RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registration(@ModelAttribute("userForm") User userForm, BindingResult bindingResult, Model model) {
userValidator.validate(userForm, bindingResult);
if (bindingResult.hasErrors()) {
return "registration";
}
userService.save(userForm);
securityService.autologin(userForm.getUsername(), userForm.getPasswordConfirm());
return "redirect:/welcome";
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model, String error, String logout) {
if (error != null)
model.addAttribute("error", "Your username and password is invalid.");
if (logout != null)
model.addAttribute("message", "You have been logged out successfully.");
return "login";
}
@RequestMapping(value = {"/", "/welcome"}, method = RequestMethod.GET)
public String welcome(Model model) {
return "welcome";
}
UserController
package com.trygym.solution.web;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
@Controller
class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
private String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
package com.trygym.solution.web;
import com.trygym.solution.model.User;
import com.trygym.solution.service.SecurityService;
import com.trygym.solution.service.UserService;
import com.trygym.solution.validator.UserValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private SecurityService securityService;
@Autowired
private UserValidator userValidator;
@RequestMapping(value = "/forget_password", method = RequestMethod.POST)
public String forget_password(Model model) {
//model.addAttribute("userForm", new User());
return "forget_password";
}
@RequestMapping(value = "/registration", method = RequestMethod.GET)
public String registration(Model model) {
model.addAttribute("userForm", new User());
return "registration";
}
@RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registration(@ModelAttribute("userForm") User userForm, BindingResult bindingResult, Model model) {
userValidator.validate(userForm, bindingResult);
if (bindingResult.hasErrors()) {
return "registration";
}
userService.save(userForm);
securityService.autologin(userForm.getUsername(), userForm.getPasswordConfirm());
return "redirect:/welcome";
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model, String error, String logout) {
if (error != null)
model.addAttribute("error", "Your username and password is invalid.");
if (logout != null)
model.addAttribute("message", "You have been logged out successfully.");
return "login";
}
@RequestMapping(value = {"/", "/welcome"}, method = RequestMethod.GET)
public String welcome(Model model) {
return "welcome";
}
}这里的问题是
HomeController.home
和UserController.welcome
都映射到/
,Spring无法确定访问端点时调用哪个方法。从一个方法中删除映射可以解决此特定问题。您对安全配置所做的更改以及异常的确切消息将有助于此HTTP状态500-请求处理失败;嵌套异常是java.lang.IllegalStateException:映射到HTTP路径“”的不明确处理程序方法:{public java.lang.String com.trygym.solution.web.UserController.welcome(org.springframework.ui.Model),private java.lang.String com.trygym.solution.web.HomeController.home(java.util.Locale,org.springframework.ui.Model)}。这是我得到的错误,之前我在安全配置文件
和中得到了这个错误。在错误消息中,看起来您将这两个方法(欢迎和主页)映射到了路径。我不太确定安全配置会如何影响这一点,虽然这与hibernate无关,但与Spring无关。Spring似乎发现一条路径映射到两个不同的端点,而Spring不知道在访问端点时实际调用哪个方法。在本例中,这似乎是它所在的根路径(“/”
)。您将welcome和home映射到相同的路径和请求方法。这是不受支持的。您真的希望welcome映射到/
?如果是,原因是什么。