简体   繁体   English

如何为 JSP 配置 spring boot mvc 应用程序?

[英]How to configure spring boot mvc app for JSP?

I am new to Spring boot ( and servlet 3.0 ).我是 Spring Boot(和 servlet 3.0)的新手。 I am trying to create spring mvc project with JSP as view.我正在尝试使用 JSP 作为视图创建 spring mvc 项目。 When I return a view from my controller it is not getting resolved as JstlView.当我从控制器返回一个视图时,它没有被解析为 JstlView。

Here is what I did:这是我所做的:

@SpringBootApplication
public class MyApp extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }

}

@Controller
public class MainController {

    @RequestMapping( value="/main" , method = RequestMethod.GET  )
    public String main(){
        return "main";
    }

    @RequestMapping( value="/" , method = RequestMethod.GET  )
    public String welcome(){
        return "welcome";
    }
}

Created both .jsp files in src\\main\\webapp\\WEB-INF\\jsp .src\\main\\webapp\\WEB-INF\\jsp创建了两个 .jsp 文件。

After googling I found that I need to specify this in application.properties So I added following in props:谷歌搜索后,我发现我需要在 application.properties 中指定它,所以我在 props 中添加了以下内容:


spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp


logging.level.org.springframework: TRACE
logging.level.com: TRACE

Even after this it is not working.即使在此之后它也不起作用。 Here is the trace.这是踪迹。

2016-04-24 19:54:49.016 TRACE 7880 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Invoking [MainController.welcome] method with arguments []
2016-04-24 19:54:49.016 TRACE 7880 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Method [welcome] returned [welcome]
2016-04-24 19:54:49.020 DEBUG 7880 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2016-04-24 19:54:49.020 DEBUG 7880 --- [nio-8080-exec-1] o.s.w.servlet.view.BeanNameViewResolver  : No matching bean found for view name 'welcome'
2016-04-24 19:54:49.022 DEBUG 7880 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory     : Invoking afterPropertiesSet() on bean with name 'welcome'
2016-04-24 19:54:49.022 TRACE 7880 --- [nio-8080-exec-1] o.s.w.s.v.InternalResourceViewResolver   : Cached view [welcome]
2016-04-24 19:54:49.022 DEBUG 7880 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.InternalResourceView: name 'welcome'; URL [/WEB-INF/jsp/welcome.jsp]] based on requested media type 'text/html'
2016-04-24 19:54:49.022 DEBUG 7880 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'welcome'; URL [/WEB-INF/jsp/welcome.jsp]] in DispatcherServlet with name 'dispatcherServlet'
2016-04-24 19:54:49.022 TRACE 7880 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView  : Rendering view with name 'welcome' with model {} and static attributes {}
2016-04-24 19:54:49.026 DEBUG 7880 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView  : Forwarding to resource [/WEB-INF/jsp/welcome.jsp] in InternalResourceView 'welcome'
2

As you see in the trace, this is trying to resolve /jsp/welcome.jsp as InternalResourceView instead of JstlView.正如您在跟踪中看到的,这试图将 /jsp/welcome.jsp 解析为 InternalResourceView 而不是 JstlView。 Eventually it fails as 404.最终它以 404 失败。

What other steps I need to follow?我还需要遵循哪些其他步骤? Is there any tutorial for SpringBoot-mvc with jsp ?有没有关于带有 jsp 的 SpringBoot-mvc 的教程?

PS I can create spring mvc app with jsp using web.xml ( using JstlView in my config file ). PS我可以使用web.xml(在我的配置文件中使用JstlView)用jsp创建spring mvc应用程序。 But I can't find any tutorial for Spring boot mvc with jsp.但是我找不到任何关于带有 jsp 的 Spring boot mvc 的教程。

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);
    return resolver;
}

also needed and your pages should be at /webapp/WEB-INF/jsp/也需要,你的页面应该在 /webapp/WEB-INF/jsp/

+ +

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

Assuming it is embedded Tomcat,假设它是嵌入式Tomcat,

You need to have followings in your pom.xml您需要在pom.xml有以下内容

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

Embedded Tomcat core package has no JSP rendering support.嵌入式 Tomcat 核心包没有 JSP 渲染支持。

You do not require the ViewResolver.您不需要 ViewResolver。 pom.xml needs the mentioned dependencies as told by Yura and place the jsp files in src\\main\\webapp\\WEB-INF\\jsp. pom.xml 需要上述提到的依赖项,如 Yura 所说,并将 jsp 文件放在 src\\main\\webapp\\WEB-INF\\jsp 中。

If we want to use jsp as view in spring boot(which by default has tomcat server) we need to add the following to our pom.xml:如果我们想在 spring boot(默认有 tomcat 服务器)中使用 jsp 作为视图,我们需要将以下内容添加到我们的 pom.xml 中:

<dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
</dependency>

we need to add our pages inside我们需要在里面添加我们的页面

src/main/webapp/WEB-INF/view src/main/webapp/WEB-INF/view

After this we can specify the location for our jsp pages in application.properties在此之后,我们可以在 application.properties 中指定我们的 jsp 页面的位置

  spring.mvc.view.prefix=/WEB-INF/view/
  spring.mvc.view.suffix=.jsp
  1. Create webapp/WEB-INF/views {Name the last folder as U Like } under src/main在 src/main 下创建 webapp/WEB-INF/views {Name the last folder as U Like }
  2. add jstl jar添加jstl jar

  3. add following two lines in application.properties在 application.properties 中添加以下两行

    spring.mvc.view.prefix:/WEB-INF/views/ spring.mvc.view.suffix:.jsp spring.mvc.view.prefix:/WEB-INF/views/ spring.mvc.view.suffix:.jsp

    Run As Spring Boot App ..U are good to go ! Run As Spring Boot App ..U 很好!

    For More U can consult my this project on github : https://github.com/SudipBiswasRana/Using-JSP-As-View-For-Spring-Project更多你可以在github上咨询我的这个项目: https : //github.com/SudipBiswasRana/Using-JSP-As-View-For-Spring-Project

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM