I've been working on this for days trying to get it to work. Trying to use JSP with Spring Boot. I've been following the example at github. So many examples on the web have a web.xml file, yet the example on github, which is referenced many times on this forum, doesn't even have a web.xml. Also, when creating a Spring Boot Project, STS doesn't create a "webapp" directory with subdirectory "WEB-INF". I had to create those manually in Windows Explorer then refresh Project Explorer in STS to make them appear.
Regardless, I have followed the example as best I can but still can't get my index.jsp file to render in the browser when I type in http://localhost:8080 in the address field.
Any guidance would be appreciated.
This is what I have so far.
Directory structure:
application.properties:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/jeddatae_editor
spring.datasource.username=webapp
spring.datasource.password=secret
index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
String name = request.getParameter("name");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JED Login</title>
</head>
<body>
<form action="/index" method="post">
<p>Insert ID here: <input type="text" id="id" name="id"></p>
<input type=submit>
</form>
<%
if(!name.equals(""))
{
%>
<div class="welcome">
<p>Welcome <%=name %> </p><p>If you are not <%=name %>, then try a different id.</p>
</div>
<%
}
%>
hello.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Hello</h2>
</body>
</html>
GreetingController.java
package com.anypackage.jedcontext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;
import com.tacticalenterprisesltd.Database;
@Controller
public class GreetingController {
@RequestMapping("/")
public RedirectView index(@RequestParam(value="id", required=false) String id, RedirectAttributes attribs) {
if (id != null)
{
Database db = new Database("jeddatae_editor");
String[][] result = db.executeSelect("SELECT CONCAT(first_name, ' ', last_name) FROM employees WHERE id=" + id + ";");
if(result.length != 0){
attribs.addAttribute("name", result[0][0]);
}
}
return new RedirectView("index");
}
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.anypackage</groupId>
<artifactId>jedtest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>jedtest</name>
<description>An application context test</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Tomcat embedded container-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
It depends upon how you package your artifact as a war/jar file.
War File Package following rules should be followed.
JAR file Package
It won't work as your embedded tomcat is using hard coded file pattern in tomcat.
Exactly follow this sample and refactor your code accordingly.
Please refer this JSP Limitations with Spring Boot.
I think you first need to handle redirect. Because in your case new RedirectView("index")
redirects to for example /index?name=Alan
Add method:
@GetMapping("/index")
public String handleRedirect() {
return "index";
}
It should work if you run it as simple spring boot app:
mvn spring-boot:run
But if you are going to deploy war file on app server don't forget to configure like this:
@SpringBootApplication
public class JedtestApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(JedtestApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(JedtestApplication.class, args);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With