Java Spring MVC 不支持请求方法“GET”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18896435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Request method 'GET' not supported Spring MVC
提问by
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;
}
And the following code in my jsp.
以及我的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>
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.
当用户从 cJobNms 列表中选择一个选项时,所选值应显示在控制器方法 showTestXsd 中。请让我知道我做错了什么。
Currently I am getting a message : Request method 'GET' not supported
目前我收到一条消息:不支持请求方法“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;
}
采纳答案by Sotirios Delimanolis
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,
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
并且@RequestParam
没有value
属性集,Spring会使用参数的名称来寻找要绑定的请求参数。在您的表单中,您显然没有名为selected
. 你想要的是获得价值
<select id="cJobs" name="cJobs" >
So change your @RequestParam
to
所以改变你@RequestParam
的
@RequestParam(value = "cJobs") String selected
to match the name
attribute of the select
input element.
匹配输入元素的name
属性select
。
回答by Gaurav
Replace
代替
@RequestMapping(value = MAIN_VIEW, method = RequestMethod.POST)
with
和
@RequestMapping(value = MAIN_VIEW, method = RequestMethod.GET)
You should also consider changing the form POST method. Your use case is more of Get rather than POST
您还应该考虑更改表单 POST 方法。您的用例更多是 Get 而不是 POST
回答by Jairo Cordero
use the next code
使用下一个代码
@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
回答by user7429629
You just have make a change in @RequestParam
which you have used in your code like
您只需对@RequestParam
您在代码中使用的内容进行更改,例如
@RequestParam("studentName") String name
Replace above line of code with
将上面的代码行替换为
@RequestParam(value="studentName") String name
Your problem will definitely be resolved.
你的问题肯定会得到解决。