Base URL in Internet Explorer and JSP - java

Internet Explorer doesn't support HTML <base> tag and even other browsers do, there are some problems when redirect takes place inservletsto some.jsppages for examplerequest dispatching.`
It's feasible to add ${pageContext.request.contextPath} with each URL
nor request.getServletPath()
JSP relative links for CSS and images with servlets forwarding may change things a lot. This link : Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
Is there a better approach with JSP / servlets or it's just an IE issue?
Link : HTML <base> TAG and local folder path with Internet Explorer
And if it is an IE issue:
1. how to fix the IE issue as the above post is unable to give a valid answer?
2. how to solve it with JSP / servlets?
My website is now showing CSS and images.
E.g. HTML output is:
<base href="http://localhost:8080/Alpinema/" /> is not working for
<link media="all" rel="stylesheet" type="text/css" href="css/all.css">
It works in other browsers like Firefox and Chrome.
My JSP code portion:
<head>
<base href="${fn:substring(url, 0, fn:length(url) - fn:length(uri))}${req.contextPath}/" />
<meta charset="utf-8">
<title>Alpinema.com</title>
<link media="all" rel="stylesheet" type="text/css" href="css/all.css">
/css?family=Merriweather|PT+Sans:700|Nobile:400italic' rel='stylesheet' type='text/css'>
</head>

Use <c:url> tag from JSTL to reference CSS/JavaScript resources inside my JSP files. By doing so you can be sure that the CSS/JavaScript resources are referenced always relative to the application context (context path).
Example
index.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>Some Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="<c:url value="/css/main.css" />" />
<script type="text/javascript" src="<c:url value="/js/utils.js" />"></script>
<script type="text/javascript" src="<c:url value="/js/jquery-1.8.3.js" />"></script>
</head>
<body>
...
</body>
</html>
For even more solutions see my answer here:
Adding external resources (CSS/JavaScript/images etc) in JSP.

Related

Java Spring Framework-Bootstrap Integration

I just started learning spring framework.
I have met a problem and decided to share it to help you.
First I added the project bootstrap library for a project.
Then I downloaded a free template from the Internet. I put the resources of this template in the project in the following location (see WebContent / WEB-INF / view / resources)
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>SB Admin - Start Bootstrap Template</title>
<!-- Bootstrap core CSS-->
<link href="${contextPath}/WebContent/WEB-INF/view/resources/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom fonts for this template-->
<link href="${contextPath}/WebContent/WEB-INF/view/resources/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- Custom styles for this template-->
<link href="${contextPath}/WebContent/WEB-INF/view/resources/css/sb-admin.css" rel="stylesheet">
</head>
Then I copied the codes of the login page from the source code into the project and defined the locations of the files used for HTML at the top.(you can see codes below)
When I compile and run my code, I do not have libraries added and I see a page with simple HTML code. What is wrong with me? How can I fix this problem? If you tell me my mistakes and you help me, I'm happy. Everyone thanks in advance
I solved the problem. First, in my project "application-context.xml"
I added the following code to the page. I moved my template files to "/ WebContent / resources / thema /" :
<mvc:resources mapping="/resources/**" location="/resources/thema/"
cache-period="31556926"/
I defined contextPath :
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
Don't forget to define this library for this :
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
After, I changed the path to the template folder in my .jsp file as follows:
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>SB Admin - Start Bootstrap Template</title>
<!-- Bootstrap core CSS-->
<link href="${contextPath}/resources/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom fonts for this template-->
<link href="${contextPath}/resources/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- Custom styles for this template-->
<link href="${contextPath}/resources/css/sb-admin.css" rel="stylesheet">
</head>
This solved my problem.
I got help here for the solution:
Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
and
http://www.mkyong.com/spring-mvc/spring-mvc-how-to-include-js-or-css-files-in-a-jsp-page/
Thank you for your good help : Jorge Campos

How to reference the CSS path properly?

I work with a Java/Spring project and the project structure is provided below,
I need to reference the app.css in the index.jsp file. Currently, I reference the following way in the index.jsp and the app doesn't get the CSS information's.
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Bitcoin Wallet</title>
<link href="${pageContext.request.contextPath}/resources/css/app.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
</html>
In the dispatcher-servlet.xml, I provided the resources path as below,
The page I get in the localhost clearly doesn't have the css. How to solve the issue?
To use resources in spring mvc project you have to declare mvc:resources, to map url to a physical file path location.
<mvc:resources mapping="/resources/**" location="/resources/css/"
When you use /resources/ in jsp file spring will map this url to /resources/css/.
Next you can reference css file in jsp file:
<link href="<c:url value="/resources/css/app.css" />" rel="stylesheet">
And move resources folder into webapp folder as

Does Thymeleaf have something like JSP tags?

