I am submitting a form using jquery in my Spring mvc.
this is the jquery call to submit form.
function uploadJqueryFormForEdit(documentId){
alert("ccc");
$('#result').html('');
$("#editDocumentForm").ajaxForm({
success:function(data) {
alert("ddd");
$('#result').html(data);
alert("eee");
//getProjectSegment('documents','DocumentSegment',projectId);
$('#editDocumentForm').remove();
},
error:function(e){
alert(e.responseText);
$("#msgDiv").html('Error');
},
dataType:"text"
}).submit();
}
And this is the form that I'm going to submit.
<form action="cont/uploadEdit?documentId=15&projectId=2" name="editDocumentForm" id="editDocumentForm" enctype="multipart/form-data" method="post">
When i'm using one parameter in action url, eg.
action="cont/uploadEdit?documentId=15"
it works fine. but when i'm using two parameters as
action="cont/uploadEdit?documentId=15&projectId=2"
it doesn't call to controller method correctly(not hitting that method at all)
here is the controller method
#RequestMapping(value = "cont/uploadEdit", method = RequestMethod.POST)
public #ResponseBody String uploadEdit(#ModelAttribute("sessionId") String sessionId,#RequestParam("documentId") int documentId,#RequestParam("projectId") int projectId,MultipartHttpServletRequest request, HttpServletResponse response, UploadedFile fileDetail,UserBean userbean,Model model) throws SessionException {
logger.info("uploadEdit");
}
why can't I use two parameters in action tag.?
this is the controller method that worked fine with first action
#RequestMapping(value = "cont/uploadEdit", method = RequestMethod.POST)
public #ResponseBody String uploadEdit(#ModelAttribute("sessionId") String sessionId,#RequestParam("documentId") int documentId,MultipartHttpServletRequest request, HttpServletResponse response, UploadedFile fileDetail,UserBean userbean,Model model) throws SessionException {
logger.info("uploadEdit");
}
Add params = {"documentId", "projectId"} attribute to the RequestMapping annotation
#RequestMapping(value = "cont/uploadEdit", params = {"documentId", "projectId"}, method = RequestMethod.POST)
public #ResponseBody String uploadEdit(#ModelAttribute("sessionId") String sessionId,#RequestParam("documentId") int documentId,#RequestParam("projectId") int projectId,MultipartHttpServletRequest request, HttpServletResponse response, UploadedFile fileDetail,UserBean userbean,Model model) throws SessionException {
logger.info("uploadEdit");
found the error:
In the form that we are going to submit (in my case "editDocumentForm") there should be no any other input tags with the same name as in action url variables,
eg if there is something like this,
<input type="hidden" id="projectId" name="projectId" value="somevalue"/>
it will make conflicts. So make sure that no conflicts occur.
Related
I have used the #ModelAttribute annotation on a get method in my controller:
#ModelAttribute
public TSetTestEmp get(#RequestParam(required=false) String id) {
TSetTestEmp entity = null;
if (StringUtils.isNotBlank(id)){
entity = tSetTestEmpService.get(id);
}
if (entity == null){
entity = new TSetTestEmp();
}
return entity;
}
and have requested the TSetTestEmp info like this:
#RequiresPermissions("set:emp:tSetTestEmp:list")
#RequestMapping(value = {"list", ""})
public String list(TSetTestEmp tSetTestEmp, HttpServletRequest request, HttpServletResponse response, Model model) {
Page<TSetTestEmp> page = tSetTestEmpService.findPage(new Page<TSetTestEmp>(request, response), tSetTestEmp);
// model.addAttribute("tSetTestEmp", tSetTestEmp); // fixed using this.
model.addAttribute("page", page);
return "modules/set/emp/tSetTestEmpList";
}
As beginning without the comment statement, the JSP code like below throw an exception:
<form:form id="searchForm" modelAttribute="tSetTestEmp" action="${ctx}/set/emp/tSetTestEmp/" method="post" class="form-inline">
<span>Employee Nameļ¼</span>
<form:input path="empName" htmlEscape="false" maxlength="64" class=" form-control input-sm"/>
</form:form>
Error
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'tSetTestEmp' available as request attribute
According to my debugging, the form:input tag causes this exception. If I use input tag and bind the value empName using ${tSetTestEmp.empName}, this page will show the right result.
If I want to use the form:input tag in my JSP page, the comment in the list method must be removed which means I must assign the Model manually.
#M. Deinum suggested me to fix the problem like this and it works:
#RequiresPermissions("set:emp:tSetTestEmp:list")
#RequestMapping(value = {"list", ""})
public String list(#ModelAttribute("tSetTestEmp") TSetTestEmp tSetTestEmp, HttpServletRequest request, HttpServletResponse response, Model model) {
Page<TSetTestEmp> page = tSetTestEmpService.findPage(new Page<TSetTestEmp>(request, response), tSetTestEmp);
model.addAttribute("page", page);
return "modules/set/emp/tSetTestEmpList";
}
But as I can see, the initial code without #ModelAttribute before method argument can also reach the instance returned by get method. However the JSP page just cannot reach it same as back-end do.
I'm totally confused about how this situation happens.
Thanks for reading till here :)
Any suggestion will help.
I'm trying to get a RequestMapping to pick up the following url pathname.
action?param1=6¶m2=b¶m3=true¶m4=50652¶m5=2
I'm able to get the path below to work. However when converting & to '& amp;' I am unable to read any parameters.
action?param1=6¶m2=b¶m3=true¶m4=50652¶m5=2
Method for action
#RequestMapping(value = "/action", method = RequestMethod.GET)
public ModelAndView doAction(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
throws Exception {
log.info(httpRequest.getParameter("param1"));
}
FYI
I want to steer clear of any string manipulations
I have the page about.ftl which is invoked when I type localhost:8080/ui/about
and I have put the following block of code inside. Through SendToServlet() function I am trying to send the user info to my controller which is the OAuthController.java
function SendToServlet(){
$.ajax({
url: "localhost:8080/ui/about",
type: "POST",
data: JSON.stringify({ user:jsonObj}),
contentType: 'application/json',
success: function(result) {
alert(done);
},
error: function(xhRequest, ErrorText, thrownError) {
alert(JSON.stringify(jsonObj));
}
});
}
</script>
My Spring MVC Controller class code has the following implementation - all that it does is that it accepts the user's information and then sets up current user:
#Controller
#RequestMapping("/about")
public class OAuthController {
#RequestMapping(method = RequestMethod.POST)
#ResponseBody
public String post( #RequestBody String items, HttpServletRequest request, HttpServletResponse response)
{
String jsonResp = items;//sb.toString();
ArrayList<String> usercredentials = new ArrayList<String>();
usercredentials = parseJson(jsonResp);
UserMethods usermethods = new UserMethods();
usermethods.setCurrentUser (usercredentials);
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
return "{\"success\":\"\"}";
}
public ArrayList<String> parseJson(String json){
}
}
My problem is that the controller is never invoked and actually I never see a Post request to be sent anywhere through Firebug. I've spent several days on it, but still no luck. Do you have any suggestions?
You need to bind value with your function. Your url localhost:8080/ui/about/post is just used to locate the controller but it will not execute any function because you didn't make a call to any function.
To call the function, you have to do like this :
#RequestMapping(method = RequestMethod.POST, value ="/anyValue")
public String anyFunctionName(anyParam){...}
Above function will bind to url localhost:8080/ui/about/anyValue.
Don't you need to call it like this?
url: "localhost:8080/ui/about/post",
but first do this:
#RequestMapping(method = RequestMethod.POST, value = "/post")
How about try to add .html?Application won't add it automatically.
How do I enhance the Controller below to utilize Spring MVC's Flash Attributes? The use case is a copy function.
POST/REQUEST/GET implementation:
client clicks "copy" button in the UI
server sets the response "Location" header
client redirects to "path/to/page?copy"
server provides ModelAndView
client (jQuery success function) sets window.location
FooController redirect method:
#RequestMapping(value = "{fooId}", method = POST, params = { "copy" })
#Transactional
#ResponseStatus(CREATED)
public void getCopyfoo(#PathVariable String fooId,
HttpServletResponse response, RedirectAttributes redirectAttrs) {
response.setHeader("Location", uriPath);
//no worky?!:
redirectAttrs.addFlashAttribute("barKey", "barValue");
}
FooController get method:
#RequestMapping(value = "{fooId}", method = GET)
#Transactional(readOnly = true)
public ModelAndView findFooById(#PathVariable String fooId,
HttpServletRequest request){
Map<String, ?> map = RequestContextUtils.getInputFlashMap(request);
// map is empty...
return modelAndViewFromHelperMethod();
}
I'm affraid that RedirectAttributes work only with RedirectView,
so Your controller should return for example:
1. String: "redirect:/[uriPath]"
2. new RedirectView([uriPath])
If you realy need to handle server response with JS, then maybe handling HTTP status 302 would help.
An item is displayed at this URL:
/item/10101
using this Controller method:
#RequestMapping(value = "/item/{itemId}", method = RequestMethod.GET)
public final String item(HttpServletRequest request, ModelMap model,
#PathVariable long itemId)
{
model = this.fillModel(itemId);
return "item";
}
The page contains a form that submits to the following method in the same controller:
#RequestMapping(value = "/process_form", method = RequestMethod.POST)
public final String processForm(HttpServletRequest request,
#ModelAttribute("foo") FooModel fooModel,
BindingResult bindResult,
ModelMap model)
{
FooModelValidator validator = new FooModelValidator();
validator.validate(FooModel, bindResult);
if (bindResult.hasErrors())
{
model = this.fillModel(fooModel.getItemId());
return "item";
}
return "account";
}
If the validator finds errors in the form, it redisplays the item but instead of displaying it at the original url:
/item/10101
it displays it at its own url:
/process_form
Is it possible to redisplay the form at the original URL?
/item/10101
(I've tried getting the referrer and redirecting to it in processForm but then all of the model contents end up displayed as URL name/value pairs:)
#RequestMapping(value = "/process_form", method = RequestMethod.POST)
public final String processForm(HttpServletRequest request,
#ModelAttribute("foo") FooModel fooModel,
BindingResult bindResult,
ModelMap model)
{
String referrer = request.getHeader("referer");
FooModelValidator validator = new FooModelValidator();
validator.validate(FooModel, bindResult);
if (bindResult.hasErrors())
{
model = this.fillModel(fooModel.getItemId());
return "redirect:" + referrer;
}
return "account";
}
Short answer: No.
What happens is a server-side redirect (forward), which is within the same request, and so the submitted values are preserved (and displayed in the form)
The url will change if you use a client-side redirect (return "redirect:item";), but in that case a new request will come and the submitted values will be lost.
But here are two options that you have:
use the same URL in the mappings for both methods and distinguish them based on request method - GET for the former, POST for the latter. This might be confusing, so document it.
find / implement flash scope for spring-mvc. There's nothing built-in. The flash scope means that values are preserved (in the session usually) for a submit and the subsequent redirect. This option includes the manual handling, by putting the submitted object in the session, and later retrieving & removing it