I am trying to build and deploy the Spring MVC web application from My MacBook Air. Created the maven project and added all the necessary files for the project. Able to build the project and deploy to Tomcat 8 via maven pom.xml. When I access the page it just shows me the jsp code as it is instead of content.
I could not able to see the generated servlet file out tomcat work folder. Since am deploying the files via maven I don't think it can hide in my eclipse workspace.
I went through similar questions but none of them worked out the form. For example, there were some suggestions to add / in front of WEB -INF in servlet.xml and add the tags in JSP and also put the content inside of the body tag. I hope this is something to do with my Tomcat8 and Maven3.3.9
When I access the application with URL, I can see the logs inside of my controller so it means that my code is deployed into Tomcat but only JSP to servlet conversion is not happening.
Here are my configuration files.
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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Digital Services</display-name>
<servlet>
<servlet-name>digitalservices</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/digitalservices-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>digitalservices</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
ditalservices-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"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.rajan.myservices.controller" />
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<mvc:resources mapping="/resources/**" location="/resources/" />
<!--
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"
id="tilesViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles3.TilesView" />
</bean>
-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Maven 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.rajan.myservices</groupId>
<artifactId>DigitalServices</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>DigitalServices Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<springframework.version>4.0.3.RELEASE</springframework.version>
<apachetiles.version>3.0.5</apachetiles.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Below declared dependencies are included for the servers who may complain
about servlet/jstl missing dependency -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>DigitalServices</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://127.0.0.1:8080/manager/text</url>
<server>TomcatServer</server>
<username>tomcat</username>
<password>tomcat1</password>
<path>/DigitalServices</path>
<update>true</update>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Controller Class
package com.rajan.myservices.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.rajan.myservices.model.Person;
@Controller
public class HomePageController {
@RequestMapping(value="index")
public String index() {
System.out.println("In index");
return "index";
}
@RequestMapping(value="viewPerson")
public ModelAndView viewPersons(Model model) {
Map<String, List<Person>> persons =
new HashMap<String, List<Person>>();
System.out.println("In View Person");
persons.put("persons", Person.createPersons());
System.out.println("persons: "+persons);
return new ModelAndView("personList", persons);
}
}
Index.jsp
<html>
<body>
<div style="margin:10px;">
<h3>Welcome to Digital Services by <% out.println("Hello Test"); %></h3>
</div>
</body>
</html>
Jsp output when acccess the application using http://localhost:8080/DigitalServices/index
Welcome to Digital Services by <% out.println("Hello Test"); %>
FYI - I can see those sysouts from Controller class in catalina.out.
Put below line in your jsp file.
If this doesn't solve your problem then refer this JSP displaying source code instead of executing
You are using pure HTML in JSP file so snippets won't compile. You are missing page directive. The page directive is used to provide instructions to the container that pertain to the current JSP page. You may code the page directives anywhere in your JSP page. By convention, page directives are coded at the top of the JSP page.
Following is the basic syntax of page directive −
<%@ page attribute = "value" %>