【问题标题】:No mapping for GET / in SpringMVCSpring MVC 中没有 GET / 映射
【发布时间】:2018-11-21 18:28:37
【问题描述】:

我正在尝试构建 Spring MVC Web 应用程序。 问题出在我的欢迎页面 (localhost:8080/) 中。在我的输出日志中,我看到: 无 GET 映射/

我将欢迎页面设置为 URL:"/spring-mvc-login" 但每次我重新启动应用程序时,它都会尝试查找 URL "/" 这是不在我的控制器中服务。我希望将欢迎页面重定向到 URL "/spring-mvc-login",但它不起作用。

有趣的是,当我输入“localhost:8080/spring-mvc-login”时,它工作正常。唯一的问题是将此 URL 重定向到欢迎页面。

WEB.XML

 <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/todo-servlet.xml</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>

    <welcome-file-list>
        <welcome-file>/spring-mvc-login</welcome-file>
    </welcome-file-list>

todo-servlet.xml

<bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

控制器

public class LoginController {

    @RequestMapping(value = "/spring-mvc-login", method= RequestMethod.GET)
    public String sayHello(){
        return "login";
    }

    @RequestMapping(value = "/spring-mvc-login", method = RequestMethod.POST)
        public String handleLoginRequest(@RequestParam String name, @RequestParam String password,
                                         ModelMap modelMap){
        modelMap.put("name",name);
        modelMap.put("password", password);
        return "welcome";

        }

}

Controller 中,当我将 @RequestMapping 从“/spring-mvc-login”更改为“/”时它工作正常,但我想将欢迎页面重定向到“/spring-mvc -login”而不是“/”。感谢您的帮助。

【问题讨论】:

  • 我试过了,没用。

标签: java spring spring-mvc dispatcher


【解决方案1】:

确保在 src/main/java 而不是在 src/main/resources 中创建包和类。 如果 maven 没有创建 src/main/java 文件夹,只需创建文件夹并将包文件夹/类移到那里。 这样做为我解决了无映射错误。

希望这些信息对某人有用。

【讨论】:

    【解决方案2】:

    别忘了使用@RestController 注解。 如果你想重定向使用:

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public void redirect(HttpServletResponse httpResponse) throws Exception {
    
         httpResponse.sendRedirect("/spring-mvc-login");
    }
    

    【讨论】:

    • 我正在使用@Controller 注解。当我添加它时它正在工作,但它是设置欢迎页面的正确方法吗?
    • 我会使用 spring-boot-security 包来设置它。如果有人未经身份验证,您可以在那里拥有 / 登陆页面并设置重定向。您可以将上下文设置为 /spring-mvc-login,但这会很奇怪。
    【解决方案3】:

    尝试删除“/”

    之前

    <welcome-file>/spring-mvc-login</welcome-file>
    

    之后

    <welcome-file>spring-mvc-login</welcome-file>
    

    【讨论】:

    • 我有同样的错误“没有为 GET / 映射”所以没有变化
    • 您在控制器类中缺少这些注释 @Controller @RequestMapping("/")
    • 我没有提供 URL“/”,因为我认为我可以将 WEB.xml 中的欢迎页面重定向到地址“/spring-mvc-login”
    • 当然,当我添加此 @RequestMapping("/") 注释时,它工作正常。但是如何将我的欢迎页面重定向到 WEB.xml 中的“/spring-mvc-login”地址?这只能在Controller Class中完成吗?
    • 有什么特别的原因吗? Spring 通过只返回一个字符串来促进这一点,它是新的
    【解决方案4】:

    尝试更改 web.xml 内容:

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

    或将控制器映射更改为:

    @RequestMapping(value={"/", "/spring-mvc-login"})
    

    【讨论】:

      【解决方案5】:

      将@Controller注解放在Controller类之上

      【讨论】:

        猜你喜欢
        • 2021-04-29
        • 1970-01-01
        • 2018-02-28
        • 1970-01-01
        • 2020-07-30
        • 2012-08-14
        • 2019-09-27
        • 2014-07-19
        • 2020-01-08
        相关资源
        最近更新 更多