【问题标题】:Spring MVC @RequestMapping for class level and method level not working用于类级别和方法级别的 Spring MVC @RequestMapping 不起作用
【发布时间】:2020-02-23 00:55:48
【问题描述】:

我尝试放置 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;
    }

}

附上文件夹结构的图片

还有一个额外的发现,我试图在 home/home.jsp 中寻找 jsp。我能知道为什么会这样吗?右边应该只看home.jsp。如果 remvoe 类级别注释,那么它工作正常,或者我在 webapp home 内创建一个文件夹它工作正常但再次单击提交后它会查找 home/home/home.jsp

我添加了一个方法="POST"

现在问题是重启tomact后

点击后像这样提交

如果再次点击提交显示错误 404

这是我的 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>

【问题讨论】:

  • “不工作”是什么意思?有什么错误吗?
  • HTTP 错误 404。声明它没有找到它正在寻找 home/home.jsp 的 jsp。但是为什么需要查看文件夹 home 并搜索 home.jsp
  • 为什么不删除类级别的注释并同时更改form action="home"?
  • @SoftwareEngineer 这很奇怪。你试过/home/home action 属性吗?
  • 默认请求映射方法是'GET'方法。表单提交默认使用 'POST' 方法。您可以将方法设置为 POST

标签: java maven spring-mvc


【解决方案1】:

如果您只是想让当前的代码正常工作,那么一个简单的解决方案是将 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>

【讨论】:

    【解决方案2】:

    这是可行的

    @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>
    

    你也不需要 web.xml,因为你已经在你的 WebConfig 和 SpringConfig 中描述了部署描述符

    【讨论】:

    • 嗨还是不行localhost:8080/web_main/home?t1=&t2=消息描述请求的资源不可用。
    • 你是在第一次使用localhost:8080/web_main/home 打开页面然后点击提交吗? “web_main”来自哪里?我没有在你的配置中看到
    • 在我写的答案中我使用 localhost:8080/home/home 打开页面,然后当点击提交时,它将返回相同的方法并再次加载 localhost:8080/home/home . localhost:8080/about/home 也会发生相同的处理,并且两个地址都加载相同的 home.jsp。
    • 是的,我第一次使用 localhost:8080/web_main/home.jsp 打开它。然后第二次提交变成 localhost:8080/web_main/home/home.jsp 和第三次 localhost:8080/web_main/home/home/home.jsp 错误 404/ web_main 是我创建的文件(项目名称)
    • 我分享了你的 web,xml,但我删除了它,因为我使用基于注释的 java 没有使用它。但我得到了分享你,我以前在删除之前使用的xml
    【解决方案3】:

    在 404 选项卡中有 3 个主页而不是 URL 中所需的 2 个主页,因此它显示 404 。 而不是提交表单中的相对路径 home/home 给出绝对 /home/home

    【讨论】:

    • 是的,这是正确的,但为什么要显示 3 个主页?右边应该只有 1 个家,因为它在 webapp 的 home.jsp 中
    • 这是由于应用程序的重定向导致 URL 更改为 home/home 而不是它正在重定向的 jsp
    【解决方案4】:

    网址中有一个问题。 你观察过实际的网址吗? 它在上下文之后显示 /home/home/home,但 url 指向 /home/home。 解决方案可能是从表单标记中的操作 url 中删除一个 /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>
    

    【讨论】:

    • 我关注了我们的头部部分和@Dot Batch 解决方案。谢谢你
    【解决方案5】:

    问题是您的操作路径&lt;form action="home/home"&gt; 这是一个相对路径,意味着它将执行以下操作。

    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 不见了。

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

    <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
    
    <c:url value="/home/home" var="homeUrl" />`
    <form action="${homeUrl}">
    

    将导入添加到 JSP 的顶部。

    【讨论】:

    • 嗨,它显示 HTTP 状态 404 - 当第一次单击提交时。 localhost:8080/web_main/$%7BhomeUrl%7D?t1=2&t2=2
    • 我已经在maven中添加了依赖javax.servletjstl1.2跨度>
    • 我在 head java.sun.com/jsp/jstl/core" %> 然后它工作了。谢谢你
    • @Software Engineer &lt;%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %&gt; 是您添加到 jsp 文件顶部的导入。
    • 如果你愿意,可以上传到github,我可以直接发表评论
    【解决方案6】:

    当使用RequestMapping默认请求映射method值是GET方法。 Html 表单提交默认使用POST 方法。所以你需要在这个 url 中添加 post 方法。您可以为同一个请求映射提供多个方法值。

    【讨论】:

    • 我试过了,方法级别@RequestMapping(value = "home", method = RequestMethod.POST),类级别@RequestMapping(value = "home") .但仍然无法正常工作,得到 HTTP 状态 405 - 不支持请求方法“GET”;消息:不支持请求方法“GET”;描述:请求的资源不允许使用指定的 HTTP 方法。
    • 您需要在方法级别添加 post 方法。并将其从类级别中删除
    • 你有根 url 路径吗?如果你有它,你需要在表单中添加它
    • 我之前也尝试过添加 post 方法,但没有成功
    • 什么是根 url?
    【解决方案7】:

    您的 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;
        }
    
    }
    

    【讨论】:

    • 嗨,我的是 spring mvc。需要这样做吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    相关资源
    最近更新 更多