How to map the hibernate to the JSP view? - java

Following is a folder hierarchy in the Spring boot Hibernate project ,
This is a method in UserResource Controller
#GetMapping("/")
public ModelAndView home(HttpServletRequest request){
//request.setAttribute("mode", "MODE_HOME");
ModelAndView model = new ModelAndView("index");
model.addObject("msg", "hello world");
return model;
}
Following code also added to the application.properties file to find the correct jsp page.
spring.mvc.view.prefix:WebApp/Web/
spring.mvc.view.suffix:.jsp
But this is not worked and following error is occurred.
Anyway, I tried many ways by modifying the path in the 'application.properties' file and couldn't find the right solution. Are there any steps to fire the jsp view?

If your main class don't extends SpringBootServletInitializer ,try this.
public class SpringHibernateAssignmentApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringHibernateAssignmentApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringHibernateAssignmentApplication.class, args);
}
}

Related

SpringBoot White page

I just start learning SpringBoot.
I use spring boot build-in tomcat get my spring boot program run. But when I try to visit the page, it gives me a Whitelabel Error Page.
When I start this program, it shows as follow:
I think my program and tomcat start successfully.
This is my start code:
DemoApplication.java
#SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
This is my Controller code:
BookController.java
#Controller
public class BookController {
private IBookService bookService;
public BookController(IBookService bookService) {
this.bookService = bookService;
}
#RequestMapping(value = "/book_list", method = RequestMethod.GET)
public String getAllBook(Model model, HttpSession httpSession, HttpRequest httpRequest) throws Exception {
List<Book> list = bookService.getAllBook();
model.addAttribute("bookList", list);
return "book";
}
}
So if I visit 'localhost:8080/bookstore/book_list', it will find the controller and this controller should help me go to the /WEB-INF/jsp/book.jsp because my WebMvcConfig like following:
WebMvcConfig.java
#Configuration
public class WebMvcConfig {
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
}
But why this is a white page?
This is my program structure:
DemoApplication is in a demo subpackage. The default is that it only scans subpackages of the class annotated with #SpringBootApplication, so it doesn't find any of your components.
Two ways to fix:
Move the class out of the demo package.
This is what Spring Boot demo applications usually do.
Specify the packages to scan:
#SpringBootApplication(scanBasePackages = "com.zx")
Alternatively, use #ComponentScan, same thing:
#SpringBootApplication
#ComponentScan("com.zx")

Spring Boot Context Path Ignored With External Tomcat

