My welcome-file is returning an empty page in Open Liberty - websphere-liberty

On Open Liberty 21.0.0.6., instead of presenting my welcome JSF page, my browser returns empty content when I point it to http://<host>:<port>/<ctxRoot>,
web.xml
<web-app id="WebApp_ID" version="4.0"
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">
<display-name>MyJSF</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
and a feature set of:
server.xml
<server>
<featureManager>
<feature>jaxrs-2.1</feature>
<feature>cdi-2.0</feature>
<feature>jpa-2.2</feature>
<feature>jdbc-4.3</feature>
<feature>jsf-2.3</feature>
<feature>mpHealth-3.0</feature>
</featureManager>
ADDITIONAL INFO
I also have some other stuff in my app like JPA and JAX-RS.

ROOT CAUSE - JAX-RS application with "/" application path
In my case the problem is that I had a JAX-RS Application configured to use: #ApplicationPath("/") which was colliding with my goal of having the "/" application path serve up the welcome-file.
SOLUTION
Move the JAX-RS app to its own path within the WAR, e.g:
import javax.ws.rs.core.Application;
#ApplicationPath("/api")
public class TestApp extends Application { }
THOUGHTS
This was easy to stumble into by taking an existing simple JAX-RS app using "/" as the app path, and then adding JSF to it. Then I thought I was having trouble with the JSF implementation.

Related

Creating a route with Spring in Netbeans

I know this is a pretty basic issue, but I struggle hard with it...
I'd like to create a bunch of routes with the Spring framework in the Netbeans IDE, I have created a test #Controller class:
#Controller
public class HelloController {
#RequestMapping("/test")
public ModelAndView thisIsATest(HttpServletRequest request) {
return (new ModelAndView("myTestPage.jsp"));
}
}
myTestPage.jsp is a JSP file in the WEB-INF/jsp/ folder, and the HelloController class is in the Source Packages/ folder within a controller java package.
When I start the server, I can acces the root '/index.htm' that displays the index.jsp page (from the redirect.jsp file), but when I try to access '/test' or '/test.htm' I get a 404 error...
I really don't know how to make a Spring controller to work, and I did many tutorials without success.
In your applications web.xml make sure you have something like this. This will allow access to all your jsp pages. Add any other filters you may need here too.
<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">
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
</web-app>

ignored declarative Security in IBM WebShere application server

