I integrate Spring 4 MVC and Thymeleaf. And i successfully get my active template based on my Controller.
But when i add request attribute to my View, my request attribute can't render the value.
I think it's automatically.
Here is my XML configuration file :
<context:component-scan base-package="com.fanjavaid" />
<mvc:annotation-driven />
<mvc:resources location="/resources/" mapping="/resources/**" />
<!-- THYMELEAF CONFIGURATION -->
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".html"></property>
<property name="templateMode" value="HTML5"></property>
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"></property>
</bean>
Here is My Controller :
#Controller
public class JobseekerController {
#RequestMapping("/")
public String index(Model model) {
model.addAttribute("message", "Welcome to Spring 4 MVC");
return "index";
}
}
And here is my View :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Welcome Page</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
My View is HTML file.
fanjavaid,
I just think you need to add some code.
HTML
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TEST</title>
</head>
<body>
<h1 th:text="${message}">hi</h1>
</body>
</html>
You search thymeleaf tutorial
Related
I hava i18n, but experience problems with chinese and hindi letters. I have question marks ????? instead of text.
Here is my dispatcher-servlet.xml:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.aartek.interceptor.LoginInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.aartek.interceptor.SecondInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/product.do*" />
<bean class="com.aartek.interceptor.LoggingInterceptor" />
</mvc:interceptor>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
Login.jsp
<%#page contentType="text/html;charset=utf-8" pageEncoding="UTF-8"
language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%#taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Login</title>
<span style="float: right">
English |French|<a href="?
lang=hi_IN">Hindi</a>|Chinese
</span>
<form:form method="POST" modelAttribute="keyUser"
action="checkLogin.do">
<table width="100%" border="0">
<tr>
<td><spring:message code="label.userId"
/></td>
<td><form:input path="userId" id="userId" /></td>
</tr>
In similar way I wrote for email id,password and submit.
3.messages_zh_CN.properties
label.userId=\u7528\u6237\u540D
label.emailId=\u7535\u5B50\u90AE\u4EF6ID
label.password=\u5BC6\u7801
label.submit=\u63D0\u4EA4
In similar way I wrote for English, Hindi and French .
My question:
How I can remove the question marks and its for Chinese and Hindi only, how can I display correct text?
File encode of your properites file, java class, jsp should be UTF-8.
i want send email with spring framework and thymeleaf .but when i run to the process, it doesn`t continue to run.
my Configuration:
<bean id="emailResolver"
class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
<property name="prefix" value="/template/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5"></property>
<property name="order" value="1"></property>
<property name="characterEncoding" value="UTF-8"></property>
<property name="cacheable" value="false"></property>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolvers">
<set>
<ref bean="emailResolver" />
</set>
</property>
</bean>
java snippets:
#Autowired
SpringTemplateEngine templateEngine;
public void createThyTest()
{
Context con = new Context();
con.setVariable("name", "volcanno");
System.out.println(this.getClass().getClassLoader().getResource(""));
String string2 = templateEngine.process("Test.html", con);
System.out.println("-----");
System.out.println(string2);
}
when i run the code,there is no any "---" to be output.
output:
file:/D:/Neon_eclipse_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ProjectDemo/WEB-INF/classes/
No other error message is output.
and i use :
thymeleaf-spring3-3.0.9.RELEASE.jar
thymeleaf-3.0.9.RELEASE.jar
spring 3.1.2
thanks
my template:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="#{greeting(${name})}">
Hello, Peter Static!
</p>
</body>
</html>
I am now in Spring mvc trying to learn apche tiles...but its not working....don't know why?
****servlet-context.xml** file**
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.
InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.mymvc.myapp" />
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/defs/general.xml</value>
</list>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles3.TilesView" />
< /bean>
</beans>
Tiles dependicies in pom.xml
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>${apache.tiles}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>${apache.tiles}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.6</version>
</dependency>
general.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="common" template="/WEB-INF/layout/classic.jsp">
<put-attribute name="footer" value="/WEB-INF/layout/footer.jsp" />
</definition>
<definition name="index" extends="common">
<put-attribute name="title" value="Java Blog Aggregator" />
<put-attribute name="body" value="/WEB-INF/views/index.jsp" />
</definition>
</tiles-definitions>
classic.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:getAsString name="title"/></title>
</head>
<body>
<tiles:insertAttribute name="body" />
<br>
<br>
<center>
<tiles:insertAttribute name="footer" />
</center>
</body>
</html>
footer.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
©IShmam Shahriar
IndexController.java
package com.mymvc.myapp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class IndexController {
#RequestMapping("/index")
public String index(){
return "index";
}
}
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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">
<title>Insert title here</title>
</head>
<body>
<p>index page</p>
</body>
</html>
Your HTML is wrong! It has a closing <body> and <html> tag in you "main-content (index.jsp)"!
You have your template: classic.jsp and you include index.jsp in the <tiles:insertAttribute name="body" /> placeholder. So the resulting HTML would looks like, (the complete index.jsp will be inserted at the placeholder!):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>...</title>
</head>
<body>
<html> <!-- problem starts her -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO8859-1">
<title>Insert title here</title>
</head>
<body>
<p>index page</p>
</body> <!-- that is the reason for not -->
</html> <!-- having a footer -->
<br>
<br>
<center>
©IShmam Shahriar
</center>
</body>
</html>
Did you noticed that you "close" the html content with </body> and </html> already before the footer! -- The solution is simple: remove all the header, html and body tags from index.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<p>index page</p>
An other point:
Remove your InternalResourceViewResolver from the configuration. The problem is that you have two view resolvers (Internal and Url/Tiles) and it is likely that your viewname (return "index") is resolved by the Internal view resolver, but not by the Url/Tiles resolver.
According to Spring Reference, Chapter 18.3.2 Integration Tiles, you just need:
org.springframework.web.servlet.view.tiles3.TilesConfigurer
org.springframework.web.servlet.view.UrlBasedViewResolver with TilesView configured for viewClass property (or use org.springframework.web.servlet.view.tiles3.TilesViewResolver - that is an anready configured subclass of UrlBasedViewResolver)
and you can have a org.springframework.web.servlet.view.ResourceBundleViewResolver
example:
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/defs/general.xml</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
</bean>
I cannot load the css files. Try almost all way to reach it. Could someone advice where should be the problem. Thanks in advance.
tiles-definitions where is defined tiles:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="defaultTemplate" template="/WEB-INF/template/default/template.jsp">
<put-attribute name="header" value="/WEB-INF/template/default/header.jsp" />
<put-attribute name="menu" value="/WEB-INF/template/default/menu.jsp" />
<put-attribute name="body" value="/WEB-INF/template/default/body.jsp" />
<put-attribute name="footer" value="/WEB-INF/template/default/footer.jsp" />
<put-attribute name="stylesheets" value="/src/main/resources/css/layout.css" />
</definition>
</tiles-definitions>
template.jsp is defalut template:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Default tiles template</title>
<c:forEach var="css" items="${stylesheets}">
<link rel="stylesheet" type="text/css" href="<c:url value="${css}"/>">
</c:forEach>
</head>
<body>
<div class="page">
<tiles:insertAttribute name="header" />
<div class="content">
<tiles:insertAttribute name="menu" />
<tiles:insertAttribute name="body" />
</div>
<tiles:insertAttribute name="footer" />
</div>
</body>
</html>
welcomepage.jsp using template.jsp:
<%# taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<tiles:importAttribute name="stylesheets"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Default tiles template</title>
<c:forEach var="css" items="${stylesheets}">
<link rel="stylesheet" type="text/css" href="<c:url value="${css}"/>">
</c:forEach>
</head>
<body>
<div class="page">
<tiles:insertAttribute name="header" />
<div class="content">
<tiles:insertAttribute name="menu" />
<tiles:insertAttribute name="body" />
</div>
<tiles:insertAttribute name="footer" />
</div>
</body>
</html>
dispatcher-sevrlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:annotation-driven/>
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="intranetwebapp.*" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/intranetwebapp" />
<property name="username" value="root" />
<property name="password" value="gargamel" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop
key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="intranetwebapp.entity" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>
<!-- Tiles configuration -->
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles/tiles-definitions.xml</value>
</list>
</property>
</bean>
</beans>
can you try something like below,
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Default tiles template</title>
<link rel="stylesheet" href="<c:url value='/resources/css/layout.css'/>">
</head>
or you can set stylesheet variable to /resources/css/layout.css in spring model object and set the value in c:url to value='${stylesheet}' in jsp.
I am knew with Spring web flow integration and i am trying to configure my web-flow but in some reason when I wont to start my project Tomcat show me 404 message like he can not found my main-flow.xml which have state-view to welcome.xhtml.
I am redirecting from URL in
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:c="http://java.sun.com/jsf/core"
xmlns:ui = "http://java.sun.com/jsf/facelets"
xmlns:h = "http://java.sun.com/jsf/html">
<head>
<title>Insert title here</title>
</head>
<h:body>
<h:outputLabel value="Hello World"></h:outputLabel>
</h:body>
</html>
index.jsp.
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<view-state id="welcome">
</view-state>
</flow>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:faces="http://www.springframework.org/schema/faces"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config.xsd
http://www.springframework.org/schema/faces
http://www.springframework.org/schema/faces/spring-faces.xsd">
<bean id="jpaFlowExecutionListener" class="org.springframework.webflow.persistence.JpaFlowExecutionListener">
<constructor-arg ref="entityManagerFactory" />
<constructor-arg ref="transactionManager" />
</bean>
<bean id="facesContextListener" class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener" />
<webflow:flow-executor id="flowExecutor">
<webflow:flow-execution-listeners>
<webflow:listener ref="jpaFlowExecutionListener" />
<webflow:listener ref="facesContextListener"/>
</webflow:flow-execution-listeners>
</webflow:flow-executor>
<webflow:flow-registry id="flowRegistry" flow-builder-services="facesFlowBuilderServices" base-path="/WEB-INF/flows">
<webflow:flow-location-pattern value="/main/*-flow.xml" />
</webflow:flow-registry>
<faces:flow-builder-services id="facesFlowBuilderServices" development="true" />
<faces:resources />
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="order" value="1"/>
<property name="flowRegistry" ref="flowRegistry" />
<property name="defaultHandler">
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
</property>
</bean>
<bean class="org.springframework.faces.webflow.JsfFlowHandlerAdapter">
<property name="flowExecutor" ref="flowExecutor" />
</bean>
<bean id="faceletsViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.faces.mvc.JsfView"/>
<property name="prefix" value="/WEB-INF/flows" />
<property name="suffix" value=".xhtml" />
</bean>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
</beans>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="refresh" content="0; URL=/app/main" />
</head>
<body></body>
</html
What URL are you hitting? web-flow file names are ignored in the URL pattern.
Lets say a flow is defined in "/WEB-INF/flows/main/main-flow.xml" and your "base-path" is set to "WEB-INF/flows". Then to navigate to your flow you goto
http://<your-host>/<your-webapp-name-if-defined>/main
The flow file name is completely ignored and not used in the URL at all.
Also you should specify a view name for your view to render something
<view-state id="welcome" view="myView">
</view-state>