I am utilising the Bootstrap front end framework, although I am having some trouble getting the icons to load. I have implemented Spring security, I am not sure whether this may be affecting them not to load. I am calling my .css and .js files like this:
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/style/bootstrap.css" />
<script src="${pageContext.request.contextPath}/static/js/jquery.js"></script>
This is the configuration that I have in my Dispatcher servlet config:
<mvc:resources location="/resources/" mapping="/static/**" />
This is really irritating, can someone please help. Thanks
In your spring security config, have you excluded your resources directory containing your css, js, images from the secured zone ?
<http use-expressions="true" security="none" pattern="/resources/**">
</http>
If the problem is only related to the twitter bootstrap icons, have you included the files glyphicons-halflings.png glyphicons-halflingswhite.png in a resources/img folder ?
Related
I use eclipse, springmvc and maven. I created a folder under
Web-Content --> WEB-INF --> resources
I added the following into my servlet:
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
<mvc:annotation-driven />
Then I try to include a picture into my index.jsp file.(Web-Content->index.jsp)
<img src = "/resources/images/Logo.png" />
But it's not loading my image. I tryed many tutorials but it doesn't work. I guess I'm something basic wrong. Can you tell me whats wrong?
EDIT:
my serlvet got this
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
In my jsp file I did this:
<img src="<c:url value="/resources/logo.png" />"/>
I got resource folders with the logo.png file in the following places:
main->resources
main->webapp->resources
main->webapp->WEB-INF->resources
It still doesn't work
I also tried :
<mvc:resources mapping="/resources/**"
location="/, classpath:/WEB-INF/public-resources/"
cache-period="10000" />
and include the picture with
/resources/images/logo.png
and the picture located in
src/main/webapp/images/logo.png
doesnt't work for me
EDIT:
I guess it this:
<mvc:resources mapping="/resources/**" location="/resources/" />
got no effect to the program. I can always acces the url http://localhost:8080/test/resources/logo.png
doenst matter if I got this mvc line in my serlvet or not. If I change
<mvc:resources mapping="/resources/**" location="/resources/" />
to
<mvc:resources mapping="/resources1/**" location="/resources/" />
I can't acces to the file with http://localhost:8080/test/resources1/logo.png but with http://localhost:8080/test/resources/logo.png
Greets Sam
As an aside, while it is probably not best to make an Eclipse Dynamic Web Project and convert it to a Maven project through Eclipse, it will work, because Eclipse will put an entry into your POM that points to the WebContent directory. Have a look you will probably find it there. There is nothing wrong with making a Dynamic Web Project per se, but I typically change the initial directory configuration to match maven's expectations in the Wizard and convert to a maven project right away. Much easy to work on the POM than copy jars to the lib directory.
Otherwise, the resources directory you referring to should be in the root of your war file. The best way to get it there is to put resources at the root of your WebContent directory, not under WEB-INF. Try moving it there, open the .war file and inspect the contents to be sure it is in the root, and you should be good to go.
I have a Spring Boot Application. I am trying to load to my HTML file a CSS file, but probably I am doing something wrong. In Spring MVC Project I've done something like this:
In HTML file I added the following line:
<link href="<c:url value="/resources/css/style.css" />" rel="stylesheet">
And in mvc-dispatcher-servlet.xml I added following code:
<mvc:resources mapping="/resources/**" location="/resources/"
cache-period="31556926"/>
<mvc:annotation-driven />
And everything worked perfect.
Now I'm using thymeleaf templating language and load my CSS file like that:
<link rel="stylesheet" type="text/css" th:href="#{/assets/css/style.css}" />
My project files hierarchy is following:
In current project I don't have WEB-INF directory so I don't have such a files like mvc-dispatcher-servlet.xml or web.xml so I don't know if I should or where I should paste that code with resources mapping. Im completely new to Spring so I apologise for my lack of knowledge.
Any tips and solutions will be much appreciated.
I am able to use a custom login page with Spring Security 3.2.4, but after migrating with the code below using 4.0.0, I see a generic login form instead of my custom one:
<beans:bean id="authSuccessHandler" class="com.company.web.RoleBasedAuthenticationSuccessHandler" />
<http disable-url-rewriting="false" use-expressions="true">
<form-login login-page="/login"
username-parameter="j_username"
password-parameter="j_password"
login-processing-url="/j_spring_security_check"
authentication-failure-url="/login?login_error=true"
authentication-success-handler-ref="authSuccessHandler"/>
<!-- SOME INTERCEPT-URLs (redacted) -->
<intercept-url pattern="/login" access="permitAll"/>
<remember-me
remember-me-parameter="_spring_security_remember_me"
remember-me-cookie="SPRING_SECURITY_REMEMBER_ME_COOKIE"/>
<logout
logout-url="/j_spring_security_logout"
logout-success-url="/index" />
</http>
I also tried enabling debug logging on the various Spring classes. I set it on my custom authSuccessHandler, but I don't see any output from it. No luck with searching on SO or Google either.
Is there anything incompatible about this configuration?
Update:
I'm also using Apache Tiles as so:
<definition name="login" extends="scrollableLayout">
<put-attribute name="header" value="/WEB-INF/jsp/heading_blue.jsp"/>
<put-attribute name="body" value="/WEB-INF/jsp/login.jsp"/>
</definition>
And using the following:
<mvc:view-controller path="/login" />
Spring Security 3.x used spring_security_login as the default login URL (source: official documentation). This could be set to a custom value as <security:form-login login-page="/login"> and mapped to a controller to render a custom page.
Spring Security 4.x has abandoned spring_security_login and switched to login as the default login URL (source: official Spring Security 4.x migration guide). Therefore, the URL login now goes to the default Spring Security infrastructure, that displays the default, auto-generated login page.
The remedy is simple if you are using JSP as the view rendering technology. Simply rename your login page to login.jsp, drop it in the root folder of the page hierarchy and Spring Security will pick it up automatically. If you are not using JSP, you will have to use a different login-page value (perhaps signin instead of login and then change your controller mapping as well.
Note that the default logout URL has also changed in 4.x. If you have any custom logic written for the logout URL, do make sure to review that as well.
Do review the official migration guide as a lot of things have changed in 4.x.
I'm getting a 404 in Jetty if the mvc:resources tag in my web-context.xml is not mapped to the same filename:
<mvc:resources mapping="/some-file" location="/WEB-INF/js/some-file.js" />
Where as the following works fine:
<mvc:resources mapping="/some-file.js" location="/WEB-INF/js/some-file.js" />
How can I map files to a different file-name in the URL space?
I recommend you don't map single files but an entire directory.
<mvc:resources mapping="/js/**" location="/js/" />
As you can see the mapping attribute takes an ant path to match multiple files and directories.
For more information see http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-config-static-resources
I am new to spring and am trying it.
I have a jsp file which includes css and images as follows
<link href="css/style.css" rel="stylesheet" type="text/css" />
Now i have a url path as /localhost/myapp/test/tst which is mapped to a controller which then forwards it to view jsp
view.jsp is present it
/web-inf/jsp/view.jsp
But when hit the path /localhost/myapp/test/tst the css and images path gets resolved to
/localhost/myapp/test/tst/css/style.css
Actaully i want it to come as /localhost/myapp/css/style.css
css and html are present in root of webapp
How i have following in my spring config
<mvc:annotation-driven />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />
Your CSS href is currently relative to your page, you need to make it relative to the server root by adding a leading slash (plus context, etc). Its also a good idea to add the contextPath so that if/when you change the webapp name or deploy it as a root webapp, the resources still work.
<link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css" />