如何通过类级别RequestMapping调用请求映射方法级别

2024-05-09

我使用 spring 做了一个简单的程序。当我没有使用类级别 RequestMapping 时,我得到了方法级别 RequestMapping 的答案。但我想同时使用类级别和方法级别 RequestMapping。

这是我的控制器代码

package com.birthid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/classLevel")
public class Controaller1 
{
     @RequestMapping("/spring")
     public ModelAndView display(@RequestParam("name") String name)
     {
         ModelAndView model=new ModelAndView("view");
         model.addObject("msg", name);
         return model;
     }      
}

html代码

<html>
<head>
   <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   <title>Hello App Engine</title>
</head>

<body>
   <h1>valith web application!</h1>
   <form action="/classLevel" method="get">
      name:<input type="text" name="name"/><br>
      <input type="submit" value="clik me"/>
   </form>
</body>
</html>

当我在地址栏中给出这个网址时。我得到了准确的输出。http:localhost:8888/classLevel/spring?name=john

但是当我按下我在 html 页面中设计的按钮时,就会出现错误。


嗯,问题很简单,就是你的表单动作,你有action="/classLevel"应该是action="/classLevel/spring"因为你的方法有/spring作为 RequestMapping 进行更改:

<form action="/classLevel" method="get">

To :

<form action="/classLevel/spring" method="get">

因为正如您在 url 测试中所做的那样,方法调用应该是:/classLevel/spring.

看一眼使用@RequestMapping映射请求 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping的部分春季文档了解更多信息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何通过类级别RequestMapping调用请求映射方法级别 的相关文章

随机推荐