I have set the context path for tomcat as follows:
#Component
public class CustomContainer implements
WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
#Override
public void customize(TomcatServletWebServerFactory factory) {
factory.setContextPath("/capripol");
factory.setPort(8080);
}
}
Navigating to localhost:8080/capripol works fine and I am prompted with my login screen, however after logging in my forms and controllers do not append to the context path, so instead of navigating to /capripol/MainMenu etc. they navigate to /MainMenu. How do I set the context path such that my form actions and controllers will be appended do it - why is the tomcat factory context path not setting?
Edit: My Application class
#SpringBootApplication
public class CapripolApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(CapripolApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(CapripolApplication.class);
}
#Configuration
public class WebConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/", "classpath:/images/")
.setCachePeriod(0);
}
}
}
A few ways to do it. You can add it to each controller, usefully if you want to change the context path
#Controller
#RequestMapping(value = "/foo")
public class bar{
#GetMapping(value = "/bar")
public void stuff(){
//doing stuff
}
}
Or you can put it in your application.properties / yml
server.servlet.contextPath=/foo/*
There are technically some other more round about ways to do it, especially if you are using an older version of Spring, but I would think the application properties is what you are looking for.

404 Not Found after deploy java web application .war file

I'm wrote a web application in java using Spring framework. Tested it and deployed to remote tomcat server. After deploying I have message OK - Started application at context path [/proxynator]. But, if I use links like http://109.206.178.66:8080/proxynator/ and http://109.206.178.66:8080/proxynator/proxynator/test I have 404 – Not Found and Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
In Application I have starter class
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
and controller
#RestController
#RequestMapping("/proxynator")
public class MainController {
#Autowired
private ProxyRepo proxyRepo;
#Autowired
private CountryRepo countryRepo;
#RequestMapping("/countries")
#ResponseBody
public List<Country> findCountries() {
return countryRepo.findAll();
}
#RequestMapping("/test")
#ResponseBody
public String testMethod() {
return "HELLO";
}
}
I don't know, why I have this problem, because I setting up my tomcat server right, path to my controller is right and application on server is running.
Any ideas how to solve it?
UPD
I was changed my controller like:
#RestController
public class MainController {
#Autowired
private CountryRepo countryRepo;
#RequestMapping("/countries")
#ResponseBody
public List<Country> findCountries() {
return countryRepo.findAll();
}
#RequestMapping("/")
#ResponseBody
public String testMethod() {
return "HELLO";
}
}
And now my enter point is / that calling testMethod(), but it doesn't working too.
To solve this problem I was extends SpringBootServletInitializer and override configure method`
#Override
override fun configure(application: SpringApplicationBuilder?):
SpringApplicationBuilder {
return super.configure(application)
}
and I changed project structure like in official documentation. And now it works good.
PS
I was rewrite project to Kotlin

No mapping found on Spring-Boot

I'm new a spring-boot and spring framework. According to me, web app create and deploy very easy with spring-boot but when i run my sample spring web app, application not found "welcome.html" page. I checked all similar question on stackoverflow and not worked me. I cannot see little issue but I didnt find my problem. My application structure and codes are below:
MyApplication class is below:
#SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
WelcomeController class is below:
#Controller
public class WelcomeController {
#RequestMapping(value = "/welcome")
public String welcome() {
return "welcome"; //<- this is your login.html if it is directly in resource/templates folder
}
}
application.properties file is below:
spring.mvc.view.prefix = templates/
spring.mvc.view.suffix = .html
spring.mvc.static-path-pattern=/resources/**
WebMvcAppConfig class is below:
public class WebMvcAppConfig extends WebMvcConfigurerAdapter {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry); //To change body of generated methods, choose Tools | Templates.
registry.addViewController("/welcome").setViewName("welcome.html");
}
}
Firstly thanks a lot for quickly response my question #Andy Wilkinson and georges van. I looked for in spring boot reference guide and Serving Web Content with Spring MVC and I learned a lot of information about spring-boot. I removed WebMvcAppConfig because this class not necessary for starter and removed SpringBootServletInitializer. I moved html files into templates as you say. I keep simple and application run without issues.

What should the template path be when using Spring Boot and a Sitemesh filter?

I'm trying to make use of Sitemesh (3) templating with Spring Boot (4+) using Java annotation based config.
When I hit the controller URL, the handler method is invoked.
The Sitemesh filter is activated (debugging proves that).
However I am getting a 404, which I believe is because with the config I have the Freemarker template isn't found (wrong path somewhere).
Code follows, any suggestions what I'm doing wrong would be great!
Filter:
#WebFilter
public class SitemeshFilter extends ConfigurableSiteMeshFilter {
#Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
System.out.println("in sitemesh filter");
builder.addDecoratorPath("/*", "templates/main.ftl")
.setMimeTypes("text/html")
.addExcludedPath("/javadoc/*")
.addExcludedPath("/brochures/*");
}
Controller:
#Controller
public class UserController {
#Autowired
MemberService memberService;
#RequestMapping(value="member/{id}")
public ModelAndView viewMember(#PathVariable("id") int memberId, ModelAndView mv) {
mv.setViewName("member");
ClubMember member = memberService.getClubMember(memberId);
mv.addObject("member", member);
return mv;
}
}
Main class:
#SpringBootApplication
#ServletComponentScan
#EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
public class ClubManagementApplication {
public static void main(String[] args) {
SpringApplication.run(ClubManagementApplication.class, args);
}
}
Application.properties:
spring.mvc.view.prefix=/views/
My templates live in :
src/main/resources/templates <- this is where I've put the sitemesh templates live
src/main/resources/views <- here are the Freemarker pages
In case anyone else has same issue:
templates ended up in resources/templates
sitemeshfilter:
#Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
builder.addDecoratorPath("/*", "/main.ftl")
.setMimeTypes("text/html", "application/xhtml+xml", "application/vnd.wap.xhtml+xml");
}
Note nothing before '/main.ftl'

Categories