I don't mean taglibs, I'm using a JSP tag to do something like this:
ChildPage.jsp:
<%# page contentType="text/html" pageEncoding="UTF-8" %>
<%# taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:layout>
<jsp:attribute name="head">
<link href="css/custom.css" type="text/css" rel="stylesheet"/>
</jsp:attribute>
<jsp:attribute name="scripts">
<script src="js/custom.js"></script>
</jsp:attribute>
<jsp:body>
<p>This is from the child page</p>
</jsp:body>
</t:layout>
layout.tag:
<%# tag description="Layout template" pageEncoding="UTF-8" %>
<%# attribute name="head" fragment="true" %>
<%# attribute name="scripts" fragment="true" %>
<!DOCTYPE html>
<html lang="en">
<head>
<link href="css/main.css" type="text/css" rel="stylesheet"/>
<jsp:invoke fragment="head"/>
</head>
<body>
<div id="body">
<p>This is from the parent or "layout"</p>
<jsp:doBody/>
</div>
<div id="footer">
<script src="js/main.js"></script>
<jsp:invoke fragment="scripts"/>
</div>
</body>
</html>
When rendered:
<!DOCTYPE html>
<html lang="en">
<head>
<link href="css/main.css" type="text/css" rel="stylesheet"/>
<link href="css/custom.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div id="body">
<p>This is from the parent or "layout"</p>
<p>This is from the child page</p>
</div>
<div id="footer">
<script src="js/main.js"></script>
<script src="js/custom.js"></script>
</div>
</body>
</html>
This allows me to include scripts in the header section of the JSP from both the layout and child pages. Same for the body and footer.
I've read through Thymeleaf docs/examples but maybe I'm not understanding correctly as it doesn't look like I can do what I am trying to achieve.
The reason why I have "inverted" what seems like a simple include is every page that I have includes certain scripts and a header section, but my child pages also have scripts to be imported and stylesheets to be included.
Can I achive this somehow? Am I doing this wrong?
By default Thymeleaf uses so called Include-style layouts. Disadvantages of this approach explained on official site. I would advise you to use Thymeleaf Layout Dialect. This is much more convenient dialect to create Hierarchical-style layouts.
By the way, in Layout Dialect all content of <head> tag will be merged automatically. Just take a look on example.

javax.el.ExpressionFactory.newInstance() not found on Jetty and Java 7

I have this JSP:
<%# page pageEncoding="UTF-8" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="contextPath" value="http://localhost:8000/"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<link type="text/css" rel="stylesheet" href="${contextPath}/css/styles.css"/>
</head>
<body>
<!-- some html -->
</body>
When I navigate to this page, I get next error:
java.lang.NoSuchMethodError: javax.el.ExpressionFactory.newInstance()Ljavax/el/ExpressionFactory;
at org.apache.jasper.compiler.JspUtil.getExpressionFactory(JspUtil.java:1182)
at org.apache.jasper.compiler.JspUtil.validateExpressions(JspUtil.java:644)
at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:752)
at org.apache.jasper.compiler.Node$ELExpression.accept(Node.java:946)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2291)
at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2341)
at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2347)
at org.apache.jasper.compiler.Node$Root.accept(Node.java:498)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2291)
[...]
The environment to execute this page is Java 7 and Jetty.
If I remove ${contextPath} works fine.
Why i have this error?
Best regards
The cause of your error is 2 different EL language jars in your classpath.
EL version 2.1 is what is actively being used by your webapp, and is the cause of your error (that method doesn't exist in EL 2.1)
EL version 2.2 is what your webapp needs to be using for javax.el.ExpressionFactory.newInstance(), as that method was introduced in EL 2.2
Make sure you are using the correct version of the EL lib, and that you don't have multiple versions of it present in your webapp
Also, why not just use ...
<link type="text/css" rel="stylesheet"
href="${pageContext.request.contextPath}/css/styles.css"/>
and skip the whole <c:set> you are using? (your choice of a fully qualified URI is a bad idea anyway)

Thymeleaf namespace in html files display errors in Netbeans - How can I get it pass HTML checking?

Netbeans HTML checking doesn't like my thymeleaf namespace.
Here is my HTML Thymleaf file:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="head">
<object th:include="fragments/meta :: meta" th:remove="tag" />
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700|Open+Sans:300italic,400,300,700' rel='stylesheet' type='text/css'/>
<link href="/../../../webjars/Semantic-UI/2.0.6/semantic.min.css" rel="stylesheet" th:href="#{/webjars/Semantic-UI/2.0.6/semantic.min.css}"/>
<link href="../../../css/core.css" rel="stylesheet" th:href="#{/css/core.css}" />
<link href="../../../css/product.css" rel="stylesheet" th:href="#{/css/product.css}" />
</head>
<body>
</body>
</html>
This is the error displayed in Netbeans:
How can I get Netbeans to play nicely with Thymeleaf?
Try to fix it in this way you may ignore spring security thing.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="utf-8"/>
<title>Sample</title>
<link th:href="#{/resources/css/bootstrap.css}" rel="stylesheet" type="text/css"/>
<link th:href="#{/resources/css/bootstrap-theme.css}" rel="stylesheet" type="text/css"/>
</head>
I found out how to disable these errors when I was taking this spring security tutorial
Simply by adding this to html tag:
xmlns="http://www.w3.org/1999/xhtml"
what's even cooler, netbeans now tracks if you closed every tag with slash "/" in document.

Categories