Hibernate Form Validation Display Error - spring-mvc

I have prepared a sample spring mvc application to understand Hibernate Validator
API.
The view is not getting displayed, when I run it, even though I have all the
required files ready.
The following are my files :
List item
_
#EnableWebMvc
#Configuration
#ComponentScan({"com.*"})
public class SpringConfig extends WebMvcConfigurerAdapter {
public void addHandler(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/resources/**/").addResourceLocations("/resources/"
);
}
#Bean
public InternalResourceViewResolver viewResolver()
{
InternalResourceViewResolver vr = new InternalResourceViewResolver();
vr.setPrefix("/WEB-INF/view/");
vr.setSuffix(".jsp");
return vr;
}
}
Controller
#RequestMapping("/form")
public class FormController {
#RequestMapping(method=RequestMethod.GET)
public String initForm(Model model) {
Form form = new Form();
model.addAttribute("form", form);
return "form";
}
#RequestMapping(method = RequestMethod.POST)
public String submitForm(#Valid Form form, BindingResult result) {
String returnVal = "successForm";
if(result.hasErrors()) {
returnVal = "form";
}
return returnVal;
}
}
Model
public class Form {
#Size(min=5, max=10, message="Should be of 5-10 characters")
private String name;
#Min(value = 5, message="Should be atleast of 5 characters")
private String lastname;
#NotNull(message="Cannot be empty")
#Size(min=5, max=10, message="Should be of 5-10 characters")
private String password;
#Pattern(regexp="[0-9]+",message="Wrong Zip")
private String zip;
#Min(value=18,message="Should not be below 18")
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
JSP
"http://www.w3.org/TR/html4/loose.dtd">
Insert title here
Fill your form!
<form:form method="POST" commandName="form">
<table>
<tr>
<td>Enter your name:</td>
<td><form:input path="name" /></td>
<td><form:errors path="name" cssStyle="color: #ff0000;"/></td>
</tr>
<tr>
<td>Enter your lastname:</td>
<td><form:input path="lastname" /></td>
<td><form:errors path="lastname" cssStyle="color: #ff0000;" />
</tr>
<tr>
<td>Enter your password:</td>
<td><form:input path="password" /></td>
<td><form:errors path="password" cssStyle="color: #ff0000;" />
</tr>
<tr>
<td>Enter your zip:</td>
<td><form:input path="zip" /></td>
<td><form:errors path="zip" cssStyle="color: #ff0000;" /></td>
</tr>
<tr>
<td>Enter your email:</td>
<td><form:input path="email" /></td>
<td><form:errors path="email" cssStyle="color: #ff0000;" /></td>
</tr>
<tr>
<td>Enter your age:</td>
<td><form:input path="age" /></td>
<td><form:errors path="age" cssStyle="color: #ff0000;" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
</table>
</form:form>
pom.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.samples.service.service
HibernateValidation
0.0.1-SNAPSHOT
war
<properties>
<!-- Generic properties -->
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-
8
<!-- Web -->
<jsp.version>2.2</jsp.version>
<jstl.version>1.2</jstl.version>
<servlet.version>2.5</servlet.version>
<!-- Spring -->
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>
<!-- Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<dependency>
javax.el
javax.el-api
2.2.4
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
org.glassfish.web
javax.el
2.2.4
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.10.Final</version>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.3.0.ga</version>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
<groupId>asm</groupId>
<artifactId>asm-util</artifactId>
<version>3.3.1</version>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.3</version>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Other Web dependencies -->
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

Related

How to send form to controller on thymeleaf

I have this codeand it doen't work(i think it's th:action="#{/employee}",because it wants from me, that i put html href)
Sorry for my eng))
If you knows,help me,pleace))))
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import javax.persistence.*;
import javax.validation.constraints.Size;
import java.util.Collection;
import java.util.Set;
#Entity
#Table(name = "t_user")
public class User implements UserDetails {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Size(min=2, message = "Не меньше 5 знаков")
private String username;
#Size(min=2, message = "Не меньше 5 знаков")
private String password;
#Transient
private String passwordConfirm;
#ManyToMany(fetch = FetchType.EAGER)
private Set<Role> roles;
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Override
public String getUsername() {
return username;
}
#Override
public boolean isAccountNonExpired() {
return true;
}
#Override
public boolean isAccountNonLocked() {
return true;
}
#Override
public boolean isCredentialsNonExpired() {
return true;
}
#Override
public boolean isEnabled() {
return true;
}
public void setUsername(String username) {
this.username = username;
}
#Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return getRoles();
}
#Override
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPasswordConfirm() {
return passwordConfirm;
}
public void setPasswordConfirm(String passwordConfirm) {
this.passwordConfirm = passwordConfirm;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
import com.gainground.gainGroung.service.UserService;
import com.gainground.gainGroung.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import javax.validation.Valid;
#Controller
public class RegistrationController {
#Autowired
private UserService userService;
#GetMapping("/registration/employee")
public String registrationEmpl(Model model) {
model.addAttribute("userForm", new User());
return "employee-registration";
}
#PostMapping("/registration/employee")
public String addEmpl(#ModelAttribute("userForm") #Valid User userForm, BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) {
return "employee-registration";
}
if (!userForm.getPassword().equals(userForm.getPasswordConfirm())){
model.addAttribute("passwordError", "Пароли не совпадают");
return "employee-registration";
}
if (!userService.saveEmpl(userForm)){
model.addAttribute("usernameError", "Пользователь с таким именем уже существует");
return "employee-registration";
}
return "redirect:/login";
}
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Page Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<h1>Регистрация</h1>
<p>Пожалуйста, заполните эту форму, чтобы создать учетную запись.</p>
<hr>
<form action="#" th:action="#{/registration/employee}" th:object="${userForm}" method="post">
<h2 class="form-signin-heading">Login</h2>
<p>
<label for="username">Username</label>
<input type="text" id="username" th:field="*{username}" class="form-control" placeholder="Username" >
</p>
<p>
<label for="password">Password</label>
<input type="password" id="password" th:field="*{password}" class="form-control" placeholder="Password">
</p>
<button class="btn btn-lg btn-primary btn-block" type="submit">Зарегестрироваться</button>
</form>
</div>
</body>
</html>
this pom
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.2.RELEASE
....
<java.version>1.8</java.version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.27</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.4.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>10</source>
<target>10</target>
</configuration>
</plugin>
</plugins>
</build>
enter image description here
I am stupid) I forgot add input to passwordConfirm

