I am working on an Spring Boot MVC application. My issue is that when the view appears, it does not parse the values from the Model.
application.properties
spring.application.name=test-mvc
server.context-path=/test-mvc
Config
@Configuration
@EnableWebMvc
public class ApplicationConfigurerAdapter extends WebMvcConfigurerAdapter{
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
}
Application
@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);
}
}
Controller
@Controller
@RequestMapping("/view")
public class TestController {
@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, ModelMap model) {
model.addAttribute("name", name);
return "greeting";
}
}
greeting.jsp
<html>
<head><title>Hello world Example</title></head>
<body>
<h1>Hello ${name}, How are you?</h1>
</body>
</html>
Output from http://localhost:8080/test-mvc/view/greeting?name=Mikey:
Hello ${name}, How are you?
I have also tried returning a ModelAndView. I am not sure why it's not putting "Mikey" in where ${name}
is.
Update
Every time I add a taglib or anything to the JSP page, it displays the page source. It doesn't render the page. Below is my pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<properties>
<hibernate.version>5.0.5.Final</hibernate.version>
<postgres.version>9.4.1208</postgres.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
Add below dependencies in your
pom.xml
:You might have to add below dependency to avoid
javax.servlet.ServletException: java.lang.NoSuchMethodError: org.apache.el.lang.ELSupport.coerceToType
. If you don't add this asscope=required
then tomcat-embed-el-8.0.32.jar will be placed in yourTomcat 8.0\webapps\AppName\WEB-INF\lib
folder and will cause above error. You don't need to add below dependency if you run your app from IDE ormvn Spring-boot:run
command.Then visit - http://localhost:8080/AppName/view/greeting?name=Mikey
Check this repo for Complete Source Code.
UPDATE: The issue Tomcat war deployment of JSP sample is not working due to "tomcat-embed-el" dependency is fixed in Spring Boot - 1.4.0.M2