Jersey MVC: jsp issue: The requested resource is not available - jersey

I'm working in:
Eclipse (Version: Neon.1a Release (4.6.1))
, using:
Jersey 2.25.1
Jersey MVC
Apache Tomcat 7.0.76.
Here's the structure of my application:
Classes:
Jsp:
I'm configuring my application by extending ResourceConfig:
AppConfigs.java.
Here's the resource class:
ForumsController.java
The problem is that I get 404-Not found (for: http://localhost:8080/StudSiteProj/).
Can you, please, help me understand what's the cause?
I specified an absolute path in return new Viewable(..), so I guess the application should search for index.jsp using this path. Why isn't this working then?

I finally found the solution.
I found 2 problems:
First problem
Cause:
The way #ApplicationPath annotation was written.
Conclusion:
It should not be like:
or
or
Solution:
So now my AppConfigs class is:
Second problem
Cause:
JspMvcFeature.TEMPLATE_BASE_PATH property was not set.
Conclusion:
I understood that this property might be what's missing by looking in apidocs here. It said that:
So, if there isn't one, I have to set some.
Solution:
I set it to: /WEB-INF/app/views
Now, my AppConfigs class looks like this:
The resource class ForumsController is now:
See, it's only:
, because the value in JspMvcFeature.TEMPLATE_BASE_PATH will be prefixed to it. So "index.jsp" will be searched in:
This path is relative to servlet context path ( ex.: for my project, named StudSiteProj, the context path is /StudSiteProj).
Here I worked with absolute paths (paths specified in Viewable).
But there exists another approach in Jersey MVC (relative paths).
Here's my answer to my question =) on another stackoverflow page.

Related

Spring Boot doesn't create Service bean and bypasses it's activity in Controller

Project structure:
Here is the repository (no class exceeds 20 lines of code): https://github.com/MoskovchenkoD/spring5-jokes
Here is the problem: Service implementation isn't used, and 'joke' attribute doesn't get printed on the page (just '123'). Controller's #RequestMapping method is simply ignored or bypassed.
How to fix it? I was following a step-by-step video from generating a project at start.spring.io to launching it.
Much appreciated!
Yet another childish error =(
I moved the Application class one level up and now it works fine.

pageContext.request.contextPath and generic linking

In reference to my older unanswered question, I have a further error / problem that is partially related to it.
generic linking, variables and paths in jsp
When I include my header.jsp like this:
<%#include file="/WEB-INF/view/jsp/common/header.jsp" %>
It works fine.
But does not if I do it like this:
<%#include file="${pageContext.request.contextPath}/view/jsp/common/header.jsp" %>
Error:
HTTP Status 500 - /WEB-INF/view/jsp/common/login/login.jsp (line: 8, column: 1) File "${pageContext.request.contextPath}/view/jsp/common/header.jsp" not found
The above with ${} is a proper way and thats what I had been doing in the past until I started using spring and spring security.
But I dont think its a problem of spring or spring security.
I really cant understand why WEB-INF has a weightage and
How can I make my links generic (ref my old stated question)
Actually, it's not the proper way in this case. The context path is not used for creating paths within the internals of your app, as in this case. Instead, it is used to generate URLs within the view layer, such as links, form URLs, and etc.
In this case, the following is correct:
<%#include file="/WEB-INF/view/jsp/common/header.jsp" %>
The include line above is referring to a resource that is packaged inside your war file. How would the context path, which is configured at the servlet container, have anything to do with the location of something packaged inside your war?
To recap, you only need to use ${pageContext.request.contextPath} to prefix URLs that are going to be placed in output to the client.

Is it possible to set a system path as spring context

I want to load my Spring application context via annotations like this:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "file:/path/to/spring-context1.xml", "file:/path/to/spring-context2.xml"})
I am trying this because the context file is not in my classpath.
I've read several times that this was possible but it does not seem to work.
So my question is: "Is this really possible and am I doing something wrong?
I've got another question if this happens to be possible: if I want to use a relative path instead of an absolute one, from where should it be relative? From the pom.xml location?
As said in the comments of my first post, the problem came from me ;)
So the answers are :
yes, you can use the "file:" prefix to set your Spring context
yes, you can use relative path, relative to the pom.xml directory
Hope this helps.

Spring: Difference of /** and /* with regards to paths

What's the difference between two asterisks instead of one asterisk when we refer to paths?
Earlier I was debugging my Spring 3 project. I was trying to add a .swf using
<spring:url var="flashy" value="/resources/images/flash.swf"/>
With my web.xml's ResourceServlet looking like
<servlet-name>Resource Servlet </servlet-name>
<url-pattern>/resources/*</url-pattern>
But unfortunately I was getting this error:
WARN org.springframework.js.resources.ResourceServlet - An attempt to access a protected resource at /images/flash.swf was disallowed.
I found it really strange since all my images in the images folder were accessed but how come my .swf was "protected"?
Afterwards, I decided to change the /resources/* to /resources/** and it finally worked. My question is... why?
This is a path pattern that is used in Apache Ant library. Spring team implements it and uses it throughout the framework.
Back to your problem. According to the Javadoc for AntPathMatcher, it only has 3 rules:
? matches one character
* matches zero or more characters
** matches zero or more 'directories' in a path
UPDATE 2022
In the latest Spring Framework versions there is a forth rule:
{spring:[a-z]+} matches the regexp [a-z]+ as a path variable named "spring"
See the details in the latest (as of now) Spring Framework version 5 Javadoc: AntPathMathcer.

Spring application-context, not able to load (property-placeholder) .properties file

I have a web-app, which loads a application-context files from many locations.
One of the application-context file is in a .jar file (this jar is present in WEB-INF/lib).
This application-context has an entry like this:
<context:property-placeholder location="classpath:META-INF/spring/default.app.properties" ignore-unresolvable="true" ignore-resource-not-found="true"/>
But the default.app.properties is never found. I keep getting errors about
Could not resolve placeholder 'db.driver' - something that is defined in default.app.properties and referred in application-context via ${db.driver}
It is almost as if property-placeholder is being ignored. I tried giving absolute path to my default.app.properties too.. even that wouldn't work.
Have you solved this problem? I've encoutered the same recently. My solution is simple and unlikely to be the case but... in my case there were two types of placeholders of different types. One type was configured using
<context:property-placeholder/>
the other type was configured as a bean of type ServletContextPropertyPlaceholderConfigurer. Removing one type of placeholder solved the problem.
Because Spring allow exist only one <context:property-placeholder/>, When Spring find a <context:property-placeholder/>,it will ignore the remains. So put all the properties conf in one place.
reference to :http://www.iteye.com/topic/1131688

Resources