why SpringTemplateEngine in thymeleaf don`t process my template - spring

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>

Related

Spring i18n question mark instead of text

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.

Thymeleaf Spring 4 can't render Expression Language

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

Spring MVC dispatcher-servlet returns a 404 Error and not Welcome page

I have searched all the answers to 404 error pages about Spring MVC dispatcher, and I cannot find the answer to my issue.
I am also having an issue with Spring framework 4.1.6 as it no longer has the SimpleForm for forms for log in.
I understand that I need Maven to use the lastest Spring Security log in, which I plan to create a new Spring/Maven project and import this application into.
Just really want to know I have flow control going on before I export it to the Spring/Maven project. So any help with why my Welcome screen is not being found ....is much appreciated.
Heres my web.xml
enter code here<servlet>
<servlet-name>springMVCpizza</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springMVCpizza-servletConfig.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVCpizza</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Welcome.jsp</welcome-file>
</welcome-file-list>
Here is my dispatcher-servlet:
<mvc:annotation-driven />
<context:component-scan base-package="com.pizza.controllers" />
<bean id="urlMapping" class="org.springframework.web.servlet.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="Welcome.jsp">welcomePageController</prop>
</props>
</property>
</bean>
<bean id= "welcomePageController" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="LogIn" ref="LoginFormController">
</property>
</bean>
<bean id="loginFormController" class="com.pizza.controllers.loginFormController"></bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"/></bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor"/>
</list>
</property>
<property name="urlMap">
<map>
<entry key="/LogIn.jsp">
<ref bean="loginFormController"/>
</entry>
</map>
</property>
</bean>
<bean id="loginFormController" class="com.pizza.web.LoginFormController">
`enter code here`<property name="commandName"><value>login</value></property>
`enter code here` <property name="commandClass"><value>com.pizza.web.LogIn</value></property>
</bean>
<bean id="loginValidator" class="com.pizza.web.LoginValidator"/>
<bean id="loginFormController" class="com.pizza.controllers.LoginFormController">
<property name="sessionForm"><value>false</value></property>
<property name="commandName"><value>login</value></property>
<property name="commandClass"><value>com.pizza.web.LogIn</value> </property>
<property name="validator"><ref bean="loginValidator"/></property>
<property name="formView"><value>LogIn.jsp</value></property>
<property name="successView"><value>success</value></property>
<property name="urlMap">
<map>
<entry key="/LogIn.jsp">
<ref bean="loginFormController"/>
</entry>
</map>
</property>
</bean>
<bean id="LocaleChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="hl"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
<bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<!-- maximum file size in bytes -->
<property name ="maxUploadSize" value="100000"/>
</bean>
Heres the Welcome 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>Welcome to the Pizza Store</title>
</head>
<body bgcolor="#EEEEEE">
<center>
<h1>Welcome to the Pizza Store!</h1>
Sign In
<p>Or </p> Create Account
</center>
</body>
</html>

Show properties values in views in Spring MVC

I'm struggling to make something veeery simple work. Everything I really need to do is to write a property value in a view, i.e.:
<!DOCTYPE html>
<head>
...
<base href="${properties.config.baseurl}" />
...
</head>
<body>
...
</body>
</html>
My Spring MVC configuration (relevant bit):
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list><value>/WEB-INF/config.properties</value></list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="exposedContextBeanNames">
<list><value>properties</value></list>
</property>
</bean>
/WEB-INF/config.properties file:
config.baseurl = http://localhost:8080/
The view doesn't display anything at all (i.e. <base href="" />), can anyone explain to me why?
Thank you.
You need to use
${properties['config.baseurl']}
rather than
${properties.config.baseurl}
Otherwise it will try to navigate config.baseurl like a bean path, rather than as a string literal.

Access to freemarker variables in html file using FreeMarkerViewResolver

I configured my web application like this link http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch17s04.html
My context
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
<property name="freemarkerSettings">
<props>
<prop key="tag_syntax">square_bracket</prop>
<prop key="auto_import">spring.ftl as spring, echannels.ftl as echannels
</prop>
<prop key="template_update_delay">2147483647</prop>
</props>
</property>
</bean>
<bean id="htmlViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="contentType" value="text/html;charset=ISO-8859-1" />
<property name="cache" value="${viewResolver.html.cache}" />
<property name="prefix" value="html/" />
<property name="suffix" value=".html" />
</bean>
I use spring-webflow to link all my page html.
In my page html, i can access to conversion scope variables by using ${name_variable}.
My question is: how could i access to FreeMarker variable "foo" in my html if i defined it in my context:
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
<property name="freemarkerVariables">
<map>
<entry key="foo" value="foo" />
</map>
</property>
</bean>
Example of my HTML
<html>
<head>
<title>Welcome</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<h1>Welcome ${issuerId}</h1>
<form action="?execution=${flowExecutionKey}" method="post" name="defineFeeForm">
<input type="submit" name="_eventId_next" value="Next" /> <br/>
</form>
</body>
I can access to ${issuerId} because i have request.setConversationScope("issuerId", "1234") but i want to access also to foo freemarker variable but when i use ${foo}, i got Expression foo is undefined.
Any ideas?
Thanks to #ddekany , it's worked, there're nothing wrong in my configuration. I confused before. :). Yes, foo is visible simply as ${foo}.
Another remark, if i want to add new variables during running of my application, i can use
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("foo2", "foo2");
freeMarkerConfigurer.getConfiguration().setAllSharedVariables(new SimpleHash(variables, freeMarkerConfigurer.getConfiguration().getObjectWrapper()));
and i can access with ${foo2}.
Thanks again. It's done now.

Resources