请求方法'GET'不支持Spring MVC [英] Request method 'GET' not supported Spring MVC

查看:82
本文介绍了请求方法'GET'不支持Spring MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器中有以下代码

I have the following code in the controller

  private static final String CJOB_MODEL    = "cJobNms";

  @RequestMapping(value = MAIN_VIEW, method = RequestMethod.POST)
  public String showTestXsd(//@ModelAttribute(FORM_MODEL) TestXsdForm xsdForm,
            //@ModelAttribute(MODEL) @Valid
            //TestXsdTable testXsdTable,
            @RequestParam String selected,
            Model model) throws DataException {

        String cJobNm =null;
        List<String> cJobNmList = null;
        System.out.println("selected:"+ selected);
        String xsdView = testXsdService.getXsdString();
        cJobNmList = testXsdService.getCJobNm();
        Set<String> cJobNmSet = new HashSet<String>(cJobNmList);
        TestXsdForm xsdForm = new TestXsdForm();
        model.addAttribute("xsdView", xsdView);
        model.addAttribute("xsdFormModel", xsdForm);
        model.addAttribute(CJOB_MODEL, cJobNmSet);
        xsdForm.setXsdString(xsdView);

        return MAIN_VIEW;
    }

还有我的jsp中的以下代码.

And the following code in my jsp.

<form:form modelAttribute="testXsdTable" name="xsdForm" action="/xsdtest/testXsdTables"
                        id="xsdForm" method="POST" class="form"
                                            enctype="multipart/form-data" >


            <tr>
             <td>
              <label for="cJobs" class="fieldlabel">Jobs:</label>
               <select id="cJobs" name="cJobs" >
                <option value="${selected}" selected>${selected}</option>
                <c:forEach items="${cJobNms}" var="table">
                    <c:if test="${table != selected}">
                            <option value="${table}">${table}</option>
                        </c:if>
                 </c:forEach>
               </select>
            </td>
            </tr>
            <pre>
               <c:out value="${xsdForm.xsdString}"/>
            </pre>
<div class="confirmbuttons">
    <a href="#"class="button positive" id="saveXsdButton" onclick="saveTestXsd();">
        <span class="confirm">Save</span>
    </a>
</div>

当用户从cJobNms列表中选择一个选项时,所选值应显示在控制器方法showTestXsd中.请让我知道我在做什么错.

When the user selects an option from the cJobNms list the selected value should be displayed in the controller method showTestXsd. Please let me know what I am doing wrong.

当前我收到一条消息:不支持请求方法"GET"

Currently I am getting a message : Request method 'GET' not supported

 @RequestMapping(value = SAVE_VIEW, method = RequestMethod.POST)
public String saveTestXsd( @ModelAttribute(MODEL) @Valid
                            TestXsdTable testXsdTable,
                            final BindingResult result,
                            final Principal principal,
                            Model model) throws DataException {

    boolean isNew = true;
    System.out.println("entering saveTestXsd in controller");
    Map<String,Object> modelMap = model.asMap();
    String xsdView = (String)modelMap.get("xsdView");
    System.out.println("xsdView:::"+ xsdView);
    if(testXsdTable!= null){
         System.out.println("xsdView(testXsdForm):::"+ testXsdTable.getXsdView());
    }


    // Check for validation errors
    if (result.hasErrors()) {
        return SAVE_VIEW;
    }

    // Get the user information
    User loggedInUser = (User) ((Authentication) principal)
                        .getPrincipal();

    return SAVE_VIEW;
}

推荐答案

我将开始回答,并在详细信息可用时将其添加到其中.对于您当前的错误

I'm going to start an answer and add to it as details become available. For your current error

org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'selected' is not present

发生这种情况是因为当您有类似的东西

This happens because when you have something like

@RequestParam String selected,

并且@RequestParam没有设置value属性,Spring将使用参数名称来查找要绑定的请求参数.在您的表单中,您显然没有名为selected的参数.您想要的是在

And @RequestParam doesn't have a value attribute set, Spring will use the name of the parameter to look for the request parameter to bind. In your form, you obviously don't have a parameter named selected. What you want is to get the value in

<select id="cJobs" name="cJobs" >

因此将您的@RequestParam更改为

@RequestParam(value = "cJobs") String selected

匹配select输入元素的name属性.

这篇关于请求方法'GET'不支持Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