Second Controller can't find .html - java

First controller works correctly,
#Controller
#ComponentScan
#EnableAutoConfiguration
public class MainController {
#RequestMapping("/profile")
private String g(){
return "Profile.html";
}
}
Second:
#Controller
#RequestMapping(value = "/gallery")
public class GContoller {
#RequestMapping(value = "/month")
String ffff() {
return "monthGallery.html";
}
#RequestMapping("/test")
#ResponseBody
String test() {
return "Test";
}
}
Page localhost:8080/gallery/test opens and display "test", but localhost:8080/gallery/month opens with 404 error. Obviosly, that 2nd controller can't find file monthGallery.html. All htmls are in /webapp.
Project structure
/java
/com.example.xxx
/webapp

Related

#RestController always return 404 when called

I have two controllers (UserController and ClientController), both of the controllers are located on the same package (com.myapp.controllers.identity), and my main application file located on the parent package (com.myapp).
I create the ClientController first and it works fine. Later on, I create the UserController. When I called the UserController, it always returns 404.
Here is the snippet of my controllers' files and main application file
Application.java
package com.myapp;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
ClientController.java
package com.myapp.controllers.identity;
#RestController
#RequestMapping(value = "/api/identity")
#Validated
public class ClientController {
#GetMapping(value = "/clients/{clientId}")
public ResponseEntity<?> getClientByClientId(#PathVariable("clientId") String clientId) {
}
}
UserController.java
package com.myapp.controllers.identity;
#RestController(value = "UserController")
#RequestMapping(value = "/api/identity")
#Validated
public class UserController {
public static final Logger logger = LoggerFactory.getLogger(UserController.class.getName());
#Autowired
private UserService userService;
#GetMapping(value = "/users/client/:clientId")
public ResponseEntity<?> getAllUsersByClientId(#PathVariable String clientId)
{
}
}
Can anybody help me solve it?
There is one simple problem in your userControler, and that is #GetMapping(value = "/users/client/:clientId") your format of capturing parameter
this kind of parameter is not supported in spring as #chrylis -on strike- mentioned
package com.myapp.controllers.identity;
#RestController(value = "UserController")
#RequestMapping(value = "/api/identity")
#Validated
public class UserController {
public static final Logger logger = LoggerFactory.getLogger(UserController.class.getName());
#Autowired
private UserService userService;
#GetMapping(value = "/{clientId}")
public ResponseEntity<?> getAllUsersByClientId(#PathVariable String clientId)
{
}
}

Spring Boot controller does not redirect

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

How to handle Angularjs routing in Spring MVC?

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");
}
}

#RestController does not work, it still return me a jsp page

Controller:
#RestController
public class CommodityController {
#RequestMapping("test.htm")
public String test() {
return "test";
}
}
Spring version:
<org.springframework.version>4.2.0.RELEASE</org.springframework.version>
Still not working:
Can I use #RestController like this?

Spring MVC #RequestMapping ... using method name as action value?

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

Categories

Resources