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");
}
}
Related
I create the following controller:
#Controller
public class RootController {
#RequestMapping(path = "/login", method = RequestMethod.GET)
public String showLoginPage() {
return "redirect:resources/templates/login.html";
}
}
But, trying to map this endpoint, I receive 404-ERROR.
This is a target-page location:
What am I doing wrong?
#Controller
public class RootController {
#GetMapping("/login")
public String showLoginPage() {
return "login";
}
It works to me.I hope hepls you!
#Controller
public class RootController {
#GetMapping("/login")
public String showLoginPage() {
return "login.html";
}
This will work
I am new to spring MVC and facing some error.
I have two controllers as below
1) LoginController.java
#Controller
#RequestMapping("/log")
public class LoginController {
#Autowired
private LoginService service;
#RequestMapping(value="login.spring",method=RequestMethod.GET)
public ModelAndView prepareLoginForm()
{
System.out.println("In get");
return new ModelAndView("Login", "login", new Login());
}
#RequestMapping(value="login.spring",method=RequestMethod.POST)
public ModelAndView processLogin(#ModelAttribute("login") Login login,BindingResult result)
{
int i=service.validateLogin(login);
if(i==0){
return new ModelAndView("redirect:login.spring");
}
ModelAndView view=new ModelAndView("redirect:Customer/Searchform.spring");
return view;
}
}
2) CustomerController.java
#Controller
#RequestMapping("/Customer")
public class CustomerController {
#Autowired
private CustomerService customerService;
#RequestMapping(value="Searchform.spring",method=RequestMethod.GET)
public ModelAndView prepareCustomer()
{
System.out.println("In customer controller");
CustomerSearchForm customerSearchForm=new CustomerSearchForm();
return new ModelAndView("CustomerSearch","customerSearchForm",customerSearchForm);
}
#RequestMapping(value="Search.spring",method=RequestMethod.POST)
public ModelAndView searchCustomer(#ModelAttribute("customer") CustomerSearchForm customerSearchForm,BindingResult result)
{
int i=customerService.serachCustomer(customerSearchForm);
if(i==1)
return new ModelAndView("Holdings");
return new ModelAndView("redirect:Customer");
}
}
So after successful login I am trying to redirect to CustomerController but in
browser url i can see that request url is
http://localhost:8080/Online_Fund_Trading/log/Customer/Searchform.spring.
As log gets added before Customer/Searchform.spring I am getting 404-The requested resource is not available error.
What changes are required to have request url as http://localhost:8080/Online_Fund_Trading/Customer/Searchform.spring.
A simple slash / is required
ModelAndView view=new ModelAndView("redirect:/Customer/Searchform.spring");
Otherwise the path will be considered relative to the path of the request you are currently handling.
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.
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
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