Spring,不支持请求方法'POST'

时间:2013-03-29 07:44:34

标签: java spring jsp post

首先要道歉要问这个反复的问题。

实际上,在我的春季申请中,我有user.jspprofessional.jsp

这是我的User.jsp:

  <form:form action="profile/user" modelAttribute="profile">
    <div>
        <jsp:include page="professional.jsp"></jsp:include>
    </div>

</form:form>

这是我的professional.jsp:

   <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<fieldset id="profile_proffiesional">
    <form:form action="profile/proffiesional" modelAttribute="PROFESSIONAL" method="POST">
        <p>
            <label for="position">Position</label>
            <form:input path="position" tabindex="4" />
        </p>
        <p>
            <label for="location">Location</label>
            <form:input path="location" tabindex="5" />
        </p>
        <p>
            <label for="description">Description</label>
            <form:input path="description" tabindex="5" />
        </p>
        <p>
            <input type="submit" value="Add">
        </p>
    </form:form>
</fieldset>

这是我的Controller类:

    @Controller
@RequestMapping(value = "profile")
public class UserProfileController {

    @Autowired
    private UserService userService;

    @Autowired
    private SessionData sessionData;

    @RequestMapping(value = "user", method = RequestMethod.GET)
    public String user(Model model) throws Exception {
        model.addAttribute("PROFESSIONAL", new UserProfessionalForm());
        model.addAttribute("EDUCATIONAL", new UserEducationalForm());
        model.addAttribute("AWARDS", new UserAwardsForm());
        return "profile/user";
    }

    @RequestMapping(value = "proffessional", method = RequestMethod.POST)
    public @ResponseBody
    String forgotPassword(UserProfessionalForm professionalForm,
            BindingResult result, Model model) {

        UserProfileVO userProfileVO = new UserProfileVO();
        userProfileVO.setUser(sessionData.getUser());
        userService.saveUserProfile(userProfileVO);
        model.addAttribute("professional", professionalForm);
        return "Your Professional Details Updated";
    }
}

Problem当我们点击Add中的professional.jsp按钮时,服务器控制台中没有响应,但显示的警告信息如下:

  29 Mar, 2013 1:03:51 PM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpRequestMethodNotSupported
WARNING: Request method 'POST' not supported

为什么会出现此警告?我已经指定了method =“POST”..

请帮助..

6 个答案:

答案 0 :(得分:8)

我在sprint security xml文件中启用了csrf,所以我只在表单中添加了一行:

<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" /> 

这样我就可以提交具有model属性的表单。

答案 1 :(得分:7)

在Jsp:

action="profile/proffiesional"

在控制器

@RequestMapping(value = "proffessional", method = RequestMethod.POST)

Spelling MisMatch!

答案 2 :(得分:6)

您的user.jsp:

 <form:form action="profile/proffesional" modelAttribute="PROFESSIONAL">
     ---
     ---
    </form:form>

在您的控制器类中:

(使其成为一个意思是完整的方法名称。我认为你是在DB中插入记录。)

@RequestMapping(value = "proffessional", method = RequestMethod.POST)
    public @ResponseBody
    String proffessionalDetails(
            @ModelAttribute UserProfessionalForm professionalForm,
            BindingResult result, Model model) {

        UserProfileVO userProfileVO = new UserProfileVO();

        userProfileVO.setUser(sessionData.getUser());
        userService.saveUserProfile(userProfileVO);
        model.addAttribute("PROFESSIONAL", professionalForm);

        return "Your Professional Details Updated";

    }

答案 3 :(得分:3)

@ModelAttribute方法中的UserProfessionalForm professionalForm参数缺少forgotPassword注释。

@RequestMapping(value = "proffessional", method = RequestMethod.POST)
public @ResponseBody
String forgotPassword(@ModelAttribute UserProfessionalForm professionalForm,
        BindingResult result, Model model) {

    UserProfileVO userProfileVO = new UserProfileVO();
    userProfileVO.setUser(sessionData.getUser());
    userService.saveUserProfile(userProfileVO);
    model.addAttribute("professional", professionalForm);
    return "Your Professional Details Updated";
}

答案 4 :(得分:1)

试试这个

@RequestMapping(value = "proffessional", method = RequestMethod.POST)
    public @ResponseBody
    String forgotPassword(@ModelAttribute("PROFESSIONAL") UserProfessionalForm professionalForm,
            BindingResult result, Model model) {

        UserProfileVO userProfileVO = new UserProfileVO();
        userProfileVO.setUser(sessionData.getUser());
        userService.saveUserProfile(userProfileVO);
        model.addAttribute("professional", professionalForm);
        return "Your Professional Details Updated";
    }

答案 5 :(得分:1)

有关信息,我删除了动作属性,当我调用ajax帖子时,我收到此错误。即使我在表单中的动作属性看起来像这样action="javascript://;"

我以为我是从ajax调用中获取它并序列化表单但是我再次将伪动作属性添加到表单中并且它有效。