Getting Error(HTTP Status 404 -) in Spring Mvc - java

I am new in SpringMVC and trying to execute simple hello world program. However, when run this in my browser (http://localhost:8080/FirstspringMVCwithannotation/welcome) I got HTTP Status- 404 Error. Here, is the code:
HelloController.java
package com.example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
#Controller
public class HelloController {
#RequestMapping("/welcome")
public ModelAndView helloWorld(){
ModelAndView model=new ModelAndView("HelloPage");
model.addObject("msg","hello world");
return model;
}
}
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>FirstspringMVCwithannotation</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.example" />
<mvc:annotation-driven/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
HelloPage.jsp
<html>
<body>
<h1>
First Spring MVC Application Demo
</h1>
<h2>
${msg}
</h2>
</body>
</html>
Here is my project structure and I added all spring jar files under lib folder
I tried to see other solution but that does not solved my problem..can anyone help me please, why i get HTTP Status- 404 Error? Thanks in advance

You received HTTP 404 because your dispatcher controller can't find you view.
Your view is under lib folder, which is under WEB-INF folder.
All you need to do is the reconfigure you view resolver, instead of what you wrote:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
write it like this: configure your view resolver to extract views from lib folder
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/lib/" />
<property name="suffix" value=".jsp" />
</bean>
One more thing, I would recommend that you place your jsp files under views folder under WEB-INF and not under lib folder.
lib under WEB-INF is a folder dedicated to 3rd party libraries and jars.

Related

HTTP Status 404 - Not Found in Spring MVC 4 application

I have NetBeans Spring MVC project with Maven.But I got the following error: HTTP Status 404 - Not Found. How can I avoid this error?
Hierarchy:
-FinalWebStore
-Web Pages
-WEB-INF
+views
web.xml
mvc-dispatcher-servlet.xml
-Source Packages
+com.karans.finalwebstore.controllers
+com.karans.finalwebstore.daoimps
+com.karans.finalwebstore.daos
+com.karans.finalwebstore.models
+Dependecies
...
Here are my files:
web.xml:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
mvc-dispatcher-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:mvc="http://www.springframework.org/schema/mvc"
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.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<bean name="/login"
class="com.karans.finalwebstore.controllers.UserController" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/webstore" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="ProductDAOimpl" class="com.karans.finalwebstore.daoimps.ProductDAOimp">
<property name="template" ref="jt"></property>
</bean>
<bean id="UserDAOimpl" class="com.karans.finalwebstore.daoimps.UserDAOimp">
<property name="template" ref="jt"></property>
</bean>
<bean id="CartDAOimpl" class="com.karans.finalwebstore.daoimps.CartDAOimp">
<property name="template" ref="jt"></property>
</bean>
<bean id="UserController" class="com.karans.finalwebstore.controllers.UserController">
<property name="userdaoimp" ref="UserDAOimpl"></property>
</bean>
</beans>
UserController.java:
package com.karans.finalwebstore.controllers;
import com.karans.finalwebstore.daoimps.UserDAOimp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class UserController {
#Autowired
UserDAOimp userdaoimp;
public void setUserdaoimp(UserDAOimp userdaoimp) {
this.userdaoimp = userdaoimp;
}
public String login(){
return "login";
}
}
login.jsp:
<%--
Document : login
Created on : Jul 15, 2017, 2:57:13 PM
Author : raziyeh
--%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>LOG IN PAGE</title>
</head>
<body>
<h1>Hello World Taze avvaleshe!</h1>
</body>
</html>
And I think other files are not important.
Change your login function as
#RequestMapping(value = "/")
public String login(){
return "login";
}
Also change the web.xml file in the below portion :
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
You need to delete the below line from mvc-dispatcher-servlet.xml
<bean name="/login"
class="com.karans.finalwebstore.controllers.UserController" />
Also, make sure that login.jsp file contains under the /WEB-INF/views/ folder.
Try the following:
Add the Spring MVC namespace to your mvc-dispatch-servlet.xml file xmlns:mvc="http://www.springframework.org/schema/mvc". You will need to update the xsi:schemaLocation as well with this "http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
Second add <mvc:annotation-driven /> to the file. If I recall correctly, this enables processing of #RequestMapping annotations.
Third, remove the bean declared with the name "/login". It is the same type as the UserController declared at the bottom of the file.

Spring MVC redirection issue on tomcat 7 running on Debian

I am a newbie to Spring MVC. I am using version 4.1.6 within a webapp. This webapp is running on a Tomcat 7 server. When I run it on windows, everything is just
allright. However, on Debian a redirection problem occurs:
I go to my index page : domain.com/appName
Click on a button that makes a POST request
Get redirected to "domain.com/appName/search" on Windows. But get redirected to
"domain.com/search" on Debian (which makes a 404 error).
Here are my sources:
web.xml
<web-app id="app"
version="3.0"
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">
<display-name>appName</display-name>
<servlet>
<servlet-name>appName</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appName</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
appName-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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.ghayth.vtc" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
<mvc:annotation-driven />
</beans>
SearchController.java
package com.ghayth.vtc.controller;
...
#Controller
public class SearchController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String index(HttpSession httpSession, Model model) {
...
return "index";
}
#RequestMapping(value = "/search", method = RequestMethod.POST)
public String search(...) {
...
return "search";
}
}
index.jsp
...
<form:form role="form" method="post" modelAttribute="searchForm" action="search">
...
<button type="submit">Search</button>
</form:form>
All my JSPs are inside "/WEB-INF/jsp".
Is there something wrong? And is there a specific configuration for tomcat 7 on Debian that must be done?
Thanks!!

