Spring MVC 3, forwarding not working when using Ajax - ajax

I have very interesting problem. I am making log in page for my web app and I am sending login request via AJAX. If success I want to forward user to another page.
It seems that his is what happens. I send Ajax request, controller forwards me to need view (I see log in debug mode) but I stay on the same page, since I assume the page is waiting for AJAX response and for that reason forwarding does not happen.
I think this is also wrong way to approach this but since I am new to this don't know better. How can I log in and and forward user to next page.
Thank you.
Here is my code:
JS Code:
Page.authenticate = function() {
$.ajax({
url: "/login/authenticate/" + $('#username').val() + "/" + $('#password').val(),
type: "GET",
success: function(poi){
// alert("nesto");
}
});
return true;
}
Controller Class:
#Controller
public class LoginPageController {
private Logger logger = Logger.getLogger(this.getClass());
#Autowired
private UserManagement userManagement;
#RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET)
public String authenticate(#PathVariable("username") String userName, #PathVariable("password") String password, Model model) {
if (userManagement.authenticateUser(userName, password)) {
String forward = "forward:/login/success";
return forward;
} else {
model.addAttribute("errorMessage", "Invalid Username/Password, please try again!");
return "/";
}
}
}

You need to response within #ResponseBody Annotation if you are using AJAX.
#RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET)
public String authenticate(#PathVariable("username") String userName, #PathVariable("password") String password, Model model) {
if (userManagement.authenticateUser(userName, password)) {
String forward = "forward:/login/success";
return forward;
} else {
String forward = "forward:/login/error?message=Invalid Username/Password, please try again!";
return forward;
}
}
#RequestMapping(value="/login/success", method=RequestMethod.GET)
#Responsebody
public String handleMySuccessRedirect() {
return "Logged In successfully"
}
#RequestMapping(value="/login/error", method=RequestMethod.GET)
#Responsebody
public String handleMyExceptionOnRedirect(#RequestParamter("message") String message) {
return message;
}
Update:
#RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET)
#ResponseBody
public String authenticate(#PathVariable("username") String userName, #PathVariable("password") String password, Model model) {
if (userManagement.authenticateUser(userName, password)) {
String response = "Logged Successfully";
return response;
} else {
String response = "Invalid Username/Password, please try again!";
return response;
}
}

There are a couple of things you can do here:
Don't return a view from your controller, instead return json, based on the response in json, set the location appropriately - window.location = 'home.action' - here is an example using ext-js
Let the login page perform a full fledged post, not an AJAX post.

Related

spring CRUD DELETE action that return viewmodel or empty body

