load the css from tiles definition xml - java

How can i load the css from tiles definition file in springs?
My JSP:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- <tiles:importAttribute name="cssList" /> --%>
<tiles:useAttribute id = "stylesList" name="styles" classname="java.util.List"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%-- <link href="<c:url value="/styles/cssreset-min.css"/>" rel="stylesheet" type="text/css" /> --%>
<%-- <link type="text/css" rel="stylesheet"
href="<tiles:getAsString name="cssList"/>"/> --%>
<%-- <link href="<c:url value="cssList"/>" rel="stylesheet" type="text/css" /> --%>
<c:forEach var="eachStyle" items="${styles}">
<link type="text/css" rel="stylesheet" href="<c:out value='${eachStyle}'/>" />
</c:forEach>
<title>Insert title here</title>
</head>
This is my tiles xml file?
<definition name="template-main" template="/WEB-INF/jsp/layouts/main.jsp">
<!-- <put-attribute name="cssList" value="/resources/styles/cssreset-min.css" type="string"/> -->
<put-attribute name="banner-content" value="/WEB-INF/jsp/sections/banner.jsp" />
<put-attribute name="title-content" value="Pet Type" />
<put-attribute name="primary-content" value="" />
<put-attribute name="footer-content" value="/WEB-INF/jsp/sections/footer.jsp" />
<put-list-attribute name="styles">
<add-list-attribute>
<add-attribute value="/resources/styles/cssreset-min.css"></add-attribute>
</add-list-attribute>
</put-list-attribute>
</definition>
I just want to load the css from tiles definition xml file. How can i do this?
Can anyone please help . I am new to this one

You have a mistake on the var id to retrieve the items list (at the c:forEach tag).
You are referencing the name (used by tiles:useAttribute) to retrieve the Tiles value, and not the id (which is the key to find the list retrieved at the JSP, in short, the defined variable name).
Your code should look like this:
<c:forEach var="eachStyle" items="stylesList">
<link type="text/css" rel="stylesheet" href="<c:out value='${eachStyle}'/>" />
</c:forEach>

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

Why bootstrap does not work in eclipse?

Why bootstrap does not work ? I download the folders from site and I add it in the root of the WebContent like WebContent/bootstrap/css..js..fonts
The jsp looks like this
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet"
href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link href='http://fonts.googleapis.com/css?family=Roboto'
rel='stylesheet' type='text/css'>
<title>Comment Box</title>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet"
type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div class="row" id="featuresheading">
<div class="col-lg-12">
<div class="table-responsive">
<table class="table ta................etc
but why the table does not appear as it suposed? please help
Try a full publish to get your bootstrap files into your application server.
I got bootstrap to work in a .jsp in Eclipse. Here's my configuration:
Here's the markup in the .jsp...
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Bootstrap</title>
<link href="bootstrap-3.3.6/dist/css/bootstrap.css" rel="stylesheet"
type="text/css" />
</head>
<body>
<div class="container">
<h1>Test Bootstrap</h1>
<table class="table table-striped">
<tr>
<td>Bootstrap</td>
</tr>
<tr>
<td>is working</td>
</tr>
</table>
</div>
</body>
</html>`
`You don't need to reference bootstrap two times. Just pick the full or the minified version. Good luck.

Passing JSP variable to Java function?

I am working with a JSP file, trying to work on a simple logon page.
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# page import="com.bridge.service.*" %>
<!-- needs to be run on server to resolve both errors -->
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.18.custom.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.18.custom.min.js"></script>
<title>Login </title>
</head>
<body>
<form method="POST" name="frmLogon">
User Email: <input type="text" name="email" value="<c:out value="${email}" />" /> <br />
Password: <input type="text" name="password" value="<c:out value="${password}" />" /> <br />
<jsp:useBean id="link" scope="application" class="com.bridge.service.Service" />
<%
Service s = new Service();
String token = s.logonToken(email, password);
%>
<input type="submit" value="Submit" />
</form>
</body>
</html>
I am having a hard time understanding how to take the two values from the POST form and passing them to the Java function. Is this possible? Am I doing this incorrectly?
For simple servlet, you shouod use:
String email = (String)request.getParameter("email");
String password = (String)request.getParameter("password");

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.

Regarding Servlet and JSP

I was practicing Servlet and JSP and got stuck with an scenario
this is my html page code.
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home</title>
</head>
<body>
<form action="Second.jsp" method="post">
<p>Name:<input type="text" name="name"/>
<p>Employee Id:<input type="text" name="empId" />
<p><input type="submit" value="Enter" />
</form>
</body>
</html>
JSP Code
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="person" class="com.Person" scope="request">
<jsp:setProperty name="person" property="name" />
</jsp:useBean>
<jsp:getProperty property="name" name="person"/>
</body>
</html>
Person.java
package com;
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
My request goes directly to jsp and the form element name and bean property name matches so no need tp set value in jsp:setProperty
However getProperty is showing null.
You missed the param attribute.
<jsp:useBean id="person" class="com.Person" scope="request">
<jsp:setProperty name="person" property="name" param="name" />
</jsp:useBean>
Within the second page simply use:
${param.name} and ${param.empId}

Categories