Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 在SpringMvc中结合POST和GET_Spring Mvc_Model View Controller_Controller - Fatal编程技术网

Spring mvc 在SpringMvc中结合POST和GET

Spring mvc 在SpringMvc中结合POST和GET,spring-mvc,model-view-controller,controller,Spring Mvc,Model View Controller,Controller,对于用户注册表单(registration.html),我通过以下方式创建了一个视图控制器: @Configuration @EnableWebMvc public class MvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(final ViewControllerRegistry registry) { super.addViewControl

对于用户注册表单(
registration.html
),我通过以下方式创建了一个视图控制器:

@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(final ViewControllerRegistry registry) {
      super.addViewControllers(registry);
      registry.addViewController("/user/registration").setViewName("registration");
  }
这很好,如果我转到/user/registration(即GET),我可以看到注册表

但是,如果我现在想通过以下方式为相同uri的POST请求创建控制器:

@Controller
public class RegistrationController {
    @RequestMapping(value = "/user/registration", method = RequestMethod.POST)
    @ResponseBody
    public GenericResponse registerUserAccount(@Valid final UserDto accountDto, final HttpServletRequest request) {

         // some code
    }
}
我在/user/registration uri处收到一条错误消息,上面说:

不支持请求方法“GET”


因此,我的post控制器似乎以某种方式覆盖了之前工作的GET控制器。为什么呢?有没有可能使这两种方法协同工作,或者我必须以与我的post控制器相同的方式编写自己的GET控制器?

addViewController您正在获取注册视图,如果url为/user/registration,请尝试在使用post方法提交视图时使用不同的url,或者使用post http方法从客户端使用ajax调用此url。好的,你所有的建议都有效。如果在
类注册控制器
中创建一个GET映射
@RequestMapping(value=“/user/registration”,method=RequestMethod.GET)
,它也可以工作。我的问题仍然是,为什么
class RegistrationController
中的post控制器(似乎)使
class MvcConfig
中的
addViewController
不再工作?请在浏览器中检查/user/registration此url的http类型。它正在进行get http调用以获取模板,并且相同的url是带有post的控制器,此url没有显式的post http调用,因此我认为它再次使用get http方法进行调用。addViewController您正在获取注册视图,如果url为/user/registration,请尝试使用post方法在提交视图时使用不同的url,或者使用post http方法从客户端使用ajax调用此url。好的,你所有的建议都有效。如果在
类注册控制器
中创建一个GET映射
@RequestMapping(value=“/user/registration”,method=RequestMethod.GET)
,它也可以工作。我的问题仍然是,为什么
class RegistrationController
中的post控制器(似乎)使
class MvcConfig
中的
addViewController
不再工作?请在浏览器中检查/user/registration此url的http类型。它正在进行get http调用以获取模板,并且相同的url是带有post的控制器,这个url没有显式的post http调用,所以我认为它再次使用get http方法进行调用。