如何使用Spring标记从JSP表单发送PUT请求

时间:2017-01-18 07:02:10

标签: java spring jsp spring-mvc put

我有jsp文件,其中有一个表单:

<form:form modelAttribute="user" class="form-horizontal" method="put"
                           enctype="multipart/form-data" action="edit"
                           charset="utf-8" accept-charset="UTF-8">
...
</form:form>

这里我使用spring的taglib:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

在我的 WebApp 课程中,我添加了 hiddenHttpMethodFilter

public class WebApp implements WebApplicationInitializer {

    private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
    private static final String DISPATCHER_SERVLET_MAPPING = "/";

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(SpringRootConfig.class);
        rootContext.register(SpringWebConfig.class);
        rootContext.register(SpringDbConfig.class);
        rootContext.register(SpringSecurityConfig.class);

        ServletRegistration.Dynamic servlet = servletContext.addServlet(
                DISPATCHER_SERVLET_NAME, new DispatcherServlet(rootContext)
        );
        servlet.setLoadOnStartup(1);
        servlet.addMapping(DISPATCHER_SERVLET_MAPPING);

        FilterRegistration.Dynamic hiddenHttpMethodFilter = servletContext
                .addFilter("hiddenHttpMethodFilter", new HiddenHttpMethodFilter());
  hiddenHttpMethodFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");

        FilterRegistration.Dynamic encodingFilter = servletContext
                .addFilter("encodingFilter", new CharacterEncodingFilter());
        encodingFilter.setInitParameter("encoding", "UTF-8");
        encodingFilter.setInitParameter("forceEncoding", "true");
        encodingFilter.addMappingForUrlPatterns(null, true, "/*");

        FilterRegistration.Dynamic securityFilter = servletContext
                .addFilter(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME, DelegatingFilterProxy.class
        );
        securityFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");

        servletContext.addListener(new ContextLoaderListener(rootContext));
    }
}

我不知道为什么我不能从这个表单发送PUT请求。此表单发送了POST请求,我收到错误:不支持请求方法“POST”。在我的控制器方法中使用@PostMapping(value = "/edit")注释之前,当我将其更改为@PutMapping(value = "/edit")时,我遇到了这个问题。 我该如何解决?

我的控制器使用应该处理PUT请求的方法:

@Controller
public class RootController extends AbstractUserController {
    @PutMapping(value = "/edit")
    public String updateProfile(@Valid User user, BindingResult result) {
        if (!result.hasErrors()) {
            try {
                user.setId(AuthorizedUser.id());
                super.update(user, user.getId());
                AuthorizedUser.get().update(user);
                return "redirect:profile";
            } catch (DataIntegrityViolationException e) {
                result.addError(new FieldError("user", "email", "already registered!"));
            }
        }
        return "register";
    }
}

P.S。:在我的其他jsp中,我使用没有spring taglib的简单html表单:

<form method="post" action="friends?friendId=${user.id}">
       <input type="hidden" name="_method" value="delete">
          <spring:message code="friends.delete" var="delete_from_friends"/>
     <input type="submit" class="btn btn-danger" value="${delete_from_friends}">
   </form>

它有效!!!

0 个答案:

没有答案