I am new to spring mvc3 development and was facing a minor issue (which I was didn't face with ASP.Net MVC3). I want to know the process of defining a default (or landing) URL for a controller.
I have an accounts controller where I do all account management related stuff. So all my urls are mapped to this controller. I want to know that how can I map my "/accounts" url request to hit openAccountsDashboard method?
Code -
.... imports...
#Controller
#RequestMapping(value = "/accounts/*")
public class AccountController {
#RequestMapping( value = "/", method = RequestMethod.GET)
public ModelAndView openAccountsDashboard(HttpServletRequest request) {
.....
return new ModelAndView("accounts/landing");
}
#RequestMapping( value = "/change-password", method = RequestMethod.GET)
public ModelAndView openPasswordChangePage(HttpServletRequest request) {
.....
return new ModelAndView("accounts/passwordChange");
}
... other actions...
}
Any help would be great!
Thanks
Try something like this:
.... imports...
#Controller
#RequestMapping(value = "/accounts/")
public class AccountController {
#RequestMapping( value = "", method = RequestMethod.GET)
public ModelAndView openAccountsDashboard(HttpServletRequest request) {
.....
return new ModelAndView("accounts/landing");
}
#RequestMapping( value = "/change-password", method = RequestMethod.GET)
public ModelAndView openPasswordChangePage(HttpServletRequest request) {
.....
return new ModelAndView("accounts/passwordChange");
}
... other actions...
}
Then you can use url like this:
http://localhost:8080/yourwebapp/accounts/
to hit openAccountsDashboard method.
Regards,
Arek
Related
This is my Project structure:-
The following is my ngRoute code:-
mainApp.config(function($routeProvider) {
$routeProvider
.when('/', {
//Homepage
templateUrl: 'main.html',
controller: 'directoryController'
})
.when('/edit', {
//Edit Templates
templateUrl: 'details.html',
controller: 'tempUnguidedEditController'
})
});
This is my Spring controller class :-
#Controller
public class MainController {
#RequestMapping(value = "/home.html", method = RequestMethod.GET)
public ModelAndView home() {
return new ModelAndView("index");
}
#RequestMapping(value = "/dashboard.html")
public ModelAndView dashboard() {
return new ModelAndView("dashboard");
}
}
When I run the dashboard.html link it gives an error on console log:-
When I run the my web pages separately on browser it works but when I integrate it with Spring it fails to locate some files. I guess I am doing something wrong in the project structure which is why it is not able to find main.html file.
I got it to work by,
i) Converting main.html,guided.html and unguided.html to main.jsp,guided.jsp and unguided.jsp.
ii) Then i intercepted the requests for all of the three individual routes in my MainController class.
#Controller
public class MainController {
#RequestMapping(value = "/home.html", method = RequestMethod.GET)
public ModelAndView home() {
return new ModelAndView("index");
}
#RequestMapping(value = "/dashboard.html")
public ModelAndView dashboard() {
return new ModelAndView("dashboard");
}
#RequestMapping(value = "/main.html")
public ModelAndView main() {
return new ModelAndView("main");
}
#RequestMapping(value = "/guided.html")
public ModelAndView guided() {
return new ModelAndView("guided");
}
#RequestMapping(value = "/unguided.html")
public ModelAndView unguided() {
return new ModelAndView("unguided");
}
}
I'm struggling with this strange behavior of This webpage has a redirect loop, I have a link in home page
<a href='<spring:url value="/url?view=offers" />'>offers</a>
and my controller is
#Controller
public class OfferController {
#Autowired
private OfferService offerService;
#RequestMapping(value = "/url", method = RequestMethod.GET)
public String showOffers(#RequestParam("view") String view, Model model) {
model.addAttribute("offers", offerService.findAll());
return "redirect:/url?view=offers";
}
}
this above code is causing the issue, what is wrong with above code.
You have an /url URL mapping, and then after access /url, the code redirect it to /url?view=offers. That is the reason you get a redirect loop.
Maybe this is your purpose:
#Controller
public class OfferController {
#Autowired
private OfferService offerService;
#RequestMapping(value = "/url", method = RequestMethod.GET)
public String showOffers(#RequestParam(value = "view", defaultValue = "offers") String view, Model model) {
model.addAttribute("offers", offerService.findAll());
return "url";
}
}
Is it possible in Spring MVC to have void handler for request?
Suppose I have a simple controller, which doesn't need to interact with any view.
#Controller
#RequestMapping("/cursor")
public class CursorController {
#RequestMapping(value = "/{id}", method = PUT)
public void setter(#PathVariable("id") int id) {
AnswerController.setCursor(id);
}
}
UPD
#Controller
#RequestMapping("/cursor")
public class CursorController {
#RequestMapping(value = "/{id}", method = PUT)
public ResponseEntity<String> update(#PathVariable("id") int id) {
AnswerController.setCursor(id);
return new ResponseEntity<String>(HttpStatus.NO_CONTENT);
}
}
you can return void, then you have to mark the method with
#ResponseStatus(value = HttpStatus.OK) you don't need #ResponseBody
#RequestMapping(value = "/updateSomeData" method = RequestMethod.POST)
#ResponseStatus(value = HttpStatus.OK)
public void defaultMethod(...) {
...
}
Only get methods return a 200 status code implicity, all others you have do one of three things:
Return void and mark the method with #ResponseStatus(value = HttpStatus.OK)
Return An object and mark it with #ResponseBody
Return an HttpEntity instance
Also refer this for interesting information.
Say I have this:
#RequestMapping(value="/hello")
public ModelAndView hello(Model model){
System.out.println("HelloWorldAction.sayHello");
return null;
}
Is it possible to skip the value="hello" part, and just have the #RequestMapping annotation and have spring use the method name as the value, similar to this:
#RequestMapping
public ModelAndView hello(Model model){
System.out.println("HelloWorldAction.sayHello");
return null;
}
Thanks!
===================EDIT=====================
Tried this but not working:
#Controller
#RequestMapping(value="admin", method=RequestMethod.GET)
public class AdminController {
#RequestMapping
public ResponseEntity<String> hello() {
System.out.println("hellooooooo");
}
}
Try to add "/*" on the request mapping value of the class
#Controller
#RequestMapping(value="admin/*")
public class AdminController {
#RequestMapping
public ResponseEntity<String> hello() {
System.out.println("hellooooooo");
}
}
You can go the page http://localhost:8080/website/admin/hello
It should work if you move the RequestMethod on your specific method:
#Controller
#RequestMapping(value="admin")
public class AdminController {
#RequestMapping(method=RequestMethod.GET)
public ResponseEntity<String> hello() {
System.out.println("hellooooooo");
}
}
and access it through http://hostname:port/admin/hello
Have a look here: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping
Good luck
I have a controller which has a few actions, which are triggered by hitting various buttons on the page. I would like to have a default action, but am unsure how to annotate the method. Here is an example:
#Controller
#RequestMapping("/view.jsp")
public class ExampleController {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView displayResults() {
ModelAndView mav = new ModelAndView("view");
mav.addObject("queryResults", methodToQueryDBForListing());
return mav;
}
#RequestMapping(method = RequestMethod.POST, params="submit=Action 1")
public ModelAndView action1(#RequestParam("selectedItemKey") String key) {
ModelAndView mav = new ModelAndView("action1");
//Business logic
return mav;
}
#RequestMapping(method = RequestMethod.POST, params="submit=Action 2")
public ModelAndView action2(#RequestParam("selectedItemKey") String key) {
ModelAndView mav = new ModelAndView("action2");
//Business logic
return mav;
}
//A few more methods which basically do what action1 and action2 do
}
How can I annotate a method which will act on a POST with any submit button pressed with no key selected?
I have tried:
#RequestMethod(method = RequestMethod.POST, params="!selectedItemKey")
#RequestMethod(method = RequestMethod.POST)
I'd really hate it if I had to set required = false on each of the methods which take RequestParams and then conditionally check to see if one comes in or not... Is there a way to annotate this to work properly?
I would make this a path variable rather than a parameter, and would get rid of the .jsp:
#RequestMapping("/view")
...
#RequestMapping("/action1")
#RequestMapping("/action2")
#RequestMapping("/{action}")
It is more "restful".
The proper annotation is:
#RequestMapping(
method = RequestMethod.POST,
params = { "!selectedItemKey", "submit" }
)
It seems odd though, that it was not hitting this method until adding that second parameter.
I'm not so familiar with annotated spring-mvc but I can remember that when extending a MultiActionController you can specify a default entry point by defining the following spring config:
<bean name="myController"
class="foo.bar.MyController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="defaultMethodName" value="init" />
</bean>
</property>
package foo.bar
public class MyController extends MultiActionController {
/**
* Called when no parameter was passed.
*/
public ModelAndView init(HttpServletRequest request,
HttpServletResponse response) {
return new ModelAndView("myDefaultView");
}
/**
* action=method1
*/
public void method1(HttpServletRequest request,
HttpServletResponse response) {
return new ModelAndView("method1");
}
/**
* action=method2
*/
public void method2(HttpServletRequest request,
HttpServletResponse response) {
return new ModelAndView("method2");
}
}
So maybe in this instance you could solve this by configuring your controller over spring config instead of using annotations.