I have two endpoints each of them is returning the "index.html" view.
http://localhost:8080/
this URL shows the index file and CSS is working as well. image
http://localhost:8080/product/1
this URL only shows the index file but CSS is not loading. image
MainController.java
package com.example.temporary.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class MainController {
#RequestMapping("/")
public String index() {
return "index";
}
#RequestMapping("/product/1")
public String process() {
return "index";
}
}
index.html
<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" th:href="#{css/index.css}">
<title>Document</title>
</head>
<body>
<h1>Index Page</h1>
<a th:href="#{/product/1}">Click Me!</a>
</body>
</html>
index.css
* {
box-sizing: border-box;
}
body {
background-color: black;
color: white;
}
File Structure
image
try to include css as below in html
<link rel="stylesheet" th:href="#{/css/index.css}">
instead of css/index.css it should be /css/index.css
Related
All I can is viewing index.html, but when I am trying to surf to adminLogin for example all i see is:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Not Found, status=404).
here is PortfolioApplication:
#SpringBootApplication
public class PortfolioApplication {
public static void main(String[] args) {
SpringApplication.run(PortfolioApplication.class, args);
}
#GetMapping("/")
public String getMainPage() {
return "index.html";
}
#GetMapping("/adminLogin")
public String getAboutPage() {
return "adminLogin.html";
}
}
when i am trying to call another pages from index.html nothing happens:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hotel_Management_java_project</title>
<link rel="stylesheet" href="css/home.css">
</head>
<body>
<div class="home" id="home">
<nav>
<div class="logo">
<a href="#">
<h2>TourAgency</h2>
</a>
</div>
<div class="header_content">
<ul>
<li>Главная</li>
<div class="login">
<button class="loginbtn" id="loginbtn">Вход</button>
<div class="Login_content">
<a th:href="#{/adminLogin}">Вход для админа</a>
<a th:href="#{/employeelogin}">Вход для работника</a>
<a th:href="#{/userlogin}">Вход для пользователя</a>
</div>
</div>
</ul>
</div>
</nav>
</body>
</html>
UserController:
#RestController
#RequestMapping("/users")
public class UserController {
#Autowired
private UserRepository urepo;
#RequestMapping("/")
public String index() {
return "index";
}
#RequestMapping("/userlogin")
public String index1(Model model) {
model.addAttribute("user", new User());
return "userlogin";
}
I was trying change #Controller to #RestController, putting my main class to up and so on, but i've got no result
I am new to Spring MVC and Servlets. I am trying to run home() method in controller class SearchController but the output is only from home.jsp file. Statement:
out.println("<h1> this is my response block</h1>");
in not included in the result on browser.
Is there anything that can be done to print the out object statement in the browser with home.jsp file?
package springmvcsearch;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class SearchController {
#RequestMapping("/home")
public String home(HttpServletResponse res) throws IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<h1> this is my response block</h1>");
return "home";
}
}
This is home.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link href="<c:url value='/resources/css/style.css' />"
rel="stylesheet">
<script src="<c:url value='/resources/js/sample.css' /> "></script>
</head>
<body>
This is home view
</body>
</html>
This is the output on the browser:
out.println() is used in jsp file. So you can edit your home.jsp as follows
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link href="<c:url value='/resources/css/style.css' />"
rel="stylesheet">
<script src="<c:url value='/resources/js/sample.css' /> "></script>
</head>
<body>
This is home view
<%
out.println("<h1> this is my response block</h1>");
%>
</body>
</html>
If you want to add something for home.jsp via the controller class, you can change your controller class as follows.
package springmvcsearch;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class SearchController {
#RequestMapping("/home")
public String home(HttpServletRequest req,HttpServletResponse res) throws IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String message = "this is my response block";
req.setAttribute("message", message);
return "home";
}
}
Then change your jsp file as follows.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link href="<c:url value='/resources/css/style.css' />"
rel="stylesheet">
<script src="<c:url value='/resources/js/sample.css' /> "></script>
</head>
<body>
This is home view
<h1>${message}</h1>
</body>
</html>
For further explanation, go through this link how to message from servlet and display in jsp
I have been looking at all of the resources available on how to include an html file into another html file using Thymeleaf's th:insert. I am wondering if anyone could tell me what I am doing wrong as I feel like this html looks exactly like the examples I have seen.
My index.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.thymeleaf.org ">
<head>
<title>Default Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div th:insert="templates/Navbar :: navbar"> </div>
</body>
</html>
My Navbar.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.thymeleaf.org ">
<head>
<meta charset="UTF-8"/>
<title>NavigationBar</title>
<link rel="stylesheet" type="text/css" media="all" href="../../css/NavBar.css" th:href="#{/css/NavBar.css}" />
</head>
<body>
<div>
<div th:fragment="navbar" class="navbar">
<div class="navbar-inner">
<a th:href="#{/}" href="/home"> Home</a>
<a th:href="#{/}" href="/help">Help</a>
</div>
</div>
</div>
</body>
</html>
Also, here is my project's resource hierarchy via screen shot
If I put the code between the into the index.html and link the css file, the navbar shows up and works. So, I am not sure why the insert is not working. Here is my configuration file which has been edited based on examples below:
#Configuration
public class WebPageControllerConfig {
private SpringTemplateEngine templateEngine;
private ServletContextTemplateResolver templateResolver;
#Value("${WebController.startHour}")
public String startHour;
#Value("${WebController.endHour}")
public String endHour;
#Value("${WebController.numOfSkus}")
public int numOfSkus;
#Value("${WebController.skusToQuery}")
public File skusToQuery;
#Bean
public ClassLoaderTemplateResolver webPageTemplateResolver(){
ClassLoaderTemplateResolver webPageTemplateResolver = new ClassLoaderTemplateResolver();
webPageTemplateResolver.setPrefix("templates/");
webPageTemplateResolver.setSuffix(".html");
webPageTemplateResolver.setTemplateMode("HTML5");
webPageTemplateResolver.setCharacterEncoding(CharEncoding.UTF_8);
webPageTemplateResolver.setOrder(1);
return webPageTemplateResolver;
}
/* Old way of trying to configure
#Bean
public MessageSource messageSource() {...}
#Bean
public ServletContextTemplateResolver setTemplateResolver(){...}
#Bean
public SpringTemplateEngine setTemplateEngine() {...}
#Bean
public ViewResolver viewResolver() {...}
End of old configuration */
public String getStartHour() {return startHour;}
public String getendHour() {return endHour;}
public Object getnumOfSkus() {return numOfSkus;}
public File getSkusToQuery(){return skusToQuery;}
}
Change to
th:insert="templates/Navbar :: navbar"
and
th:fragment="navbar"
Configuration example:
import org.apache.commons.lang3.CharEncoding;
import org.springframework.context.annotation.*;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
#Configuration
public class ThymeleafConfiguration {
#Bean
#Description("Thymeleaf template resolver serving HTML 5 emails")
public ClassLoaderTemplateResolver emailTemplateResolver() {
ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
emailTemplateResolver.setPrefix("root folder where all thymeleaf files/");
emailTemplateResolver.setSuffix(".html");
emailTemplateResolver.setTemplateMode("HTML5");
emailTemplateResolver.setCharacterEncoding(CharEncoding.UTF_8);
emailTemplateResolver.setOrder(1);
return emailTemplateResolver;
}
}
Try with:
th:replace="/navbar::navbar"
or
th:insert="/navbar::navbar"
It works for me. No need to specify "template/navbar".
I want to add a title in template
#()
#(title: String)
<!DOCTYPE html>
<html lang="en">
<head>
<title>#title</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
</body>
</html>
but I see the error:
Compilation error
not found: value title
in routes
GET / controllers.HomeController.index
controller
public class HomeController extends Controller {
public Result index() {
return ok(views.html.index.render());
}
}
What is my mistake?
I'm using GWT 2.0, GWT-Ext 1.5 & Java 1.6 with Mozilla 3.6.x.
I'm trying to implement ThemeChanger module as per this link. But I'm not able to achieve it. Can anyone look at my code & tell me what am I missing or doing wrong?
Test.html
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="ThemeTest.css">
<link id="theme" rel="stylesheet" type="text/css" href="/public/resources/themes/green/css/xtheme-green.css"/>
<link id="theme" rel="stylesheet" type="text/css" href="/public/resources/themes/red/css/xtheme-red.css"/>
<link id="theme" rel="stylesheet" type="text/css" href="/public/resources/themes/gray/css/xtheme-gray.css"/>
<link id="theme" rel="stylesheet" type="text/css" href=""/>
<title>Web Application Project</title>
<script type="text/javascript" language="javascript" src="themetest/themetest.nocache.js"></script>
</head>
<body>
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
<noscript>
<div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
Your web browser must have JavaScript enabled
in order for this application to display correctly.
</div>
</noscript>
</body>
</html>
Test.gwt.xml
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='test'>
<inherits name='com.google.gwt.user.User'/>
<inherits name='com.gwtext.GwtExt' />
<inherits name='com.google.gwt.user.theme.standard.Standard'/>
<stylesheet src="resources/css/ext-all.css" />
<stylesheet src="resources/css/style.css" />
<script src="adapter/ext/ext-base.js" />
<script src="ext-all.js" />
<entry-point class='com.test.client.ThemeTest'/>
<source path='client'/>
<source path='shared'/>
</module>
EntryPointClass: ThemeTest.java
#Override
public void onModuleLoad() {
FormPanel formPanel = new FormPanel();
formPanel.setTitle("Form");
TextField nameField = new TextField("Name");
TextField descField = new TextField("Desc");
formPanel.add(nameField);
formPanel.add(descField);
formPanel.add(new ThemeChanger());
RootPanel.get().add(formPanel);
}
ThemeChanger.java
public class ThemeChanger extends ComboBox {
public ThemeChanger() {
final Store store = new SimpleStore(new String[]{"theme", "label"}, new Object[][]{
new Object[]{"/public/resources/themes/green/css/xtheme-green.css", "Green"},
new Object[]{"", "Aero Glass"},
new Object[]{"/public/resources/themes/red/css/xtheme-red.css", "Red"},
new Object[]{"/public/resources/themes/gray/css/xtheme-gray.css", "Gray"},
});
store.load();
setFieldLabel("Select Theme");
setEditable(false);
setStore(store);
setDisplayField("label");
setForceSelection(true);
setTriggerAction(ComboBox.ALL);
setValue("Gray");
setFieldLabel("Switch theme");
addListener(new ComboBoxListenerAdapter() {
public void onSelect(ComboBox comboBox, Record record, int index) {
try {
String theme = record.getAsString("theme");
CSS.swapStyleSheet("theme", theme);
} catch (Exception e) {
e.printStackTrace();
}
}
});
setWidth(100);
}
}
Check your locations of themes.
You can download gxt-2.2.1.zip.
Extract it on your computer. there will be a directory named resources containing themes
which you need to copy at your project location.
I have checked your code by palcing below example values
new Object[]{"/test/resources/themes/slate/css/xtheme-slate.css", "Slate"},
new Object[]{"/test/resources/themes/access/css/xtheme-access.css", "Access"},
In ThemeChanger classs with themes given in above .jar
It worked.