Configuration with #Controller, InternalResourceViewResolver and ModelAndView in Spring MVC doesn't work

I'm new to Spring MVC. I know there are a lot of similar questions like this but I already tried most of them for several hours and still facing the issue that 404 error shows up when I request http://localhost:8080/SpringRESTFulExample/ from browser. But there's no any exception in console.
Here is my code,
RootConfig.java
#Configuration
#ComponentScan(basePackages = "springrestful_example")
public class RootConfig {
}
WebConfig.java
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = "springrestful_example")
public class WebConfig extends WebMvcConfigurerAdapter{
#Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
.
.
.
}
WebInitializer.java
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {RootConfig.class};
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {WebConfig.class};
}
#Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
UserController.java
#RestController
public class UserController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index(){
ModelAndView modelandView = new ModelAndView("index");
return modelandView;
}
.
.
}
index.jsp is under /WEB-INF/views/index.jsp
Dependencies I use
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
That's all I got. Correct me if my code is being wrong. I will be happy if i see Hello World text written in index.jsp body.
What package are your Config classes in? Are they in basePackages you specify? Try not specifying the basePackages in the Component scan annotation.

view not getting resolved spring boot web

I am new to spring boot web mvc. My view on RequestMapping("/") is not getting resolved. Below i have shared required code.
my controller class is
#Controller
public class ExpenseSheetController {
#Autowired
private ExpenseSheetDao expenseSheetDao;
#RequestMapping("/")
public ModelAndView home() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("home");
modelAndView.addObject("message","on home page");
return modelAndView;
}
}
pom.xml is as follows
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- my-sql connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
I have kept my view at src/main/resources/templates as home.html

SAXException on root element unmarshalling in spring web service

