I want to use x-editable into my page, and I learn it from this document.
The html element is here:
Click and input
And my controller is:
#RequestMapping(value = "/candidates/updateDisplayName", method = RequestMethod.POST)
public #ResponseBody String updateDisplayName(#RequestParam(value = "displayName") String displayName, HttpServletRequest request) {
System.out.println("Update display name");
System.out.println(displayName);
return "";
}
However, I got error again and again, the error message is like:
{"timestamp":1417250586743,"status":400,"error":"Bad
Request","exception":"org.springframework.web.bind.MissingServletRequestParameterException","message":"Required
String parameter 'displayName' is not
present","path":"/candidates/updateDisplayName"}
I knew that it's caused by the request parameter but not sure how to solve, could anyone help with this? Thanks a lot.
I changed the "displayName" to "name" in the #RequestParam it works, thanks for chrome developer tools, it helps me.
#RequestMapping(value = "/candidates/updateDisplayName", method = RequestMethod.POST)
public #ResponseBody String updateDisplayName(#RequestParam(value = "name") String displayName, HttpServletRequest request) {
System.out.println("Update display name");
System.out.println(displayName);
return "";
}
Related
my web application has multiple jsp tags where the GET method is being called and security token is part of the URL.Now after the last security review we have been asked to remove security token from the URL. What are the options I have here ?What is the best approach ? Thank you in advance for any input!
jsp
<fmt:message key="label.chkNbr" />
java method
#RequestMapping(value = SORT_URL_MAPPING, method = RequestMethod.GET)
public String sortCompany(
#PathVariable("userType") String userType,
WebRequest webRequest,
#RequestParam(required = true, value = "sortFieldName") String sortFieldName,
ModelMap modelView) {
DataTableViewer<Company
> dataTableViewer = getDataTableViewer(webRequest);
dataTableViewer.toggleColumnSortOrder(sortFieldName);
initializeModelView(modelView, webRequest);
return SUCCESS_VIEW;
}
I would like to redirect a message list from a post to a get.
#RequestMapping(method = RequestMethod.GET, value = "/ManageNonRoutedActivities")
public ModelAndView manageNonRoutedActivities(ModelAndView modelAndView,
ArrayList<Message> messages,
#ModelAttribute("nonRoutedActivity") NonRoutedActivity nonRoutedActivity,
#RequestParam(value = "search", defaultValue = "") String search,
#RequestParam(value = "orderField", defaultValue = "Description") String orderByField,
#RequestParam(value = "orderDirection", defaultValue = "asc") String orderByDirection) {
ManageNonRoutedActivitiesRequest request = new ManageNonRoutedActivitiesRequest(new ManageNonRoutedActivitiesDAOImpl(), search, orderByField, orderByDirection);
System.out.println(messages.size());
modelAndView.setViewName("default");
modelAndView.addObject("viewModel", new ManageNonRoutedActivitiesImpl(request, search, orderByField, messages));
return modelAndView;
}
#RequestMapping(method = RequestMethod.POST, value = "/ManageNonRoutedActivities")
public String updateOrCreateActivity(#Valid NonRoutedActivity nonRoutedActivity,
BindingResult result,
RedirectAttributes redirectAttributes) {
SecurityAccessor securityAccessor = new SecurityAccessor();
ArrayList<Message> messages = new ArrayList<>();
if (result.hasErrors()) {
return manageErrorsForManageNonRoutedActivities(result, nonRoutedActivity, redirectAttributes, messages);
}
if (nonRoutedActivity.shouldUpdateEndDate()) {
nonRoutedActivity.updateEndDate();
messages.add(new Message("endDate.add.success", "success"));
} else
messages.add(new Message("activity.add.success", "success"));
nonRoutedActivity.setModifiedBy(securityAccessor.getLoggedInUsersName());
nonRoutedActivity.save();
redirectAttributes.addFlashAttribute("messages", messages);
return "redirect:/ManageNonRoutedActivities";
}
The problem I'm having is that messages in the get part of the controller keeps coming out null. I originally had this messages instance as a List<Message> but this did not work since messages can be null. This caused an issue since List cannot be instanciated. This is why I am using ArrayList. This is not working though because the ArrayList is not binding correctly. Anyone know why this is happening and how to resolve this issue without creating a wrapper for the list?
So I'm not sure this is the best solution but the if I remove the name of the redirect attribute it works.
My requirement is simple i.e i want to write a common methods which suppose to have auto intelligence based on user request it will generate response, like if i submit as a html it should produce html . if i submit as a Json it should produce Json.
Below is 2 sample code ,If i write 2 separate method it works fine but i want to write it to one common method.
1)below is sample code which works for html
#Controller
public class Program1Controller {
#RequestMapping("/helloworld")
public #ResponseBody ModelAndView helloWord(){
String message = "Welcome to TEST";
return new ModelAndView("Test1", "message",message);
}
}
2)below is sample code which work for json
#RequestMapping(value = DUMMY_EMP, method = RequestMethod.GET)
public #ResponseBody String getDummyEmployee() {
String message = "Welcome to TEST";
return message;
}
Instead writing 2 separate method i want to write one method which should have auto intelligence to send a response based on user request.
Above Query for GET ,Same also how can i do for POST.
You can handle that one in ajax. In your controller you have 2 different methods returning JSON, POST and GET and it can have the same value for its RequestMapping. Example:
#RequestMapping(value = "/returnJSON", method = RequestMethod.GET)
public #ResponseBody String getDummyEmployee() {
String message = "Welcome to TEST";
return message;
}
#RequestMapping(value = "/returnJSON", method = RequestMethod.POST)
public #ResponseBody String getDummyEmployee2() {
String message = "Welcome to TEST";
return message;
}
This should also be done for the one returning the HTML Object:
#RequestMapping(value = "/helloworld", method = RequestMethod.GET)
public #ResponseBody ModelAndView helloWord(){
String message = "Welcome to TEST";
return new ModelAndView("Test1", "message",message);
}
}
#RequestMapping(value = "/helloworld", method = RequestMethod.POST)
public #ResponseBody ModelAndView helloWord2(){
String message = "Welcome to TEST";
return new ModelAndView("Test1", "message",message);
}
}
Now, before going to the jquery ajax call, pass as parameter the url and type:
$.ajax({
type: type,
url: url,
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
I've been googling a while and I couldn't find a clear answer or documentation about this specific method.
I want to redirect to another site, like stackoverflow.com using this method... But I don't know how to do it. Any help will be appreciated.
#RequestMapping(value = "/redirectTravelocity", method = RequestMethod.GET)
private ModelAndView processForm()
{
ModelAndView modelAndView = new ModelAndView( "redirect:stackoverflow.com" );
Map<String, Object> model = modelAndView.getModel();
model.put( "error", "this.is.my.error.code" );
return new ModelAndView( "redirect:stackoverflow.com", model );
}
It doesn't work, it redirects within my site and it crashes... I know this is stupid but I don't know how to do it.
Here is one way to do it:
#RequestMapping(value = "/redirectTravelocity", method = RequestMethod.GET)
private String processForm()
{
return "redirect:http://stackoverflow.com";
}
change redirect:stackoverflow.com to redirect:http://stackoverflow.com
I'm trying to make a true RestFull service and keeping to the documentation. However I'm stuck now with a problem I can't see a clear answer for. I want to use a filter to query some data from the webservice. The following path is defined on the controller of the webservice
#RequestMapping(value="/rest/postalcode/list/filter?lang={lang}&postalcode={postalcode}&country={country}&city={city}", method = RequestMethod.GET, produces="application/json")
public #ResponseBody JsonPostalCodeList findFilteredPostalCodes(#PathVariable("lang") String lang, #PathVariable("postalcode") String postalcode, #PathVariable("country") Long country, #PathVariable("city") String city, Model model) throws Exception {
}
Then I try to call it with the following method on client side
public JsonPostalCodeList findPostalCodes(
JsonPostalCodeSelectorData selectorData) {
String url = getWebserviceLocation()+"/rest/postalcode/list/filter?lang={lang}&postalcode={postalcode}&country={country}&city={city}";
MbaLog.debugLog(logger,"Calling webservice with url: " + url);
return getRestTemplate().getForObject(url, JsonPostalCodeList.class, selectorData.getContactLanguage(), selectorData.getPostalCode(), selectorData.getCountry(), selectorData.getCity());
}
now selectorData.getPostalCode() can be null for example because , the user didn't fill in a postalcode to filter on. Same can be true for country and city (lang is always filled in). But each time I run it I get an IOException not found (probably due to the null's). I tried once with everything filled in and I go perfectly in my method at service side. So how do you handle such a problem ?
I can solve it by throwing GET out of the window and just put everything in a POST body as a JSONobject mapped with Jackson and problem solved. But then I'm using a POST to fetch data while a GET should be used in pure REST to fetch data.
So RestTemplate and querying services with variable data how to go about it ?
Just took a cold shower and found it out myself :)
I don't have to use pathvariables I can just use request parameters.
#RequestMapping(value="/rest/postalcode/list/filter", method = RequestMethod.GET, produces="application/json")
public #ResponseBody JsonPostalCodeList findFilteredPostalCodes(#RequestParam("lang") String lang, #RequestParam("postalcode") String postalcode, #RequestParam("country") Long country, #RequestParam("city") String city, Model model) throws Exception {
}
and calling it with
#Override
public JsonPostalCodeList findPostalCodes(
JsonPostalCodeSelectorData selectorData) {
String url = getWebserviceLocation()+"/rest/postalcode/list/filter?lang={lang}&postalcode={postalcode}&country={country}&city={city}";
MbaLog.debugLog(logger,"Calling webservice with url: " + url);
return getRestTemplate().getForObject(url, JsonPostalCodeList.class, selectorData.getContactLanguage(), selectorData.getPostalCode(), selectorData.getCountry(), selectorData.getCity());
}