Spring MVC RequestMapping PathVariable in the first place - spring

I would like to set for each user's own profile link.
Like this:
#RequestMapping(value = "/{userlogin}", method = RequestMethod.GET)
public String page(#PathVariable("userlogin") String userlogin, ModelMap model) {
System.out.println(userlogin);
return "user";
}
But static pages get this expression too..
Like this:
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
System.out.println("hello mapping");
return "hello";
}
That is when I request GET request "hello" that calls both controllers.
I would like to do that, user controller calls only if other methods not called.
Console, when I calls localhost:8080/123:
123
Console, when I calls localhost:8080/hello:
hello
hello mapping
or
hello mapping
hello
I want to get only
hello mapping
when calls localhost:8080/hello
Who knows how it can be implemented?

Spring MVC can use URI Template Patterns with Regular Expressions. Provided :
userlogin only contain digits
others URL immediately under root contains at least one non digit character
you can use that in your #RequestMapping :
#RequestMapping(value = "/{userlogin:\\d+}", method = RequestMethod.GET)
public String page(#PathVariable("userlogin") String userlogin, ModelMap model) {
//...
}
If the separation between userlogin and other URL is different from what I imagined, it can be easy to adapt.

Related

Error 400 when receiving data from URL parameters en Spring MVC

I am trying to receive data from an URL with two parameters like this one:
http://localhost:80000/xxx/xxx/tickets/search?codprovincia=28&municipio=110000
No matter the approach, I am always getting a 400 error, but if I access the URL without the two parameters, the controller returns the view correctly (without the parameters, naturally)
This is the code of my controller:
#Controller
#RequestMapping(value = "/xxx" )
public class BuscadorIncidenciasController extends BaseControllerWeb {
#RequestMapping("tickets")
public String tickets(Model model, #RequestParam ("codprovincia") String codprovincia, #RequestParam ("municipio") String municipio, HttpServletRequest request) throws NoAjaxException {
//...
return CONST.JSP_VIEW;
}
...}
Extra info: if I use this URL:
http://localhost:9081/xxx/xxx/tickets/search/28/790000
And this code:
#Controller
#RequestMapping(value = "/xxx" )
public class BuscadorIncidenciasController extends BaseControllerWeb {
#RequestMapping(value = "buscar/{codprovincia}/{municipio}", method = RequestMethod.GET)
public String buscar(#PathVariable Integer codprovincia, #PathVariable Integer municipio ,Model model, HttpServletRequest request) throws NoAjaxException {
//...
return CONST.JSP_VIEW;
}
...}
It gets the parameters correctly. The problem is that I have to use the first URL. I have reviewed similar questions about similar issues, and I have implemented the solutions to those issues, but I get the 400 error regardless what I try (add value="xxx=, required=false, and other suggestions.)
For RequestParam, you need to explicitly add 'name' attribute
#RequestParam(name = "codprovincia"), #RequestParam (name = "municipio")
No need to for HttpServletRequest, unless you have reason
Also, in your 'tickets' method, RequestMapping is not conforming to your URL path.
I think it should be
#RequestMapping("/xxx/tickets/search")
Cheers!

How to handle two jsp forms pages in Spring MVC controller class

I have two different jsp pages one is login.jsp and form.jsp.i want to build application like ones login success then form page will open. here i am handle two jsp pages but it will show ambiguity problem.
#RequestMapping(value = "/", method = RequestMethod.GET)
public String loginModel(Model model){
model.addAttribute("loginBean",new LoginBean());
return "login";
}
#RequestMapping(value = "/", method = RequestMethod.GET)
public String model(Model model){
//FrontBean fBean=new FrontBean();
model.addAttribute("frontBean",new FrontBean());
return "form";
}
Here is your login page, it is your welcome page also
#RequestMapping(value = "/", method = RequestMethod.GET)
public String loginModel(Model model) {
model.addAttribute("loginBean", new LoginBean());
return "login";
}
then you should not add the same path to another method. change your other method to like this (change the mapping value to the second method)
#RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String model(Model model){
//FrontBean fBean=new FrontBean();
model.addAttribute("frontBean",new FrontBean());
return "form";
}
You also need business logic to check the username and password. thereafter redirect to your welcome page like this. (your login.jsp should send the username and password to /check-user in post method)
#PostMapping("/check-user")
String checkUser(#RequestParam("userName") String userName , #RequestParam("passWord") String passWord){
if(userName.equals("Your username") && passWord.equals("Your password")){
return "redirect:welcome";
}
return "error";
}
Remember this is not a secure way to implement. it is just an example of an easy understanding. you can implement your own things. best of luck

Spring - Query parameters without question mark

I'm having an issue parsing an URL with Spring.
My endpoint is
#RequestMapping(path = "/register", method = RequestMethod.GET)
public String userActivation(#RequestParam("token") String token, #RequestParam("code") String code, final Map<String, Object> model) {
...
}
So I am expecting a token and a code in the URL.
The problem I am facing is that the service redirecting to my page omits the question mark, something like:
http://myapp/register/&token=sdgddfs&code=fdasgas
Which Spring fails to match to my endpoint.
Is there any way to handle this?
You can re-write you method using #PathVariable instead of #RequestParam
So you'll have an URL like http://myapp/register/sdgddfs/fdasgas, and an annotation for method
#RequestMapping(path = "/register/{token}/{code}")
public String userActivation(#PathVariable("token") String token, #PathVariable("code") String code) { ... }

Request parameters in spring

I need to take two parameters in my spring controller.
http://mydomain.com/myapp/getDetails?Id=13&subId=431
I have controller which will return Json for this request.
#RequestMapping(value = "/getDetails", method = RequestMethod.GET,params = "id,subId", produces="application/json")
#ResponseBody
public MyBean getsubIds(#RequestParam String id, #RequestParam String subId) {
return MyBean
}
I am getting 400 for when i tried to invoke the URL. Any thoughts on this?
I was able to get it with one parameter.
Try specifying which parameter in the query string should match the parameter in the method like so:
public MyBean getsubIds(#RequestParam("id") String id, #RequestParam("subId") String subId) {
If the code is being compiled without the parameter names, Spring can have trouble figuring out which is which.
As for me it works (by calling: http://www.example.com/getDetails?id=10&subId=15):
#RequestMapping(value = "/getDetails", method = RequestMethod.GET, produces="application/json")
#ResponseBody
public MyBean getsubIds(#RequestParam("id") String id, #RequestParam("subId") String subId) {
return new MyBean();
}
P.S. Assuming you have class MyBean.

Redirect with GET needs relative path, why?

I am very new to Spring MVC and am seeing a rather trivial behavior I don't understand.
Bellow you can find snippets to my Controller (consider I have feed.jsp and feedList.jsp). What I don't understand is why I need the "../list" in one redirect, when the other works without it
#Controller
#RequestMapping("/feed/*")
public class FeedController {
#RequestMapping(value = "delete/{feedId}", method = RequestMethod.GET)
public String deleteFeed(#PathVariable("feedId") Integer feedId) {
feedService.delete(feedId);
return "redirect:../list";
}
#RequestMapping(value = "save", method = RequestMethod.POST)
public String saveFeed(#ModelAttribute("feed") Feed feed, BindingResult result) {
feedService.create(feed);
return "redirect:list";
}
}
Perhaps the UrlBasedViewResolver is handling view names as relative to the current request mapping url (citation needed).
Anyway, I always use context-relative absolute paths (starting with a slash): redirect:/list. Actually, if your jsp is called "feedList", then your should return redirect:/feedList

Resources