i want some thing like this in spring mvc, url/varible means like this. www.something.com/1 or /2 etc. what is the code in the controller class, and why we always implement the get method in the controller class
Asked
Active
Viewed 187 times
1
-
1Typically questions end with this punctuation "?" and actually ask something that can be answered. – Woot4Moo Dec 09 '10 at 21:43
1 Answers
1
To get the variable from the url you have to use @RequestMapping annotation
@RequestMapping(value="/url/{varible}", method=RequestMethod.GET)
public String controllerMethod(@PathVariable("varible") String theVariable, Model model) {
//I get the variable in the variable
...
}
Regarding why "we always implement the get method in the controller" I don't know exactly what you mean. If you mean that is used Get as a retreive method it is because GET is usually used (or should be used) for retrieving information while POST is for operations which involves changing data in the server. So you may have seen a method that gets information. There are other differences you can find in many of the questions involving this topic in stackoverflow such as this or this other.
-
actually i want to know that, in the annotation base controller class, if we dosn't implement the Request mapping GET method there comes an error when we call that page via any link. thats what i am asking that is it always necessary to implement the GET method for such calling. – aditya Dec 10 '10 at 05:08
-
if you use @RequestMapping(value="/url/{varible}", method=RequestMethod.GET) this request can only be done via GET, if you use @RequestMapping(value="/url/{varible}") it can be done by any method (you don't restrict it to GET). It is no neccesary to implement it, though it is a good practice to restrict it just to GET when you read data and to PUT when you write data. – Javi Dec 10 '10 at 07:59