I have a spring MVC rest application that is deployed as a war file to IBM WebSphere application server v 8.5, i want to secure some of the rest api in this application, hence, i used the application web.xml and declare the security role i want, then i enabled the application security from the WAS console, but for some reason my security roles are ignored and i can access all rest API that are supposed to be secured, any help is appreciated.
<?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">
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>LBS_System</web-resource-name>
<url-pattern>/LBS/*</url-pattern>
<http-method>POST</http-method>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>Administrators</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>defaultWIMFileBasedRealm</realm-name>
</login-config>
<security-role>
<role-name>Administrators</role-name>
</security-role>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<resource-ref>
<res-ref-name>jdbc/MoictDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
</resource-ref>
<persistence-unit-ref>
<persistence-unit-ref-name>persistence/MoICTAppUnit</persistence-unit-ref-name>
<persistence-unit-name>MoICTAppUnit</persistence-unit-name>
</persistence-unit-ref>
</web-app>
You should not include your context-root (LBS in your case) in the url-pattern. It is relative to your application context-root. The /* pattern protects all urls, but only in your application, not others. So if you just want to protect for example rest api, it is usually mapped to some sub path e.g. /LBS/rest/something, in that case you would put /rest/* in the pattern.
You should not include context-root in any mappings and url patterns in the web.xml, especially that application might be deployed under different context-root and in that case it would be broken.

Spring Websocket, 404 error while connecting. App not using Spring MVC

I'm trying to get working the Spring Example tutorial:
https://spring.io/guides/gs/messaging-stomp-websocket/
But I need it working on an existing Application that I need to add WebSocket support. The idea is once I get the basic working, start building from there.
The differences I have with the example in the URL is:
I'm not using SpringBootApplication, but Tomcat instead (7.0.69)
That implies I do have a web.xml (included below)
I skipped the Application.java with the main... I'm building a WAR and deploying it manually into Tomcat.
Whem I start Tomcat I read the following which seems relevant:
03:06:52,908 INFO SimpleBrokerMessageHandler:157 - Starting...
03:06:52,908 INFO SimpleBrokerMessageHandler:260 - BrokerAvailabilityEvent[available=true, SimpleBrokerMessageHandler [DefaultSubscriptionRegistry[cache[0 destination(s)], registry[0 sessions]]]]
03:06:52,913 INFO SimpleBrokerMessageHandler:166 - Started.
The problem is the following:
When I click on connect on the index.html, I get a 404 when I'm trying to reach the server at the mapping url "/hello" (not sure why info is there, I guess is part of the protocol..) http://localhost:8080/hello/info
I was googling this, even found some answers in stackoverflow. Some have fixed this addding as prefix the DispatcherServlet mapping to the websocket url...
However my app isn't using Spring MVC, so I don't have a DispatcherServlet configured... and I looks like an overkill to include it just for the WebSockets.
I tried adding the annotation #EnableWebMvc to the WebSocketConfig class (in the link is the code, I have the same lines)
Any suggestion will be much appreciated !
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
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>App</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
I give up trying to avoid having an MVC Dispatcher for an APP witch doesn't use Spring MVC.
This solved it:
web.xml
<servlet>
<!-- Only used by websockets -->
<servlet-name>SpringMVCDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/websockets-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVCDispatcherServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
And then, in the index.html I only added the prefix /ws/ when connecting (only there, sinding messages without the prefix is ok).

Integrating Crystal Report with Spring Framework - JSP

I am integrating my Crystal Report code with Spring Framework.
Initially I had developed simple web application with only jsps (without spring feature) and CRystal Reports are rendering properly. I had tested it with parameters and DB connection Also.
Now I am trying to integrate it with Spring framework. I did all necessary set-up.
Reports are rendering properly, with parameters and DB connection. But when I click on subreport link I am getting error.
"The viewer is unable to connect with the CrystalReportViewerServlet
that handles asynchronous requests. Please ensure that the Servlet and
Servlet-Mapping have been properly declared in the application's
web.xml file."
Also, images on reports are not displaying (showing cross).I tried to search on different forums but no luck.
Here is my 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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>CRWeb1</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>crystal_image_uri</param-name>
<param-value>/reports/crystalreportviewers</param-value>
</context-param>
<context-param>
<param-name>crystal_image_use_relative</param-name>
<param-value>reports</param-value>
</context-param>
<servlet>
<description></description>
<display-name>reports</display-name>
<servlet-name>reports</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet- class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>reports</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<display-name>reports</display-name>
<servlet-name>CrystalReportViewerServlet</servlet-name>
<servlet-class>com.crystaldecisions.report.web.viewer.CrystalReportViewerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CrystalReportViewerServlet</servlet-name>
<url-pattern>/CrystalReportViewerHandler</url-pattern>
</servlet-mapping>
</web-app>
Thanks,
Sarika

Can I initialize Spring via Tomcats web xml (../webapps/WEB-INF/web.xml)?

Thanks to the UNUSUAL deployment of Tomcat6 I need to deploy my web app that is using the Spring & Hibernate on a server where access to the WAR deployment's web.xml at /webapps/MyDeployment/WEB-INF/web.xml is not allowed.
So I need to know if such a deployment is even possible, Where we will initialize the spring framework in the serever's web.xml at /webapps/WEB-INF/web.xml and not use the WAR's web.xml ?
Below is the WAR's web.xml that I am currently using at my environment,
<?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>abcd</display-name>
<!--Here we specify about the DispatcherServlet class in the Web Deployment
Descriptor -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.abcd</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.xyz</url-pattern>
<url-pattern>*.pqr</url-pattern>
</servlet-mapping>
Unfortunately changes in the Tomcat configuration is not allowed,
Any suggestions will be highly appreciated.
Thank you for all your help, but we ended up solving the problem by asking the server administrator to lift some of the restrictions that had been put regarding file access to the tomcat user.
Now we can access the domain web xml aswell as the server web xml.

Resources