Spring & static content ( css, js) - spring

I want to create simple app, that would use css and js. But I can't reach that static content... I've found some solutions, but I wasn't able to modify them for my solution :(
Would be anybody please so kind and could show some explicit solution (where to add/modify something and what exactly to add/modify) ?
my app structure in navigator:
http://i.nahraj.to/f/gNc.jpg
Content of web.xml and servlet-context.xml http://pastebin.com/fVcNZPst
I'm running my app on tomcat server.
The home.jsp page is correct, because when I rewrite it to home.html and open it via web browser, it shows correctly. I would really appreciate any help.

There are a few issues in your structure:
a. You have put <resources mapping="/resources/**" location="/resources/" /> for mapping your resources, this would map anything with uri starting with /resources/ to locations under your webapp, in your structure that would be src/main/webapp/resources, you however don't have that folder.
b. If you want your files from webapp/css, webapp/img, webapp/js to be available either you can move them into src/main/webapp/resources folder and access them with say /resources/js/test.js uri or just put this entry into your servlet-context.xml file also - <mvc:default-servlet-handler /> - if you are interested I have provided more details here

Related

Restrict remote access to folder inside tomcat webapp folder

I have Spring Boot micro-service based application hosted on Tomcat 9. So, I have multiple folders deployed in {{tomcat root dir}}/webapps folder. I want only one folder to be accessible from outside network (gateway service). Others folder should be accessible only from localhost.
So, how can I achieve this without adding context.xml? I know this can be achieved by adding context.xml but context.xml is getting overwritten after every war deployment.
So want to check if this can be achieved at more global level like modifying server.xml.
Valves must be defined in a <Context> section, but adding a META-INF/context.xml to your application is not the only way to define a context.
If you want to restrict most applications to localhost, the easiest way is to modify $CATALINA_BASE/conf/context.xml:
<Context>
...
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1"" />
</Context>
or $CATALINA_BASE/conf/<engine_name>/<host_name>/context.xml.default (usually $CATALINA_BASE/conf/Catalina/localhost/context.xml.default) if you wish the changes to apply only to a single host.
Every web application context will inherit the <Valve>s in the aforementioned files. Since you want to have a web application (let's say /pubApp) without these restrictions you need to create a file $CATALINA_BASE/conf/<engine_name>/<host_name>/pubApp.xml and set the override attribute to true:
<Context override="true">
<!-- Remember to copy all configuration values from `conf/context.xml`
and `conf/<engine_name>/<host_name>/context.xml.default`
that you wish to apply, since they will not be read anymore.
-->
...
</Context>

Fail to serve .jsp and .html file via default servlet handler using spring mvc <mvc:default-servlet-handler />

The title should explain the biggest part :)
I should be able for example to access http://www.someurl.com:8080/index.jsp but instead I get HTTP Status 404 - /index.jsp
Now why do I assume I should be able to serve static content (ie not be redirected to custom controller but to default servlet handler in stead.)?
Because I have added the following element to my mvc dispatcher servlet config:
<mvc:default-servlet-handler />
I have read that in some case the name of the default server cannot be guessed and I have looked it up in the file: ̣*~/tomcat7/conf/web.xml .*
The default servlet is specified by a name "default". So I tried adding:
<mvc:default-servlet-handler default-servlet-name="default"/>
But to no avail...
I have one spring dispachter servlet mapped to '/'.
I have two controllers mapped to, one controller is mapped to '/' and one is mapped to '/todo'
The controllers work fine.
I thought the controller mapped to '/' could be the culprit so I removed that controller but to no avail.
Anybody an idea? And is it possible to have a controller mapped to '/' and still serve a page like /index.jsp??
Arf, I had put my static resources under the webapp/WEB-INF folder instead of the webapp folder. Now it seems te be working fine ... :)

How to import css/js in spring MVC application jsp page

I am new to spring mvc , I am creating a user registration page , there I need to put external css and import external js .
I put the script tag with src and link tag with href. But i think in Spring MVC application they will not work straight forward.
For this I added resourceservlet mapping but still it is not working
I am not able to find any way how I csn include these assets in my jsp file.
Please help me to get this one.
Regards,
Pranav
Put the CSS, JS, image files, etc., into a directory (which may contain subdirectories) in your web site, and than map the requests to that directory:
<mvc:resources mapping="/resources/**" location="/public-resources/"/>
In this example the files are in the public-resources directory, and you refer to them using /resources in the link in your JSP.
For more information see Spring docs

How handle resources likes images, *.css with Spring Mvc

I've a problem with handle static resources.
It's my web-app
WebAppRoot
-resources
-css
-style.css
-imahes
-jsp
-template.jsp
-secure
-template_secure.jsp
so
myLocation=resources
How can I handle resource in a central way, for example I put location in a variable and
use this in tag
I tried with but I don't understand how have to use.
If you are using Spring 3.0.4 or later, you can use the < mvc:resources ...> element in your application configuration, and you will not need to create a Controller to handle static content for you.
It is as simple as specifying the location of your static content, and the path from which to access them:
<mvc:resources mapping="/resources/**" location="/public-resources/"/>
This uses the mvc namespace found http://static.springsource.org/schema/mvc/spring-mvc.xsd.

Seam 2.2.0 URL rewrite not working

I'm trying a simple example of URL rewriting, but it's not working for me. I'm using Seam 2.2.0 deployed to JBoss 5.1.0.
My pages.xml contains:
<page view-id="/html/index.xhtml">
<rewrite pattern="/home" />
</page>
and
<page view-id="/html/common/redirect.xhtml" action="#{redirectAction.redirect}">
<rewrite pattern="/link" />
...
My components.xml contains:
<web:rewrite-filter view-mapping="*.seam"/>
As far as I can tell from the documentation, that should be all I need. However, none of the internal links appear different, and if enter the URL: http://mysite/home or mysite/link?param=something, I just get a 404 page. What am I missing?
I had the same problem when I tried to move from the Tuckey Rewrite Filter to Seam. After some trial and error I finally noticed that I had a page.xml for the view I but added the pattern in the main pages.xml. The former seems to overwrite the latter.
From the seam documentation:
"The view-mapping parameter must match the Servlet mapping defined for the Faces Servlet in the web.xml file. If omitted, the rewrite filter assumes the pattern *.seam."
I don't know why you approach is not working. But don't forget that you can use viewName.page.xml file for the same purpose.
For an example since the name of your page is index.xhtml you have to create the index.page.xml file inside the same folder which contains the index.xhtml file(in your case inside the html folder).
In index.page.xml file add this.
<rewrite pattern="/home" />
As you are already doing your component.xml file should contain the line
<web:rewrite-filter view-mapping="*.seam"/>
Deploy again and it should work.

Resources