1

I can't get the values added to the model in jsp dispalyed. I have tried with everything and and checked all answers on stackoverflow, but nothing helps.

To save your time, I paste a part of code:

My pom.xml :

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath/>
</parent>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>

and my java config code is :

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

and

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver getViewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
   }
}

my controller is :

@Controller
@RequestMapping(value = "workorder")
public class WorkOrderController {

    @RequestMapping(value = "/toProviewPage", method = RequestMethod.GET)
    public ModelAndView toPreview(){
        WorkOrderVo workOrderVo = new WorkOrderVo();
        workOrderVo.setId(1);
        workOrderVo.setName("xxx");
        workOrderVo.setPriority(1);
        workOrderVo.setDetail("xxxx");
        return new ModelAndView("workOrderPreview", "workOrderVo", workOrderVo);
    }
}

my jsp file is :

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">

<div class="form-group">
    <label for="detail" class="col-sm-2 control-label">Detail</label>
    <div class="col-sm-10">
        <textarea class="form-control" rows="5" id="detail" name="detail" value="${workOrderVo.dreadonly="true"></textarea>
    </div>
</div>

but spring boot doesn't resloved is as a jsp file, and display all the html code is the Browser.

Where I write errors?

Rollen Holt
  • 497
  • 2
  • 11
  • 19
  • my tomcat version is 8.0.x and jdk version is 8 – Rollen Holt Jul 29 '15 at 09:04
  • Did you run your Application (Tomcat) with of your IDE, if so what IDE your are working with? – sven.kwiotek Jul 29 '15 at 12:25
  • @s.kwiotek I use Intellij IDEA, I run application use IDEA's Spring boot comfigurations。 the main class is `io.rollenholt.github.workorder.Application` – Rollen Holt Jul 29 '15 at 13:11
  • 1
    Try in your Project Structure - Modules - Dependencies to change the scope to "compile" except tomcat-embed-jasper, core.compiler:ecj and scope "runtime" for commons-el. Thats works for me. – sven.kwiotek Jul 29 '15 at 15:08
  • @s.kwiotek Thank you very much. when I remove the `provided` element from pom.xml file it works for me.Your answer saves me several hours – Rollen Holt Jul 30 '15 at 01:34

2 Answers2

2

You can't run the main() method directly for the application when you are using "embedded Tomcat". If you look at your POM it is indicating that the Jasper dependency is provided meaning that it expects your artifact to be put inside a Tomcat container. Running the main() method in your application does not actually load up Jasper which is why you are not seeing the pages processed as JSP. For IntelliJ you need to set up a Maven run configuration (not an application) and use spring-boot:run which apparently sets up Tomcat for you. Here is a picture of how to set it up. Maven Run Setup
I did it with your application and it appears that the JSPs are being processed. I don't see the exact code you are referencing above in GitHub but I noticed that the preview page had a JSP tag that was processed. There was no value in it but the tag was replaced. Its not clear to me that this is documented so I got a hint from this question. If anyone finds official Spring Boot documentation that makes it clear let me know and I will update this.

Community
  • 1
  • 1
Rob Baily
  • 2,770
  • 17
  • 26
1

I'll summarize the current two feasible solutions:

  • first :

modify pom dependency, change:

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

to

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
  • The second:

use spring-boot:run command to start application.

Rollen Holt
  • 497
  • 2
  • 11
  • 19