If I need only .jsp as a view in my web application, then I write something like this:
#EnableWebMvc
#ComponentScan("com.test")
public class WebConfig extends WebMvcConfigurerAdapter {
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
}
Is it possible to use .html as well? For instance:
#Controller
public class HelloController {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello"; // uses jsp extension
}
}
#Controller
public class HomeController {
#RequestMapping(value = "/home", method = RequestMethod.GET)
public String home() {
return "home"; // uses html extension
}
}
Related
I have the same problem as mentioned in
no mapping for GET
but didn't work for me.
this is the structure :
resource structure in my project
and this is my config class :
#Configuration
#EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
and this is my controller:
#RequestMapping("/")
public ModelAndView getHomepage() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("home.html");
return modelAndView;
}
and I have this in my html file:
<link rel="stylesheet" href="/css/home.css" />
but still get the error:
No mapping for GET /css/home.css
No mapping for GET /js/home.js
would be appreciated for any suggestions
thank you
this is youre config
#Configuration
#EnableWebMvc
#ComponentScan("directory with youre getHomepage() controller ")
public class MvcConfig implements WebMvcConfigurer {
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver =
new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/"); //youre directory with home.jsp
resolver.setSuffix(".jsp");
return resolver;
}
}
this is youre controller
#Controller
#RequestMapping({"/"})
public class HomeController {
#RequestMapping(method=GET)
public String home() {
return "home";
}
}
Getting this error while returning Html page in response from controller
org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringMVCDemo/WEB-INF/response.html] in DispatcherServlet with name 'dispatcher'
where to put response.html?
Here is my code
Controller class
-----------------
#Controller
public class LoginController {
#RequestMapping("/login")
public ModelAndView login(#RequestParam(name = "username") String username,
#RequestParam(name = "password") String password){
String str = "Welcome " + username;
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("response");
modelAndView.addObject("response",str);
return modelAndView;
}
}
Config file
------------
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = "com.skuba.*")
public class MvcConfig extends WebMvcConfigurerAdapter{
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/");
viewResolver.setSuffix(".html");
return viewResolver;
}
}
This is dispatcher controller
------------------------------
public class FrontController extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {MvcConfig.class};
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[0];
}
#Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
Assuming the view files are in the right directory(i.e., "/WEB-INF/") try adding the following to the MvcConfig class.
// Use the DefaultServletHandlerConfigurer
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
I'm trying little spring project and encounter a problem if i use xml configuration file (web.xml and spring-servlet.xml) everything is fine. If i use java annotation the problem come up :(
I'm so confuse why intellij idea not showing my web views in my webapp folder.
Not show jsp file (pressed Ctrl + space)
And cannot resolve any variable name in my controller class Cannot resolve var name
### Controller CLASS ###
#Controller
public class HomeController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String home(ModelMap modelMap) {
modelMap.addAttribute("message", "Hello, from java class");
return "welcome";
}
}
Configuration Class
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = "com.toskbilisim")
public class HomeConfiguration extends WebMvcConfigurerAdapter {
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
#Bean
public MessageSource messageSource(){
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
}
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 have a problem with mappings and ModelAndView. All simply mappings like "/login" works but I have problem with:
#RestController
#RequestMapping("/note/{noteId:[\\d]+}")
public class NoteController {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView note(#PathVariable String noteId, HttpSession session) {
// not important code
ModelAndView modelAndView = new ModelAndView("note");
return modelAndView;
}
}
I have an error here and path: /note/WEB-INF/views/jsp/note.jsp.
I wanted to get WEB-INF/views/jsp/note.jsp and I have similar path in other ModelAndViews (without prefix).
My configuration:
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/views/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
M. Deinum solved this problem.
Replace #RestController with #Controller
#RestController returns everything as a response body, it automatically returns your ModelAndView as JSON or XML. #Controller does proper view resolution. Also your prefix should be /WEB-INF/views/jsp/. Include the leading /.