Reputation: 6285
I have successfully deployed my .WAR file to openshift.com. In order to access my website, I have to type this url: http://demo-farazdurrani.rhcloud.com/main
You see that after ".com" I have to type "/main". When I only type the website name without '/main', it throws '404 resource not found' error. I just want to be able to just type http://demo-farazdurrani.rhcloud.com/ (or better yet remove that '/' too) and it should automatically open my main home page.
This is my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-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"
metadata-complete="false">
<display-name>Advanced Mappings Demo Application</display-name>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspf</url-pattern>
<page-encoding>UTF-8</page-encoding>
<scripting-invalid>true</scripting-invalid>
<include-prelude>/WEB-INF/jsp/base.jspf</include-prelude>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
<default-content-type>text/html</default-content-type>
</jsp-property-group>
</jsp-config>
<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<http-only>true</http-only>
</cookie-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
And this is my onStartUp method (I configured it programmatically):
@Override
public void onStartup(ServletContext container) throws ServletException
{
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(RootContextConfiguration.class);
container.addListener(new ContextLoaderListener(rootContext));
AnnotationConfigWebApplicationContext servletContext =
new AnnotationConfigWebApplicationContext();
servletContext.register(WebServletContextConfiguration.class);
ServletRegistration.Dynamic dispatcher = container.addServlet(
"springDispatcher", new DispatcherServlet(servletContext)
);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
container.getServletRegistration("default").addMapping("/resources/*", "*.css", "*.js", "*.png", "*.gif", "*.jpg");
}
Any help is appreciated. Thanks
Upvotes: 1
Views: 157
Reputation: 6285
Okay now I know how to do it. First step is this: https://stackoverflow.com/a/23981486/4828463 Although rename my .war file to ROOT.war was important, I was missing one important .jsp file at a important location (now this solution is particular to my problem. It is possible that it could be helpful to you as well). I added "index.jsp" at the root of my web application. And Inside it I have
<%@ page session="false" %>
<c:redirect url="/main" />
The main thing that's needed here is the 2nd line. It will redirect all the calls to http://app-domain.rhcloud.com/ to http://app-domain.rhcloud.com/main, which I actually need. I just type http://demo-farazdurrani.rhcloud.com/ and it redirects it to http://demo-farazdurrani.rhcloud.com/main
First line is optional (not needed for my solution) and is doing this: It is needed page that doesn't need to be involved in a session. Or if this code is specified in a JSP page, it means session objects will not be available for that page. Hence session cannot be maintained for that page. (just putting it out there not neccassary for my question though).
I will show you how my project structure looks like now:
Earlier in the question I was asking if I need to change anything in web.xml. No I don't. Everything is fine there.
upvote the question and answer if you found this helpful. Thanks
Upvotes: 0
Reputation: 2242
with spring applications usually what is suggested is to use a rewriter of url like:
in your web.xml
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>yourservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>yourservlet</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
by default will read a urlrewrite.xml in your WEB-INF directory you'd have something similar to:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite
PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
"http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite default-match-type="wildcard">
<rule>
<from>/resources/**</from>
<to>/resources/$1</to>
</rule>
<rule>
<from>/spring_security_login</from>
<to last="true">/spring_security_login</to>
</rule>
<rule>
<from>/j_spring_security_check**</from>
<to last="true">/j_spring_security_check/$1</to>
</rule>
<rule>
<from>/j_spring_security_logout**</from>
<to last="true">/j_spring_security_logout/$1</to>
</rule>
<rule>
<from>/**</from>
<to>/app/$1</to>
</rule>
<outbound-rule>
<from>/app/**</from>
<to>/$1</to>
</outbound-rule>
</urlrewrite>
that should be enough, I use this very same configuration for a deployment in openshift! and works the way you want
Upvotes: 1