Spring MVC CSS/JS not found

In my maven Spring Project in netbeans CSS/Js file not opening ..
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" version="3.0">
<display-name>OrderManager</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>SpringConfiguration</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringConfiguration</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
SpringConfiguration-servlet.xml
e<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.ordermanager.users.controller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/**"/>
Project Folder
i cant able access any css or JS file even after controller and other function working fine ..
but if i open the following url in browser
http://localhost:8080/OrderManager/resources/css/loginpagestyle.css, it's showing not 404 found.
After much search, my 404 issue was resolved by removing the initial "/" in the location setting value. So instead of
<mvc:resources mapping="/resources/**" location="/resources/"/>
as in the previous answer, I used:
<mvc:resources mapping="/resources/**" location="resources/"/>
Small change, but it did the trick. I'm assuming the "/" added an additional folder level to the path. Also, I placed the resources folder directly under the webapp folder.
Thank you to Suda's Tech Zone for helping me with this issue.
Using Maven 4.0.0, eclispse kelper, java 1.7, mybatis 3.1.1.
Your resource mapping is wrong. Should be:
<mvc:resources mapping="/resources/**" location="/resources/"/>

Where Should I put beans.xml content (Jdbc Connect with Spring-Eclipse Dynamic webapplication)

Hello I am new to Spring and I am developing a Dynamic web application with Eclipse Kepler and Spring mvc. I opened the project as dynamic web project. So I have web.xml and spring-servlet.xml configuration files. When I tried to connect mysql datadase with jdbc connector I saw that there are some new Spring configurations about the connection in a Beans.xml file.
So I have 3 xml configuraton files: web.xml, spring-srvlet.xml and Beans.xml. My question is :Can I put content of bean.xml in web.xml. or spring servlet.xml? And If I do that whic changes should I do in my controller java class that I am calling Beans.xml file in that class.
Eclipse Kepler,
Spring 3.2.4
Apache Tomcat 7
Any Help will de appreciated.
web.xml contents
<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>DomainYonetim</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.xml contents
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan
base-package="Ekle" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/domain_yonetim"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/Ekle/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Beans.xml contents:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
<!-- Initialization for data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/domain_yonetim"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<!-- Definition for studentJDBCTemplate bean -->
<bean id="domainJDBCTemplate"
class="Ekle.domainJDBCTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
And finally my Controller Class:
package Ekle;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
#Controller
public class DomainEkleController {
#ModelAttribute("Domain")
public Domain getDomain()
{
return new Domain();
}
#RequestMapping(value="/DomainEkle")
public ModelAndView domainEkle() {
String message = "Hello World, Spring 3.0!";
ModelAndView domain_ekle= new ModelAndView("DomainEkle", "message", message);
return domain_ekle;
}
#RequestMapping(value="/DomainEkle",method=RequestMethod.POST)
public ModelAndView domain_eklendi_fonksiyon(#ModelAttribute("Domain")Domain domain, ModelMap model)
{
ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
DomainJDBCTemplate domainJDBCTemplate=(DomainJDBCTemplate)context.getBean("studentJDBCTempate");
domainJDBCTemplate.listDomains();
model.addAttribute("domain", domain.getDomain_adi());
model.addAttribute("sunucu", domain.getSunucu_no());
model.addAttribute("tarih", domain.getTarih());
System.out.println(domain.getTarih()+"-"+domain.getDomain_adi());
String message="Domain Kaydi Yapilmistir!";
ModelAndView dm_eklendi=new ModelAndView("DomainEkle","message",message);
return dm_eklendi;
}
}
You can add all your beans from beans.xml to spring-servlet.xml by using import as below.
<import resource="Beans.xml"/>
also I recommend you to rename this file to some meaningful name - db-context.xml(or something similar).
Also never use below code when you are using annotation configuration/ rather when your context is already built by spring mvc.
ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
Instead use #Autowired annotation. You can also use #Qualifier annotation additionally to pick up correct bean. As below.
private DomainJDBCTemplate domainJDBCTemplate;
#Autowired
#Qualifier("studentJDBCTempate")
public void setDomainJDBCTemplate(DomainJDBCTemplate domainJDBCTemplate) {
this.domainJDBCTemplate = domainJDBCTemplate;
}
And your request processing method should use this template directly without building context again(Its useless to build context every time you hit the url!!!!) as below.
#RequestMapping(value="/DomainEkle",method=RequestMethod.POST)
public ModelAndView domain_eklendi_fonksiyon(#ModelAttribute("Domain")Domain domain, ModelMap model)
{
this.domainJDBCTemplate.listDomains();
model.addAttribute("domain", domain.getDomain_adi());
model.addAttribute("sunucu", domain.getSunucu_no());
model.addAttribute("tarih", domain.getTarih());
System.out.println(domain.getTarih()+"-"+domain.getDomain_adi());
String message="Domain Kaydi Yapilmistir!";
ModelAndView dm_eklendi=new ModelAndView("DomainEkle","message",message);
return dm_eklendi;
}
Add in your spring-servlet.xml file like below.
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-xml-import
<beans
--------------------------
------------------------------
<import resource="classpath:Beans.xml" />
<context:component-scan
base-package="Ekle" />
</beans>

Spring jsp page not evaluated

I have an interesting problem with Spring 3.1.0. JSP pages are not having their EL evaluated. I've debugged the view resolution process and JstlView is being used and the Jstl libraries are detected. However, my JSP pages just render things like
<%="Hello World!"%>
There are lots of references on here about this problem, none of which have worked for me.
Script tags not rendered in JSP page (using Spring + Tiles + JSPX)
Spring and JSP EL not being processed
From the top, here's my config;
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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
</web-app>
spring-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!--
https://stackoverflow.com/questions/3652090/difference-between-applicationcontext-and-spring-servlet-xml-in-spring
-->
</beans>
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.csc.fs.emea" />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<mvc:resources mapping="/static/**" location="/" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver" p:order="1" />
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" p:order="2" p:defaultErrorView="sorry" />
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
<property name="freemarkerSettings">
<props>
<prop key="default_encoding">UTF-8</prop>
<prop key="output_encoding">UTF-8</prop>
<prop key="auto_include">macros.ftl</prop>
<prop key="auto_import">spring.ftl as spring</prop>
<prop key="template_update_delay">${freemarker.template.update.delay}</prop>
</props>
</property>
<property name="freemarkerVariables">
<props>
<prop key="googleAnalyticsId">${google.analytics.id}</prop>
</props>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="contentType" value="text/html;charset=UTF-8"></property>
</bean>
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".html" />
<property name="contentType" value="text/html;charset=UTF-8"></property>
<property name="exposeSpringMacroHelpers" value="true" />
</bean>
</list>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
</bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"></bean>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
</beans>
I'm using the ContentNegotiatingViewResolver as I have some freemarker stuff and REST in there too.
Controller
#RequestMapping("/")
#Controller
public class RootResource extends AbstractResource {
...abridged...
#RequestMapping(value="/jsp", method = RequestMethod.GET, produces = "text/html")
public String getJSP(final Model m) {
return "example";
}
}
example.jsp
<%# page isScriptingEnabled="true" isELIgnored="false" %>
<html>
<head>
<title>Hello World JSP Page.</title>
</head>
<body>
<font size="10"><%="Hello World!"%></font>
</body>
</html>
I return "example" as the view name from my controller and you can see in the logs it resolves to the correct WEB-INF/jsp/example.jsp
22:35:13,049 DEBUG [1168657208#qtp-1342126426-0] [org.springframework.web.servlet.view.ContentNegotiatingViewResolver] Returning [org.springframework.web.servlet.view.JstlView: name 'example'; URL [/WEB-INF/jsp/example.jsp]] based on requested media type 'text/html'
22:35:13,050 TRACE [1168657208#qtp-1342126426-0] [org.springframework.web.servlet.view.JstlView] Rendering view with name 'example' with model {} and static attributes {}
22:35:13,054 DEBUG [1168657208#qtp-1342126426-0] [org.springframework.web.servlet.view.JstlView] Forwarding to resource [/WEB-INF/jsp/example.jsp] in InternalResourceView 'example'
So everything looks ok, it's just that the JSP page is never evaluated correctly.
example.jsp looks like this
<%# page isScriptingEnabled="true" isELIgnored="false" %> <%="Hello World!"%>
I'm using the Maven jetty 6 plugin to run the webapp.
I'm sure I'm missing something simple, it's probably one of those "looked at it for too long" issues.
Thanks for any pointers you can give.
Update 1 - I've just changed the Spring servlet mapping from /* to /spring/* and now it works. So there's some detail around the spring servlet being mapped to /* that I've missed.
Absolutely a case of "looked at it for too long".
The spring servlet needs to be the default servlet. ie mapped to / and not /*.
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

Categories