I'm aware similar questions have been asked but I can't seem to find one that is the same as my configuration.
I have an Eclipse Java EE project set up with Maven and spring. I followed these tutorials to set it up:
http://fruzenshtein.com/setup-of-dynamic-web-project-using-maven/
http://fruzenshtein.com/spring-mvc-creation-of-simple-controller-with-java-based-config/
When I launch the project I get an error 404.
The tutorials didn't mention the creation of a dispatcher-servlet.xml so maybe that is the problem but I'm not sure if I even need it with Maven.
My config is as follows:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>FindLove</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>FindLove</groupId>
<artifactId>FindLove</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring.version>3.1.1.RELEASE</spring.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<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-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- CGLIB is required to process #Configuration classes -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<!-- Servlet API, JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
Configuration class
#Configuration //Specifies the class as configuration
#ComponentScan("com.sprmvc") //Specifies which package to scan
#EnableWebMvc //Enables to use Spring's annotations in the code
public class WebAppConfig {
#Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
Initializer class
public class Initializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebAppConfig.class);
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
Controller
#Controller
public class LinkController {
#RequestMapping(value="/hello-page")
public ModelAndView goToHelloPage() {
ModelAndView view = new ModelAndView();
view.setViewName("hello"); //name of the jsp-file in the "page" folder
String str = "MVC Spring is here!";
view.addObject("message", str); //adding of str object as "message" parameter
return view;
}
}
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<h1>Home page</h1>
<p>This is a Home Page.</p>
<p>Hello world link</p>
hello.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<p>Hello world: ${message}</p>
<p>Well done!</p>
Any advice?
Thanks in advance
I don't think
<p>Hello world link</p>
can reach a Servlet mapped to
servlet.addMapping("/");
and invoke a #Controller handler method mapped to
#RequestMapping(value="/hello-page")
Change your link to
<p>Hello world link</p>
Ideally, you would want to put it to
<p>Hello world link</p>
so that it is relevant to the context path. Don't forget to add the tag lib declarations.
Related
i look for the solution about my problem but none of them seem to work on me, so i'm gonna ask, i'm practicing Spring MVC, i can return the jsp file from "webapp" folder, but the moment i move it to a folder, then move the folder to WEB-INF folder, it just stop working:
Here is my layout:
This is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>SpringConfig</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringConfig</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Here is my SpringConfig-servlet.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"
xsi:schemaLocation="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-4.3.xsd
">
<context:component-scan base-package="com.SpringWebMvc"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Here is my HomeController:
package com.SpringWebMvc.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class HomeController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
return "user/index";
}
}
Here is my index.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home page</title>
</head>
<body>
<p>hello</p>
</body>
</html>
Here is my 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.locnt.spring-web-mvc</groupId>
<artifactId>SpringWebMvc</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version> <!-- or whatever current version -->
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
And this is what i get:
I am completely clueless about what the hell is happening, please help me :(
Seems like the issue is related to fact that in SpringConfig-servlet.xml you have:
<property name="prefix" value="/WEB-INF/views/"/>
but views are located in webapp/views.
You can try moving them to /WEB-INF/views/ folder.
After 2 days of thinking finding what the hell happen to my code, i've finnaly solve it, the thing is it's not my code, but my DAMN SERVER, i am using tomcat v10 at the moment, so i just remove v10, and change to v9 instead, and woala!! it work as it suppose to be
I am creating a simple webservice project that pulls data from db on countries and displays it in a JSON form, My project does not throw any error and builds successfully but it is unable to find deployed resource.
WorldInformation.java class
package com.webservices;
import java.util.List;
import com.webservices.models.Country;
import com.webservices.services.WorldInformationService;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
#Path("/worldinformation")
public class WorldInformation {
WorldInformationService worldInformationService = new WorldInformationService();
#GET
#Path("/getCountries")
#Produces(MediaType.APPLICATION_JSON)
public List<Country> getCountries(){
System.out.println("reached point 1");
List<Country> countryList = worldInformationService.getCountries();
return countryList;
}
#POST
#Path("/setCountry/{country}/{countryCode}")
#Consumes(MediaType.TEXT_PLAIN)
public void setCountry(#PathParam("country") String country,#PathParam("countryCode") String countryCode){
worldInformationService.setCountry(country,countryCode);
}
}
This is web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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.webservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
And this is what pom.xml looks like
<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.webservices</groupId>
<artifactId>WorldInformation</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>WorldInformation</name>
<build>
<finalName>WorldInformation</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.8</source>
<target>1.8</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>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
-->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
<properties>
<jersey.version>3.0.0-M1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
This is error snapshot
I can't seem to figure out how to get this resolved and what's going wrong, will deeply appreciate your help on this.
I think you need to add #ApplicationPath in your jax-rs application as well.
Note: The URL is also missing the context path.
For example: http://localhost:8080/<context-path>/servlet/path
From Oracle Docs :
The #ApplicationPath annotation is used to define the URL mapping
for the total application. The path specified by #ApplicationPath is the
base URI for all resource URIs specified by #Path annotations in the
resource class.
For example : Add a class WorldInformationApp annotated with #ApplicationPath in the project.
#ApplicationPath("/worldinfo")
public class WorldInformationApp extends Application {
}
Try to access the resource with this url : http://localhost:8080/<context-path>/worldinfo/worldinformation/getCountries
Note : I think you are using wrong libraries for jax-rs app.
For example : javax.ws.rs.* should be used instead of jakarta.ws.rs.*
Thanks for your valuable feedbacks, here is what was worked for me. As someone suggested. I was not looking at correct point. I was missing application name in the url I was trying to connect to. Though I ran into other problems but I managed to solve them and now its working for me.
localhost:8080/WorldInformation/worldinformation/getCountries
Thanks for your valuable help on this.
I trying to build a simple welcome page (in jsp) using Spring Boot.
Below is the project structure
Application
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
APIController
#Controller
public class APIController {
#RequestMapping("/")
public String home() {
System.out.println("testing");
return "welcome";
}
}
When I access http://localhost:8080/, I get below error
This application has no explicit mapping for /error, so you are seeing
this as a fallback.
Wed May 23 15:31:51 MYT 2018 There was an unexpected error (type=Not
Found, status=404). No message available
By curl
{"timestamp":1527061233703,"status":404,"error":"Not Found","message":"No message available","path":"/"}
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>
<artifactId>spring-boot-web-jsp</artifactId>
<packaging>war</packaging>
<name>Spring Boot Web JSP Example</name>
<description>Spring Boot Web JSP Example</description>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- This is a web application -->
<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>
<!-- Optional, test for static content, bootstrap CSS-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
You have to define the prefix and suffix for you jsp file in application.properties file like following:
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
In directory \src\main\resources\static, Create a simple index.html file
<html>
<head>Hello, Bich Van</head>
<body>
<h3>Today is a rainny day</h3>
</body>
</html>
Then try again at http://localhost:8080
If you don't like static HTML files, you can use JSP, but need a little complicate. You need declaring JSP View resolver in Spring MVC configuration.
Reference document: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-welcome-page
Ensure that you have jasper and jstl in the list of dependencies:
<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>
Here is a working starter project - https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp
It's better to use thymeleaf in your spring boot project and add the following dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
In directory \src\main\resources\template, Create a simple index.html file
i 'm new in spring boot and i wanted to start with a simple example however nothing seemed to work
this my controller:
Package controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class testController {
RequestMapping("view/aboutus")
public String greeting(Model model) {
model.addAttribute("name", "azertyyyy");
return "aboutus"; }}
and this is the jsp view:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE>
<html> <body> ${name}
<p>hello world</p> </body></html>
i added the needed dependencies:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
and i added the prefix and suffix in application.properties
when i run the application only hello world is shown on the view without the name variable, after searching here i changed the view directory from src/main/webapp/view to src/main/resource/templates but spring boot couldn't find the view anymore
I am not sure if you are fully aware of the changes we have to make when we want to run the application as war package.
Change the packing to war from jar and require dependencies.
Change the main application class to extend SpringBootServletInitializer and implement configure()
Define your jsp pages path in application properties
Put JSPs in correct path (webapp/WEB-INF/jsp check screen shot)
Directory Structure for the project
Following is the code changes I have to done to run the JSP
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.test</groupId>
<artifactId>Test_Exception_Framework</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Test_Exception_Framework</name>
<description>Project to test ExceptionFramework</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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Main Class
package com.exception;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
#SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWebApplication.class);
}
}
Rest Controller - which redirects to JSP
package com.exception.controller;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping(method = RequestMethod.GET)
public class TestExceptionController {
#RequestMapping("/")
public String welcome(Map<String, Object> model) {
System.out.println("in controller");
model.put("message", "hello spring boot");
return "welcome";
}
}
application.properties - define the path for JSPs
server.port=8085
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
1)*** use spring version 2.0.3.RELEASE or less
2) add the dependency
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
3) this optional dependency
<!-- JSTL for JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
I am currently attempting to run a very simple Spring application in RAD 9, on the local Websphere 8.5 Liberty profile. The code is from this site: http://www.programcreek.com/2014/02/spring-mvc-helloworld-using-maven-in-eclipse/
I'm using Maven. Here's my 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.pncbank.cdd</groupId>
<artifactId>cddsample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cddsample 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-core</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.5.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>spring-aop</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.5.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>spring-aop</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>cddsample</finalName>
</build>
<packaging>war</packaging>
</project>
There is a very simple view in WEB-INF/views. The controller is defined in the Java classes The Spring DispatcherServlet is configured in web.xml as described in the tutorial mentioned above:
HelloWorldController.java
package com.programcreek.helloworld.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloWorldController {
String message = "Welcome to Spring MVC!";
public HelloWorldController(){
int i = 5;
}
#RequestMapping("/hello")
public ModelAndView showMessage(
#RequestParam(value = "name", required = false, defaultValue = "World") String name) {
System.out.println("in controller");
ModelAndView mv = new ModelAndView("helloworld");
mv.addObject("message", message);
mv.addObject("name", name);
return mv;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
<description/>
</context-param>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<display-name>DispatcherServlet</display-name>
<description/>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
I have arrived at a point where the application will run, and I can visit a simple index.jsp. However, any other route leads to the error below.
I have found many questions and posts dealing with this particular error, but no real help diagnosing or resolving it. I have reviewed all the dependencies of the DispatcherServlet and, as far as I can tell, they're all present in the jar file that is deployed. Any guidance would be greatly appreciated!! TIA!
Error 404: javax.servlet.UnavailableException: SRVE0203E: Servlet
[DispatcherServlet]: org.springframework.web.servlet.DispatcherServlet
was found, but is missing another required class. SRVE0206E: This
error typically implies that the servlet was originally compiled with
classes which cannot be located by the server. SRVE0187E: Check your
class path to ensure that all classes required by the servlet are
present.SRVE0210I: This problem can be debugged by recompiling the
servlet using only the classes in the application's runtime class path
SRVE0234I: Application class path=[C:\Program
Files\IBM\WebSphere\AppServer\java\lib;C:\Program
Files\IBM\WebSphere\AppServer\java\lib\dt.jar;C:\Program
Files\IBM\WebSphere\AppServer\java\lib\htmlconverter.jar;C:\Program
Files\IBM\WebSphere\AppServer\java\lib\ibmorbtools.jar;C:\Program
Files\IBM\WebSphere\AppServer\java\lib\jconsole.jar;C:\Program
Files\IBM\WebSphere\AppServer\java\lib\tools.jar;C:\Program
Files\IBM\WebSphere\AppServer\profiles\AppSrv1\classes;C:\Program
Files\IBM\WebSphere\AppServer\classes;C:\Program
Files\IBM\WebSphere\AppServer\lib;C:\Program
Files\IBM\WebSphere\AppServer\lib\COBOLCallStubGenerator.zip;C:\Program
Files\IBM\WebSphere\AppServer\lib\EJBCommandTarget.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\IVTClient.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\OTiSConvertTime.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\activation-impl.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\admin.config.jobcl.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\admin.config.rules.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\admin.config.sched.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\aspectjrt.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\batch.wccm.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\batchpmi.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\batchprops.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\batchutilsfep.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\batfepapi.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\bootstrap.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\bsf-engines.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\com.ibm.rls.jdbc.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\commandlineutils.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\commons-discovery.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\databeans.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\ffdcSupport.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\htmlshell.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\iscdeploy.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\j2ee.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\jNative2ascii.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\jacl.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\jrom.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\launchclient.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\lmproxy.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\mail-impl.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\openwebbeans.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\pc-appext.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\pmirm4arm.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\rrd-appext.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\rsadbutils.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\rsahelpers.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\serviceadapter.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\setup.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\startup.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\tcljava.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\urlprotocols.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\wasservicecmd.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\wses_dynaedge.jar;C:\Program
Files\IBM\WebSphere\AppServer\lib\wsif-compatb.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedChannels;C:\Program
Files\IBM\WebSphere\AppServer\web\help;C:\Program
Files\IBM\WebSphere\AppServer\deploytool\itp\plugins\com.ibm.etools.ejbdeploy\runtime;C:\Program
Files\IBM\WebSphere\AppServer\deploytool\itp\plugins\com.ibm.etools.ejbdeploy\runtime\batch.jar;C:\Program
Files\IBM\WebSphere\AppServer\deploytool\itp\plugins\com.ibm.etools.ejbdeploy\runtime\ejbdeploy.jar;C:\Program
Files\IBM\WebSphere\AppServer\deploytool\itp\plugins\com.ibm.etools.ejbdeploy\runtime\ejbmapvalidate.jar;C:\Program
Files\IBM\WebSphere\AppServer\derby\lib\derby.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\sib.api.jmsra.rar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.mq.commonservices.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.mq.connector.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.mq.headers.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jmqi.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jmqi.local.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jmqi.remote.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jmqi.system.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jms.admin.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.mq.pcf.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.mqjms.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.commonservices.j2se.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.commonservices.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.jms.internal.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.jms.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.matchspace.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.provider.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.ref.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.wmq.common.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.wmq.factories.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.wmq.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.wmq.v6.jar;C:\Program
Files\IBM\WebSphere\AppServer\installedConnectors\wmq.jmsra.rar\dhbcore.jar;C:\Program
Files\IBM\WebSphere\AppServer\profiles\AppSrv1/properties;C:\Program
Files\IBM\WebSphere\AppServer/properties;C:\Program
Files\IBM\WebSphere\AppServer/lib/startup.jar;C:\Program
Files\IBM\WebSphere\AppServer/lib/bootstrap.jar;C:\Program
Files\IBM\WebSphere\AppServer/lib/jsf-nls.jar;C:\Program
Files\IBM\WebSphere\AppServer/lib/lmproxy.jar;C:\Program
Files\IBM\WebSphere\AppServer/lib/urlprotocols.jar;C:\Program
Files\IBM\WebSphere\AppServer/deploytool/itp/batchboot.jar;C:\Program
Files\IBM\WebSphere\AppServer/deploytool/itp/batch2.jar;C:\Program
Files\IBM\WebSphere\AppServer/java/lib/tools.jar;C:\Users\PT35330\IBM\rationalsdp\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\cddsample\WEB-INF\classes;C:\Users\PT35330\IBM\rationalsdp\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\cddsample\WEB-INF\lib\aopalliance-1.0.jar;C:\Users\PT35330\IBM\rationalsdp\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\cddsample\WEB-INF\lib\commons-logging-1.2.jar;C:\Users\PT35330\IBM\rationalsdp\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\cddsample\WEB-INF\lib\spring-beans-4.1.5.RELEASE.jar;C:\Users\PT35330\IBM\rationalsdp\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\cddsample\WEB-INF\lib\spring-context-4.1.5.RELEASE.jar;C:\Users\PT35330\IBM\rationalsdp\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\cddsample\WEB-INF\lib\spring-core-4.1.5.RELEASE.jar;C:\Users\PT35330\IBM\rationalsdp\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\cddsample\WEB-INF\lib\spring-expression-4.1.5.RELEASE.jar;C:\Users\PT35330\IBM\rationalsdp\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\cddsample\WEB-INF\lib\spring-webmvc-4.1.5.RELEASE.jar;C:\Users\PT35330\IBM\rationalsdp\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\cddsample]