java - Spring MVC Web 应用程序中映射到 HTTP 路径的处理程序方法不明确

标签 java xml spring hibernate spring-mvc

appconfig-security.xml 文件进行一些更改后出现此错误

appconfig-security.xml `

<http auto-config="true">
    <intercept-url pattern="/" access="hasAnyRole('ROLE_ADMIN','ROLE_INSTRUCTOR','ROLE_MEMBER')"/>
    <intercept-url pattern="/welcome" access="hasAnyRole('ROLE_ADMIN','ROLE_INSTRUCTOR')"/>
    <intercept-url pattern="/profile" access="hasRole('ROLE_MEMBER')"/>
    <form-login login-page="/login" default-target-url="/welcome" authentication-failure-url="/login?error" username-parameter="username" password-parameter="password"/>
    <logout logout-success-url="/login?logout" />
</http>

<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="userDetailsServiceImpl">
        <password-encoder ref="encoder"></password-encoder>
    </authentication-provider>
</authentication-manager>

<beans:bean id="userDetailsServiceImpl" class="com.trygym.solution.service.UserDetailsServiceImpl"></beans:bean>

<beans:bean id="encoder"
      class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
    <beans:constructor-arg name="strength" value="11"/>
</beans:bean>

`

之前我在数据库中只有一个角色。然后我尝试进行一些安全检查并向用户分配访问权限。然后我就犯了这个错误。我没有对 web.xml 进行任何更改。

web.xml

`

<display-name>Gym Related Web Application</display-name>

<!-- Location of Java @Configuration classes that configure the components that makeup this application -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/appconfig-root.xml</param-value>
</context-param>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

`

我正在使用 Spring Hibernate。请任何人帮助我找到可能出现错误的位置。谢谢。

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";
}

}

用户 Controller

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.homeUserController.welcome 都映射到 / 并且 Spring 无法确定哪个方法访问端点时调用。从一种方法中删除映射可以解决这一特定问题。

关于java - Spring MVC Web 应用程序中映射到 HTTP 路径的处理程序方法不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43709523/

相关文章:

java - JSP 中每个标记的逗号分隔值

java - for 循环中的 Web 元素

java - QuickBooks java SOAP Web 连接器结构

Java Validator 在失败时锁定文件——我做错了什么?

java - 将依赖项注入(inject) taglib 类?

java - 使用 Spring/EHCache 在负载下刷新缓存

java - 如果类具有我无法使用的构建器模式,如何正确设置变量

java - 如何让notify()在wait()之前执行,我尝试使用sleep()但仍然不起作用

java - 在 Java 中,当数组是 HashMap 的值时,更改数组的特定值

javascript - 像 var a = (xml code) 这样的 JS 语法是如何工作的?