I have restful webservice with spring which has following mapping:
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN"
"http://Castor.exolab.org/mapping.dtd">
<mapping>
<class name="com.example.web.Contacts">
<field name="contacts" type="com.example.web.model.Contact"
collection="arraylist">
<bind-xml name="contact" />
</field>
</class>
<class name="com.example.web.model.Contact" identity="id">
<map-to xml="contact" />
<field name="id" type="long">
<bind-xml name="id" node="element" />
</field>
<field name="firstName" type="string">
<bind-xml name="firstName" node="element" />
</field>
<field name="lastName" type="string">
<bind-xml name="lastName" node="element" />
</field>
<field name="birthDate" type="string" handler="dateHandler">
<bind-xml name="birthDate" node="element" />
</field>
<field name="version" type="integer">
<bind-xml name="version" node="element" />
</field>
</class>
<field-handler name="dateHandler"
class="com.example.web.DateTimeFieldHandler">
<param name="date-format" value="yyyy-MM-dd" />
</field-handler>
</mapping>
Contacts class:
public class Contacts implements Serializable {
private List<Contact> contacts;
public Contacts() {
}
public Contacts(List<Contact> contacts) {
this.contacts = contacts;
}
public List<Contact> getContacts() {
return contacts;
}
public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}
}
Contact class:
#Entity
#Table(name = "contact")
public class Contact implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private int version;
private String firstName;
private String lastName;
private DateTime birthDate;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID")
public Long getId() {
return id;
}
#Version
#Column(name = "VERSION")
public int getVersion() {
return version;
}
#Column(name = "FIRST_NAME")
public String getFirstName() {
return firstName;
}
#Column(name = "LAST_NAME")
public String getLastName() {
return lastName;
}
#Column(name = "BIRTH_DATE")
#Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
public DateTime getBirthDate() {
return birthDate;
}
public void setId(Long id) {
this.id = id;
}
public void setVersion(int version) {
this.version = version;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setBirthDate(DateTime birthDate) {
this.birthDate = birthDate;
}
#Override
public String toString() {
return "Contact - Id: " + id + ", First name: " + firstName + ", Last name: " + lastName + ", Birthday: "
+ birthDate;
}
}
When I process GET with xml format from curl, I am receiving following output:
<?xml version="1.0" encoding="UTF-8"?>
<Contacts>
<contacts>
<contacts>
<id>1</id>
<version>0</version>
<firstName>Chris</firstName>
<lastName>Schaefer</lastName>
<birthDate>
<dayOfMonth>3</dayOfMonth>
<dayOfWeek>7</dayOfWeek>
<era>1</era>
<year>1981</year>
<dayOfYear>123</dayOfYear>
<millisOfDay>3600000</millisOfDay>
<monthOfYear>5</monthOfYear>
<hourOfDay>1</hourOfDay>
<minuteOfHour>0</minuteOfHour>
<weekyear>1981</weekyear>
<yearOfEra>1981</yearOfEra>
<yearOfCentury>81</yearOfCentury>
<centuryOfEra>19</centuryOfEra>
<secondOfDay>3600</secondOfDay>
<minuteOfDay>60</minuteOfDay>
<secondOfMinute>0</secondOfMinute>
<millisOfSecond>0</millisOfSecond>
<weekOfWeekyear>18</weekOfWeekyear>
<millis>357696000000</millis>
<zone>
<uncachedZone>
<fixed>false</fixed>
<cachable>true</cachable>
<id>Europe/Belgrade</id>
</uncachedZone>
<fixed>false</fixed>
<id>Europe/Belgrade</id>
</zone>
<chronology>
<zone>
<uncachedZone>
<fixed>false</fixed>
<cachable>true</cachable>
<id>Europe/Belgrade</id>
</uncachedZone>
<fixed>false</fixed>
<id>Europe/Belgrade</id>
</zone>
</chronology>
<afterNow>false</afterNow>
<beforeNow>true</beforeNow>
<equalNow>false</equalNow>
</birthDate>
</contacts>
...
</contacts>
</Contacts>
Maven dependencies (same for web and client app):
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.9.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.190</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId> org.jadira.usertype </groupId>
<artifactId>usertype.core</artifactId>
<version> 3.0.0.CR3 </version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.castor</groupId>
<artifactId>castor-xml</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.castor</groupId>
<artifactId>castor-core</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.190</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
Then when I am trying to get java objects (in client application) via org.springframework.web.client.RestTemplate I have following exception:
Exception in thread "main"
org.springframework.http.converter.HttpMessageNotReadableException:
Could not read [class com.example.web.Contacts]; nested exception is
org.springframework.oxm.UnmarshallingFailureException: SAX reader
exception; nested exception is org.xml.sax.SAXException: The class for
the root element 'Contacts' could not be found. at
org.springframework.http.converter.xml.MarshallingHttpMessageConverter.re
adFromSource(MarshallingHttpMessageConverter.java:134)
...
I belive that something wrong is with my mapping file. As You can see I have triple contacts tag even in mapping file I have other names.. Do anyone know what can be a reason of having this exception/wrong xml format?
What is happening is that when unmarshaller is looking for class he's looking for alias "Contacts" as in XML. What he has in cache is "contacts" mapping and its not the same as "Contacts". Problem was that marshaller library was not the same as unmarshaller. Probably something wrong in configuration..
What I did is change of implementation to jaxb I put #XMLRootElement and #XMLElement on model classes and completely removed configuration of messageConverters in mvc:annotation-driven (also mapping xml files). On client application I changed messageConverter to:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxb2Marshaller" />
<property name="unmarshaller" ref="jaxb2Marshaller" />
</bean>
</list>
</property>
</bean>
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.example.web.model.Contact</value>
<value>com.example.web.Contacts</value>
</list>
</property>
</bean>
And everything works fine.

