I am creating simple spring-mvc application referring this url. I created maven web project by referring url.
After creating this project some default file named 'index.jpg' is generated in this project. And when I build and run this project it was shpoing content of 'index.jsp' file.
Now I edited content of pom.xml file and it looks like this:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.leader.unisys</groupId>
<artifactId>sample-application</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>sample-application Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.6</java.version>
<spring.version>4.0.3.RELEASE</spring.version>
<cglib.version>2.2.2</cglib.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring core & mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!-- CGLib for #Configuration -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>${cglib.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Servlet Spec -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>sample-application</finalName>
</build>
</project>
The project structure looks like as shown in the image:
Now, How can I make this project to work as per my mapping in the spring-servlet.xml file.
view resolver:
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
content from controller:
#RequestMapping(value="/")
public ModelAndView getHomePage(){
return new ModelAndView("home");
}
web xml default content:
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
I want to display home.jsp after hitting http://localhost:8080/sample-application. Can someone give me the instructions to do this. I am working with maven for first time.
Move your home.jsp under WEB-INF. And then add this line in your web.xml
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
This <welcome-file-list> in web.xml tells the application that it has to be loaded first.
what you are asking for has nothing to do with Spring or Maven but Java EE / Web container configuration - via web.xml
Here is a link that should help ..
http://docs.oracle.com/cd/E14571_01/web.1111/e13712/configurejsp.htm#WBAPP183
Relevant parts copied over:
Welcome files are defined at the Web application level. If your server is hosting multiple Web applications, you need to define welcome files separately for each Web application. You define Welcome files using the welcome-file-list element in web.xml. (The web.xml file is located in the WEB-INF directory of your Web application.) The following is an example Welcome file configuration:
Welcome File Example
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>foo.bar.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>*.foo</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/welcome.foo</welcome-file>
</welcome-file-list>
hope this helps.
When we start the web application, by default web container looks for the welcome-file-list present in web.xml, if the welcome-file-list does not exist in web.xml then the container will look for index.html,index.htm, and index.jsp under the web-content folder in case of a normal dynamic web project, but incase of maven project container looks under the web-app folder. Container follows the order and if the first file exists it will execute that file and if it doesn't exist, the container follows the order until the third file. If any one of the files doesn't exist container will throw a 404 error.
For reference https://www.javatpoint.com/welcome-file-list
The above flow is the same for both dynamic and maven web projects, the only thing that differs from each other is that we have a web-content folder in case of dynamic WP and a web-app folder incase of maven WP
Related
After debugging for a few hours, I am seeking help to set up a REST API using Jersey in Java. I am using a Tomcat 6 server and the following is my file structure of the Mavin project:
On the right, the resource which I want to expose can be seen.
For the web.xml I have a file as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details https://github.com/jax-rs -->
<web-app version="2.5">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.howtodoinjava.demo</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
and for the pom.xml as follows:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.howtodoinjava.demo</groupId>
<artifactId>JerseyArcheTypeDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>JerseyDemo</name>
<build>
<finalName>JerseyArcheTypeDemo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
-->
</dependencies>
<properties>
<jersey.version>2.20</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
I can build the Maven project and start the server. However if I want to add resources to the Server by right-clicking on the server name and clicking on "Add or Remove..." I get a message that I cannot select any resources. I followed the adivce in this question: There are No resources that can be added or removed from the server but it did not help and I also do not get any error messages which could lead me. Any recommendation what I am doing wrong?
When I want to run the Resource on the server by selecting "Run As" and then "Run on Server" I get the following message: The selection cannot be run on the server.
You can't select this project because Tomcat 6 does not support Servlet 4.0 projects. Servlet 4.0 requires Tomcat 9.x, which itself requires Java 1.8. See the Tomcat documentation.
You can lower the facet version, but it makes far more sense to simply use the newer Tomcat version. Tomcat 6 is far past its end-of-life.
I go over the internet to figure it out why I receive 404 I tried almost all solutions and didn't help. I have:
Eclipse Photom
Tomcat 9
Java version 1.8
Maven project using Jersey 2.27
When I hit http://localhost:8080/Test/rest/testservice I got 404 "Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists."
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Test</groupId>
<artifactId>com.test</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>com.test Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<finalName>com.test</finalName>
</build>
</project>
WEB.XML
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Test Jersey Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Test Jersey Service </servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
PROJECT STRUCTURE AND JAVA CLASS
I Have added in Deployment Assembly -> Maven Dependencies also
I solved it. So if anyone experience problems with 404 even the fact that you did everything right then double check these steps:
Always save (CTR + S) pom.xml and web.xml when you make any kind of
modifications
Replace index.jsp with index.html
Right click the project go to Maven -> Update Project
Right click the project go to Run as -> Run on Server (if I run it
manually it doesn't work, but when I go from run as it is working
correctly)
I have created a tutorial with step by step. If anyone wanted please contact me.
What I Have
Tomcat 9.0.7
WildFly 12.0.0
JDK 10
Maven 3.5.3
Relevant Files
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.training</groupId>
<artifactId>BigVoiceWebApp</artifactId>
<name>BigVoiceWebApp Maven Webapp</name>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.10</maven.compiler.source>
<maven.compiler.target>1.10</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!-- <build>
<finalName>${project.artifactId}</finalName>
</build>-->
<profiles>
<profile>
<id>release-profile</id>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<finalName>BigVoiceWebApp</finalName>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>Archetype Created Web Application</display-name>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/PostgresDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
My Problem
I have servlets like this:
#WebServlet(name = "MemberController", urlPatterns = {"/MemberController"})
#MultipartConfig
public class MemberController extends HttpServlet {
The problem now, WildFly somehow does not obey annotations. When I access the URL, this is what I got in my browser (copied by viewing source HTML):
<html>
<head><title>Error</title></head>
<body>Not Found</body>
</html>
I have to declare everything in web.xml (adding to previous file) for what I've declared as annotations.
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.example.training.controller.MemberController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/MemberController</url-pattern>
</servlet-mapping>
When I reupload the exact same WAR file to Tomcat, the annotations are read correctly.
My question is, why? And how to make WildFly recognize those annotations?
Thanks for the file. It's Java 10. I noticed during the build I get:
[INFO] --- maven-war-plugin:2.2:war (default-war) # SampleForError ---
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/home/scott/.m2/repository/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar) to field java.util.Properties.defaults
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
A similar warning is produced when Wildfly starts up. Some combination of Wildfly 12 and Java 10 doesn't work right in JEE7 mode.
However, there are multiple workarounds. If you run Wildfly in EE8 Preview Mode then it works. Do this by running either bin/standalone.sh -Dee8.preview.mode=true or bin/standalone.sh -c standalone-ee8.xml
Otherwise, this works without any changes in Java 8.
As an aside, you're trying very hard to confuse any container you're running in. You specify servlet spec 4 in your pom.xml but then your web.xml is:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
So your web.xml tells the container that you're using Servlet spec 2.3. If you really need servlet spec 4.0 then your web.xml should be something like:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
I'm trying to create a new project with spring boot.
But I'm getting Error described below. I have added my code.
Error
HTTP ERROR 404 page not found
Here is the link of my project structure.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.wc</groupId>
<artifactId>wc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>wc</name>
<description>Work configurator project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>`
login.jsp contains just hello
LoginController.class
#Controller
public class LoginController {
#RequestMapping(value = {"/","/login"},method = RequestMethod.GET)
public ModelAndView getLoginPage(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login");
return modelAndView;
}
}
WcApplication.class
#SpringBootApplication
public class WcApplication {
public static void main(String[] args) {
SpringApplication.run(WcApplication.class, args);
}
}
Why I'm getting this and how to solve this problem? Thanks
I think you have missed out ViewResolver configuration class.
#Configuration
#EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter
{
#Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
registry.viewResolver(resolver);
}
}
Try above configuration and it should call login.jsp page.
Update
As you have correctly added resolver in application.properties. I figured that the problem is with folder WEB-INF. This folder is secured from outer access and .jsp files should not be inside of this folder. It is for files like web.xml which should not be exposed to client.
Just rename it to view (or something else) and change application.properties as -
spring.mvc.view.prefix=/view/jsp/
spring.mvc.view.suffix=.jsp
This should definitely work.
In my case I had everything correctly setup in spring boot. The jsps were in src/main/webapp/WEB-INF/jsp
and index.html the welcome page in src/main/resources/static/index.html. Still howevere hard I tried the jsp pages were not getting rendered. I always got 404 return.
But when I changed the build process to create a war field instead of the jar file it worked fine.
Just made additional line change in the build.gradle file as below
apply plugin: "war"
and after that while ruuing the war as java -jar demo.war everything fell in place and started working.
I just had this same issue occur. Project structure was correct and everything looked good. I made a slight change to my application.properties spring.mvc.view.prefix. I changed "/WEB-INF/jsp/" to "WEB-INF/jsp/" and everything works correctly. I think in the generate target and war it is the root directory so the additional / as a prefix was confusing something
Its a hierarchy problem, the WEB-INF should be under webapp folder.
yourMainPackage/src/main/webapp/WEB_INF
(note that for it to work with springboot default configuration, webapp/ should be under main/ and not under resources)
I faced the same issue and it took me long time to figure out that there are three conditions.
1.) In application properties you need to have the following view resolver mapping
(You can use java based as well):
logging.level.web=DEBUG
spring.mvc.view.prefix=/WEB-INF/pages/
spring.mvc.view.suffix=.jsp
Note: you have to create "WEB-INF" folder by yourself as in my case "webapp"
folder was empty. DEBUG property is not necessary just keep it as it will
help you find the root cause from logs.
2.) I had to make the packaging as "war" instead of jar.
<packaging>war</packaging>
3.) Make sure you have added tomcat jasper dependency to compile the jsp pages at
runtime.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
Hope it helps and feel free to correct me.
This is my first time working with Spring and I am following the tutorial on PluralSight. Running the first example, I run into the error that is listed in the title. In my console, I get errors like these. I am using older versions of the dependencies to go through this course. I've made sure to follow the tutorial closely so not sure if I messed up somewhere or something is deprecated. I am using http://localhost:8080/FitnessTracker/greeting.html
SEVERE: Context initialization failed
java.lang.IllegalArgumentException
SEVERE: StandardWrapper.Throwable
java.lang.IllegalArgumentException
SEVERE: Allocate exception for servlet fitTrackerServlet
java.lang.IllegalArgumentException
HelloController.java
package com.pluralsight.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HelloController {
#RequestMapping(value = "/greeting")
public String sayHello(Model model) {
model.addAttribute("greeting", "Hello World");
return "hello";
}
}
servlet-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.pluralsight.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>
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>
<h1>${greeting}</h1>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>fitTrackerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>fitTrackerServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<display-name>Archetype Created Web Application</display-name>
</web-app>
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pluralsight</groupId>
<artifactId>FitnessTracker</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>FitnessTracker Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>FitnessTracker</finalName>
</build>
</project>
I had the same problem with the same PluralSight tutorial. I had both Java 1.7 and Java 1.8 installed. The project apparently throws an exception with Java 1.8. To rectify this situation you need to ensure that your Spring STS environment is set to use Java 1.7.
Right click on the project and select Properties.
In the Properties dialog, select Java Compiler.
At the bottom of the dialog, select the link to configure the Installed JREs.
Add a definition to point to JRE 1.7 and select this as the default.
The PluralSight tutorial now works for me. I am beginner to Java and Spring MVC myself so I am not sure if this was the best way to solve the problem.
Well, spring 3.2 needs java 7 or earlier. Instead of changing project properties, one another solution is just change the version number of spring mvc in pom.xml to 4.2.4.RELEASE instead of 3.2.0.RELEASE that you have used earlier.
Now, you can proceed normally with java 8.
Below, I show the whole pom.xml with only change that I mentioned above.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pluralsight</groupId>
<artifactId>FitnessTracker</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>FitnessTracker Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>FitnessTracker</finalName>
</build>
</project>
Try with 4.1.5. It works for me.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
Try to use 3.2.3.RELEASE version wherever you have spring based dependency
I faced the same issue.
So what you have to do is to use Tomcat v7.0,
It is because I was using Tomcat v9.0 and it does not work with JRE7 but JRE8.
So
Set your application with Tomcat v7.0 and set your STS or Eclipse default build path with JRE7 .
Restart your Tomcat server.
Go to your browser and paste the above link and Here you Go !!! :)
To fix this, simply change your servlet container's runtime environment to Java 7. In Eclipse:
Preferences -> Server -> Runtime Environments
Select the server you are running the project under and click Edit....
From the JRE section, select the Java 7 JRE. Note that in order to be able to do this, you first need to download and install Java 7, and make the Java 7 JRE available in Eclipse. To make Java 7 JRE available in Eclipse:
Download Java 7 and install it.
In Eclipse, select Preferences -> Java -> Installed JREs and click Search.... If you have installed Java 7 to its default path, Eclipse should be able to discover Java 7 automatically.
After adding Java 7 to Eclipse, you can select JRE 7 as the runtime environment of your servlet container.
I would also advise indicating the runtime environment being used in the server's name. For example, in Eclipse, instead of letting the server name simply be "Apache Tomcat v7.0", make it "Apache Tomcat v7.0 - JRE 7". Of course, you should update this name if you change the runtime environment of this server later.
Source: http://www.codejava.net/ides/eclipse/how-to-change-java-runtime-environment-for-tomcat-in-eclipse
NOTE: This answer applies to any other IDE as well. You just need to make sure that your servlet container runs under JRE 7.