请求方法'GET'不支持Spring MVC

时间:2013-09-19 13:46:53

标签: java spring jsp spring-mvc jstl

我在控制器中有以下代码

  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中。

<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中。请让我知道我做错了什么。

目前我收到一条消息:请求方法'GET'不受支持

 @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;
}

4 个答案:

答案 0 :(得分:4)

我将开始回答并在细节可用时添加。对于您当前的错误

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

这是因为当你有类似

之类的东西时
@RequestParam String selected,

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

中的值
<select id="cJobs" name="cJobs" >

所以请将@RequestParam更改为

@RequestParam(value = "cJobs") String selected

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

答案 1 :(得分:2)

替换

@RequestMapping(value = MAIN_VIEW, method = RequestMethod.POST)

@RequestMapping(value = MAIN_VIEW, method = RequestMethod.GET)

您还应该考虑更改表单POST方法。您的用例更多是Get而不是POST

答案 2 :(得分:0)

使用下一个代码

@RequestMapping(value = MAIN_VIEW, method = RequestMethod.GET)
//if you want to use both ( post and get ) remove Request Method annotation

//@RequestMapping(value = MAIN_VIEW)
@RequestParam(value="cJobs", required = false) String selected; 
//use this to handle an optional parameter

答案 3 :(得分:-2)

您只需在代码中使用的 import mx.controls.Alert; import mx.events.FlexEvent; [Bindable] private var quizLevel:QuizLevel = new QuizLevel(); private var check1:Boolean = false; private var check2:Boolean = false; private var check3:Boolean = false; private var check4:Boolean = false; private var check5:Boolean = false; private var check6:Boolean = false; protected function windowedapplication1_preinitializeHandler(event:FlexEvent):void { quizLevel = new QuizLevel(); quizLevel.levelNo = 1; quizLevel.value1 = true; quizLevel.value2 = true; quizLevel.value3 = true; quizLevel.value4 = false; quizLevel.value5 = false; quizLevel.value6 = false; } protected function changeClicked(event:MouseEvent):void { var checkBox:CheckBox = event.currentTarget as CheckBox; switch(event.currentTarget.id) { case "chk1":{ check1 = checkBox.selected; break; } case "chk2":{ check2 = checkBox.selected; break; } case "chk3":{ check3 = checkBox.selected; break; } case "chk4":{ check4 = checkBox.selected; break; } case "chk5":{ check5 = checkBox.selected; break; } case "chk6":{ check6 = checkBox.selected; break; } } } protected function btnSubmit_clickHandler(event:MouseEvent):void { var message:String = ""; if (quizLevel.value1 == check1 && quizLevel.value2 == check2 && quizLevel.value3 == check3 && quizLevel.value4 == check4 && quizLevel.value5 == check5 && quizLevel.value6 == check6) { message = "It'OK"; } else { message = "You're wrong"; } Alert.show(message); } ]]> </fx:Script> <s:VGroup width="100%" height="100%"> <s:CheckBox id="chk1" label="Choose #1" click="changeClicked(event)" /> <s:CheckBox id="chk2" label="Choose #2" click="changeClicked(event)" /> <s:CheckBox id="chk3" label="Choose #3" click="changeClicked(event)" /> <s:CheckBox id="chk4" label="Choose #4" click="changeClicked(event)" /> <s:CheckBox id="chk5" label="Choose #5" click="changeClicked(event)" /> <s:CheckBox id="chk6" label="Choose #6" click="changeClicked(event)" /> <s:Button id="btnSubmit" label="Submit" click="btnSubmit_clickHandler(event)" /> </s:VGroup> 进行更改,例如

@RequestParam

替换上面的代码行
@RequestParam("studentName") String name

你的问题肯定会得到解决。