用于类级别和方法级别的Spring MVC @RequestMapping不起作用

时间:2019-10-27 12:25:18

标签: java maven spring-mvc

我尝试放置2个请求映射,因为我想指向home / home,因为稍后我将拥有另一个具有about / home名称的控制器。但是我不确定为什么它不起作用。如果只有家,它正在工作,但家/家或关于/家不工作。课程级别无法正常工作。

这是控制器

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.constant.server.HelperConstant;

@Controller
@RequestMapping("/home")
public class HomeController 
{
    @RequestMapping("/home")
    public String doDisplayPage()
    {
        return HelperConstant.VIEW_HOME;
    }

}

这是jsp

<html>
<body>
    <form action="home/home">
        <input type="text" name="t1"><br>
        <input type="text" name = "t2"><br>
        <input type ="submit">
    </form>
</body>
</html>

我正在达到HTTP状态404-

已编辑

附上配置文件

package com.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return new Class[] {SpringConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[] {"/"};
    }

}

这是另一个配置文件

package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan({"com.controller"})
public class SpringConfig 
{
    @Bean
    public InternalResourceViewResolver viewResolver()
    {
        InternalResourceViewResolver vr = new InternalResourceViewResolver();

        //vr.setPrefix("/WEB-INF/");
        vr.setSuffix(".jsp");

        return vr;
    }

}

附加了文件夹结构的图片 enter image description here

还有其他发现,因此我试图在home / home.jsp中寻找jsp。我能知道为什么这样的行为吗?正确的应该只看home.jsp。如果是remvoe类级别的注释,那么它可以正常工作,或者我在webapp home内创建了一个文件夹,它可以正常工作,但是再次单击“提交”后,它将查找home / home / home.jsp

我添加了一个方法=“ POST”

现在问题是重新启动后

enter image description here

点击像这样提交后

enter image description here

如果再次单击提交,则显示错误404

enter image description here

这是我的web.xml,但是由于我已经使用过Java或配置,所以我已经将其删除

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

   <welcome-file-list>
        <welcome-file>home.html</welcome-file>
        <welcome-file>home.htm</welcome-file>
        <welcome-file>home.jsp</welcome-file>
    </welcome-file-list>

</web-app>

7 个答案:

答案 0 :(得分:1)

问题是您的操作路径http://example.com/web_main/home - form action - > http://example.com/web_main/home/home (200) - form action -> http://example.com/web_main/home/home/home (404) ,这是一条相对路径,表示它将执行以下操作。

/home/home

其他人建议使用http://example.com/web_main/home - form action - > http://example.com/home/home ,但这并没有考虑到您的应用程序上下文路径。结果如下。

web_main

请注意,<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> <c:url value="/home/home" var="homeUrl" />` <form action="${homeUrl}"> 不见了。

要解决您的问题,您需要利用JSTL,请将其添加到JSP中。

native/test

将导入添加到JSP的顶部。

答案 1 :(得分:1)

URL中有一个问题。 您观察到实际的网址了吗? 它在上下文之后显示/ home / home / home,但URL指向/ home / home。 解决方案可能是从表单标签中的操作网址中删除一个/ home

<html>
<body>
    <form **action="home"**>
        <input type="text" name="t1"><br>
        <input type="text" name = "t2"><br>
        <input type ="submit">
    </form>
</body>
</html>

<html>
<body>
    <form action="/home/home">
        <input type="text" name="t1"><br>
        <input type="text" name = "t2"><br>
        <input type ="submit">
    </form>
</body>
</html>

答案 2 :(得分:0)

您的Spring应用程序类是什么样的?如果您没有,则应创建一个。 与其将@ComponentScan放在@Configuration之下,不如执行下面的操作,并且应该更容易地将其拾取,也可以将根标记更改为/,以便于阅读:

@SpringBootApplication
@ComponentScan(basePackages = {"com.*"})
class MyClass {
   public static void main(String[] args) {
      SpringApplication.run(MyClass.class)
   }
}

@Controller
@RequestMapping("/")
public class HomeController 
{
    @RequestMapping("/home")
    public String doDisplayPage()
    {
        return HelperConstant.VIEW_HOME;
    }

}

答案 3 :(得分:0)

使用RequestMapping时,默认请求映射method的值为GET方法。 HTML表单提交默认使用POST方法。因此,您需要向该网址添加post方法。您可以为同一请求映射提供多个方法值。

答案 4 :(得分:0)

这是可行的

@Controller
@RequestMapping("/home")
public class HomeController {

    @RequestMapping(value = "/home")
    public String doDisplayPage() {
        return "../home";
    }
}
@Controller
@RequestMapping("/about")
public class AboutController {

    @RequestMapping(value = "/home")
    public String doDisplayPage() {
        return "../home";
    }
}
<html>
<body>
    <form action="/home/home">
        <input type="text" name="t1"><br>
        <input type="text" name = "t2"><br>
        <input type ="submit">
    </form>
</body>
</html>

enter image description here

由于您已经在WebConfig和SpringConfig中描述了部署描述符,因此您也不需要web.xml

答案 5 :(得分:0)

在404选项卡中,您想要的URL中有3个住所而不是2个,因此它显示404。 在提交表单中,不要使用相对路径 home / home ,而是使用绝对 / home / home

答案 6 :(得分:0)

如果您只是想使当前代码正常工作,那么一个简单的解决方案是将Context Root附加到表单操作中,如下所示。

<html>
<head>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
</head>
<body>
    <form action="${ctx}/home/home">
        <input type="text" name="t1"><br> <input type="text"
            name="t2"><br> <input type="submit">
    </form>
</body>
</html>