I want to write a DELETE action that return a no content body if no id error exist. If id not exist I want to redirect to the coresponding GET view.
Controller code:
#RequestMapping(value = "/todo/delete/{id}", method = RequestMethod.GET)
public String getDeleteTodo(Model model, #PathVariable("id") String id)
{
Optional<Todo> todo = todoRepository.findById(Long.decode(id));
if (todo.isEmpty()) {
model.addAttribute("msginfo", "ctl-todo.delete.msginfo.id-not-exist");
model.addAttribute("requestedId", id);
}
else {
model.addAttribute("todo", todo.get());
}
return "v-todo-delete";
}
#RequestMapping(value = "/todo/delete/{id}", method = RequestMethod.DELETE)
public String deleteTodo(#PathVariable String id, RedirectAttributes redirAttrs)
{
boolean exists = todoRepository.existsById(Long.decode(id));
if (exists) {
todoRepository.deleteById(Long.decode(id));
return ""; //here I want to return a no-content body response
}
else {
redirAttrs.addFlashAttribute("msginfo", "ctl-todo.delete.msginfo.id-not-exist");
redirAttrs.addFlashAttribute("requestedId", id);
return "redirect:/todo/delete" + id;
}
}
More informations about the view:
The GET view is juste a view that display the todo entity corresponding to the id. The deletion is make with a button using ajax to call the DELETE method. Then response is return as 204 with no content into the body, i redirect the user with javascript to the main page... If an id not exist in the DELETE method, I want to redirect to the GET method to show an error message.
If someone have an idea to do this.
Thanks in advance.
Try using return type as ResponseEntity with whatever response body along with a response status. Please refer below code changes:
#RequestMapping(value = "/todo/delete/{id}", method = RequestMethod.DELETE)
public ResponseEntity deleteTodo(#PathVariable String id, RedirectAttributes redirAttrs)
{
boolean exists = todoRepository.existsById(Long.decode(id));
if (exists) {
todoRepository.deleteById(Long.decode(id));
return new ResponseEntity(HttpStatus.NO_CONTENT); //This will return No Content status
}
else {
redirAttrs.addFlashAttribute("msginfo", "ctl-todo.delete.msginfo.id-not-exist");
redirAttrs.addFlashAttribute("requestedId", id);
return new ResponseEntity( "redirect:/todo/delete" + id, HttpStatus.OK);
}
}
Final anwser for me:
#RequestMapping(value = "/todo/delete/{id}", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteTodo(#PathVariable String id, RedirectAttributes redirAttrs)
{
boolean exists = todoRepository.existsById(Long.decode(id));
if (exists) {
todoRepository.deleteById(Long.decode(id));
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
else {
redirAttrs.addFlashAttribute("msginfo", "ctl-todo.delete.msginfo.id-not-exist");
redirAttrs.addFlashAttribute("requestedId", id);
/* I use CONFLICT here to explain that the entity was possibly deleted
by another user between the moment the user give the view containing
the DELETE ajax link and the moment he click on it. */
return new ResponseEntity<String>( "redirect:/todo/delete" + id, HttpStatus.CONFLICT);
}
}
Thank you Mandar Dharurkar & Jeethesh Kotian for your help ;)

Throw custom exception from Spring controller and receive it in ajax-post error function

So, i need to add custom validation to my page, the problem is, i don't have any form, i collect and send data almost manually, here is my ajax post:
$.ajax({
type: "POST",
url: "/settings/propertyedit",
dataType: 'json',
contentType: 'application/json;charset=UTF-8',
data: {
propertyName : propName,
propertyValue : propVal,
Id : Id,
SettingId : SettingId,
},
beforeSend: function (xhr) {
xhr.setRequestHeader($.metaCsrfHeader, $.metaCsrfToken);
},
success: function (response) {
//Do some something good
},
error: function(response){
//do some something worning
}
});
And controller:
#Link(label = "property edit", family = "SettingsController", parent = "Settings")
#RequestMapping(value = "/settings/propertyedit", method = RequestMethod.POST)
#ResponseBody
public String atmpropertyedit(#RequestParam String propertyName,
#RequestParam String propertyValue,
#RequestParam Long Id,
#RequestParam Long SettingId) {
//Check if it is an error
//If correct i want to return some text in success function
//If error happens want to return some relevant text to error function
}
So, the point is, that validation is also custom, so i cant throw exception simply with try catch and if i am trying to do something like:
return new ResponseEntity<>(HttpStatus.NOT_EXTENDED);//Error type is for testing purposes
I will get 400 error even without triggering into my controller. At this point i just want some simple method to let know my ajax what has happened in my controller.
The controller can be as simple as this one, you can make it happen with custom response class which I named CommonResp and an Enum VALIDATION.
Controller - returns Response class.
#ResponseBody
public CommonResp atmpropertyedit(#RequestParam String propertyName,
#RequestParam String propertyValue,
#RequestParam Long Id,
#RequestParam Long SettingId) {
// error
if (!isValidPropertyName(propertyName)) return new CommonResp(VALIDATION.INVALID_PROPERTY_NAME);
// success
return new CommonResp(VALIDATION.OK);
}
}
CommonResp.java - will be the json response.
public class CommonResp implements Serializable {
private int code;
private String message;
public CommonResp() {
this(VALIDATION.OK);
}
private CommonResp(final int code, final String message){
this.code = code;
this.message = message;
}
public CommonResp(VALIDATION validation) {
this(validation.getCode(), validation.getMessage());
}
/* Getters and Setters */
}
VALIDATION.java
public enum VALIDATION {
OK(200, "OK"),
INVALID_PROPERTY_NAME(401, "PropertyName is not valid");
private int code;
private String message;
private VALIDATION(int code, String message) {
this.setCode(code);
this.message = message;
}
/* Getters and Setters */
}
Please let me know if there are any better implementations. (propably tons of, It's just that i don't know :P)

Spring - Stop redirection on error

I have a page to manage users and I would like to stay on the page if any error occurs when clicking save.
The only cases I found online where to do with validation.
Also my page requires the userId to be posted so I don't think returning the name of the original page in the controller would work. Also I would loose the changes made in the page.
What I am trying to achieve is stay in the same page, showing a message to the user.
Here is my controller:
#RequestMapping(method = RequestMethod.POST)
public String editUser(#RequestParam("userId") String userId, final Map<String, Object> model) {
User user = spiService.getUser(userId);
model.put("user", user);
configureRoles(model, user);
return "edituser";
}
#RequestMapping(path = "/updateUser", method = RequestMethod.POST)
public String updateUser(#RequestParam("userJson") String userRoles, #RequestParam("userId") String userId, final Map<String, Object> model) throws IOException {
User user = spiService.getUser(userId);
try {
addRoles(JsonUtil.getField(userRoles, "addedRoles"), user.getRoles(), userId);
removeRoles(JsonUtil.getField(userRoles, "removedRoles"), user.getRoles(), userId);
} catch (Exception ex) {
// What now?
}
return "users";
}
Instead of redirecting you can use Ajax calls in your controller. For that you have to create one AjaxPojoClass for exampleAjaxResponseBody as your convenience.
For example
$.ajax({
type : "POST",
contentType : "application/json",
url : "/yourUrl",
data : JSON.stringify(data),
dataType : 'json',
success : function(data) {
window.location.replace("/successUrl")
},
error : function(e) {
display(e);
},
});
AjaxController
#Controller
public class AjaxController {
#ResponseBody
#RequestMapping(value = "/yourUrl")
public AjaxResponseBody getSearchResultViaAjax(#RequestBody SearchCriteria search) {
AjaxResponseBody result = new AjaxResponseBody();
//logic
return result;
}
}
you can use ajax to submit your request.

Change url path in Spring MVC by #RequestMapping

Currently path is showing
http://localhost:8081/UserLogin/login
But i want this as
http://localhost:8081/UserLogin/index
or
http://localhost:8081/UserLogin/
My controller class is
#RequestMapping(value = "/login" ,method = RequestMethod.POST)
public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {
//return "hi this is a test";
String userName = request.getParameter("data[Admin][user_name]");
String userPass=request.getParameter("data[Admin][password]");
int userId=userDAO.getUser(userName, userPass);
if(userId!=0){
String message = "welcome!!!";
return new ModelAndView("result", "message", message);
}
else{
String message = "fail";
return new ModelAndView("index", "message",message);
}
}
Want to change in else condition when not match.
Thanks in advance. :)
I would return a redirect to render the view under the new URL:
request.addAttribute("message",message) // better use a Model
return "redirect:/[SERVLET_MAPPING]/index";
It take some time to understand what you want: - I guess you want to alter the URL that is returned from the Server after login.
But this does not work this way, because the URL is requested from the browser and the server can not change them. Instead the server can respond an "HTTP 303 Redirect" (instead of the view). This cause the Browser to load the URL given with the Redirect.
#RequestMapping(value = "/login" ,method = RequestMethod.POST)
public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {
//return "hi this is a test";
String userName = request.getParameter("data[Admin][user_name]");
String userPass=request.getParameter("data[Admin][password]");
int userId=userDAO.getUser(userName, userPass);
if(userId!=0){
return new ModelAndView(new RedirectView("/result", true)); // "/result" this is/become an URL!
}
else {
return new ModelAndView(new RedirectView("/index", true)); // "/index" this is/become an URL!
}
}
#RequestMapping(value = "/index" ,method = RequestMethod.GET)
public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {
String message = "fail";
return new ModelAndView("index", "message",message); //"index" the the name of an jsp (or other template)!!
}
#RequestMapping(value = "/result" ,method = RequestMethod.GET)
public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {
String message = "welcome!!!";
return new ModelAndView("result", "message", message); //"result" the the name of an jsp (or other template)!!
}
#See http://en.wikipedia.org/wiki/URL_redirection

Sending data from AngularJS factory to a Spring Controller

I have a spring controller which should recieve data in the sessionAttribute from an angularjs factory.
My Spring Controller is :
#Controller
#SessionAttributes("dataObject")
public class ScreenDesignerController extends BaseController {
/**
* Injected screen designer service class.
*/
#Autowired
private ScreenDesigner screendiService;
#RequestMapping(value = "FormBuilder", method = RequestMethod.POST)
public final String knowDetails(
#ModelAttribute("globalUser") User globalUser,
#RequestParam(value = "dataObject") String myJsonStr,
BindingResult result, RedirectAttributes redirectAttributes,
final Model model
) throws Exception {
try {
logger.info("this is json array: " + myJsonStr);
screendiService.addData(myJsonStr);
} catch (Exception e) {
logger.info("inside customiseForm POST catch");
}
return "ScreenDesigner/FormBuilder";
}
Angular factory:
indApp.factory('sendJsonDataService', function ($http, $rootScope, superCache) {
var sendjsondataservice = {
sendJsonData: function () {
//dataObject = JSON.stringify(superCache.get('super-cache'));
alert(JSON.stringify(dataObject));
res = $http.post('FormBuilder', dataObject);
res.success(function(data, status, headers, config) {
alert("Your Screen has been saved successfully into the database!");
});
res.error(function(data, status, headers, config) {
alert( "failure message: " + JSON.stringify({data: data}));
});
}
};
return sendjsondataservice;
});
Whenever I am invoking the factory via angularjs controller to recieve "dataObject", it says "bad request 400", Though the "dataObject" has valid json data in it.
I want my spring controller to receive this data.
Please help, stuck for two days now :(
Thanks in advance!!
If you're sending JSON as a POST payload, you should be using #RequestBody instead of #RequestParam.
Thy this i modified your controller :
#Controller
#SessionAttributes("dataObject")
public class ScreenDesignerController extends BaseController {
/**
* Injected screen designer service class.
*/
#Autowired
private ScreenDesigner screendiService;
#RequestMapping(value = "FormBuilder", method = RequestMethod.POST)
public final String knowDetails(#RequestBody String myJsonStr,#ModelAttribute("globalUser") User globalUser,
BindingResult result, RedirectAttributes redirectAttributes,
final Model model
) throws Exception {
try {
logger.info("this is json array: " + myJsonStr);
screendiService.addData(myJsonStr);
} catch (Exception e) {
logger.info("inside customiseForm POST catch");
}
return "ScreenDesigner/FormBuilder";
}
Another this be sure to send json data from AngularJS factory. For instance :
headers: {
'Content-type': 'application/json'
}

Resources