Spring mvc integration with Apache Tiles 3 Using Java Configuration has Problems

The problem is the that tiles configuration is not getting applied
everytime.
I have integrated Apache tiles 3 with spring MVC. I have used annotation based configuration of spring. The problem in the application is that the tiles definitions get applied on random basis. When I try to run the application, the tiles configuration that we have configured may or may not get applied.
I am using Apache tomcat 7. Is this problem related with server? Is this problem related with Configuration? Or If Any.
Here is my Code
MVC Configuration
Annotation base Java Configuration using Spring
#EnableWebMvc
#Configuration
#ComponentScan(basePackages = { "com.om.*" })
public class MVCConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/layout/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
#Bean
public TilesViewResolver getTilesViewResolver() {
TilesViewResolver tilesViewResolver = new TilesViewResolver();
tilesViewResolver.setViewClass(TilesView.class);
return tilesViewResolver;
}
#Bean
public TilesConfigurer getTilesConfigurer() {
TilesConfigurer tilesConfigurer = new TilesConfigurer();
tilesConfigurer.setCheckRefresh(true);
tilesConfigurer.setDefinitionsFactoryClass(TilesDefinitionsConfig.class);
// Add apache tiles definitions
TilesDefinitionsConfig.addDefinitions();
return tilesConfigurer;
}
}
MVCInitializer
public class MVCInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { MVCConfig.class };
}
#Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
#Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
TilesDefinitionsConfig
public final class TilesDefinitionsConfig implements DefinitionsFactory {
private static final Map<String, Definition> tilesDefinitions = new HashMap<String,Definition>();
private static final Attribute BASE_TEMPLATE = new Attribute("/WEB-INF/views/layout/defaultLayout.jsp");
public Definition getDefinition(String name, Request tilesContext) {
System.out.println("3");
return tilesDefinitions.get(name);
}
/**
* #param name <code>Name of the view</code>
* #param title <code>Page title</code>
* #param body <code>Body JSP file path</code>
*
* <code>Adds default layout definitions</code>
*/
private static void addDefaultLayoutDef(String name, String title, String body) {
Map<String, Attribute> attributes = new HashMap<String,Attribute>();
attributes.put("title", new Attribute(title));
attributes.put("header", new Attribute("/WEB-INF/views/layout/header.jsp"));
attributes.put("menu", new Attribute("/WEB-INF/views/layout/menu.jsp"));
attributes.put("body", new Attribute(body));
attributes.put("footer", new Attribute("/WEB-INF/views/layout/footer.jsp"));
tilesDefinitions.put(name, new Definition(name, BASE_TEMPLATE, attributes));
}
/**
* <code>Add Apache tiles definitions</code>
*/
public static void addDefinitions(){
addDefaultLayoutDef("welcome", "welcome", "/WEB-INF/views/layout/welcome.jsp");
addDefaultLayoutDef("personList", "viewPerson", "/WEB-INF/views/layout/personList.jsp");
}
}
Controller
#Controller
public class SpringTilesController {
#RequestMapping(value="welcome")
public ModelAndView index() {
ModelAndView model=new ModelAndView();
System.out.println("In Controller");
model.setViewName("welcome");
return model;
}
#RequestMapping(value="viewPerson")
public ModelAndView viewPersons(Model model) {
Map<String, List<Person>> persons =
new HashMap<String, List<Person>>();
persons.put("persons", Person.createPersons());
return new ModelAndView("personList", persons);
}
}
Entity
public class Person {
private String name, email;
private int age;
public Person(String name, String email, int age) {
this.name = name;
this.email = email;
this.age = age;
}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public String getEmail() {return email;}
public void setEmail(String email) {this.email = email;}
public int getAge() {return age;}
public void setAge(int age) {this.age = age;}
#Override
public String toString()
{
return String.format(
"Person [name = %s, email = %s, age = %d]",
name, email, age);
}
public static List<Person> createPersons() {
List<Person> persons = new ArrayList<Person>();
persons.add(new Person("Tousif", "tousif#mail.com", 32));
persons.add(new Person("Asif", "asif#mail.com", 28));
persons.add(new Person("Ramiz", "ramiz#mail.com", 26));
persons.add(new Person("Rizwan", "rizwan#mail.com", 32));
persons.add(new Person("Amol", "amol#mail.com", 33));
persons.add(new Person("Ramdas", "ramdas#mail.com", 31));
return persons;
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.om</groupId>
<artifactId>TilesDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TilesDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.7</jdk.version>
<spring.version>4.1.6.RELEASE</spring.version>
<spring.security.version>4.0.1.RELEASE</spring.security.version>
<jstl.version>1.2</jstl.version>
<javax.servlet.version>3.1.0</javax.servlet.version>
<mysql.connector.version>5.1.31</mysql.connector.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- jstl for jsp page -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- Apache Tiles -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-api</artifactId>
<version>3.0.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>3.0.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>3.0.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>3.0.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-template</artifactId>
<version>3.0.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-el</artifactId>
<version>3.0.5</version>
<scope>compile</scope>
</dependency>
<!-- Spring 4 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>TilesDemo</finalName>
</build>
</project>
Keep The header,footer,menu as per the requirements.
This is defaultLayout.jsp page
<%# taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title><tiles:insertAttribute name="title" ignore="true"></tiles:insertAttribute></title>
</head>
<body style="background-color: #FFF">
<div class="page">
<tiles:insertAttribute name="header" />
<div class="content">
<div id="body">
<tiles:insertAttribute name="body" />
</div>
</div>
<tiles:insertAttribute name="footer" />
</div>
</body>
</html>
footer.jsp
<hr />
<div class="span-1 prepend-3"> </div>
<div class="span-16 last">
<p>
<b> Technology</b>
( All rights Reserved)
</p>
</div>
header.jsp
<div class="span-24">
<img src="resources/images/images.png"
width="950" style="padding-top:10px;" />
</div>
menu.jsp
Left side menu bar options
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<ul style="list-style:none;line-height:28px;">
<li><spring:url value="/index" var="homeUrl" htmlEscape="true" />
Home
</li>
<li><spring:url value="/viewPerson" var="personListUrl" htmlEscape="true" />
Person List
</li>
</ul>
welcome.jsp
This is the body Page on which header, footer and menu will be applied.
<div style="margin:10px;">
<h3>SpringMVC - Tiles3 Integration tutorial</h3>
<p>By:- XYZ</p>
</div>
The problem is the that tiles configuration is not getting applied everytime.
As you can see in this project Tiles configuration without xml the developer had a similar problem, the tiles configuration isn't working.
He found that if you call the view with the same name of the file.jsp, somethings going bad.
public static void addDefinitions(){
addDefaultLayoutDef("welcome", "welcome", "/WEB-INF/views/layout/welcome.jsp");
addDefaultLayoutDef("personList", "viewPerson", "/WEB-INF/views/layout/personList.jsp");
}
Try to use different name for the view and the file.jsp.
Try with
addDefaultLayoutDef("welcome", "Welcome", "/WEB-INF/views/layout/welcome.jsp");
Should work if the rest of the code is correct.
Maybe you need to define the view resolver's order precedence, something like this:
#Bean
public InternalResourceViewResolver internalResourceViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
//resolver.setOrder(2);
return resolver;
}
//configuracion de apache tiles
#Bean
public TilesViewResolver getTilesViewResolver() {
TilesViewResolver tilesViewResolver = new TilesViewResolver();
tilesViewResolver.setViewClass(TilesView.class);
// tiles view resolver va ha ser el primero en mostrarse
tilesViewResolver.setOrder(1);
return tilesViewResolver;
}

Resources