How to fix 'Unable to retrieve EntityManagerFactory for unitName null' - sqlite

I am using Glassfish 5.0 with a Java Web App built in Maven framework. I have set up the Connection Pool and it is connected when you ping it.
I have placed the persistent.xml file in
src/main/java/META-INF
with the config:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ramsoPU" transaction-type="JTA">
<jta-data-source>jdbc/ramsovarvet</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
However, when trying to instantiate the EntityManager I get the error message 'Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null'
I have checked that the peristent.xml is in the classpath in the war file. it is in:
WEB-INF/classes/META-INF/persistence.xml
The JNDI name is correct, as I set it up in the server context.
Below is the code where I instantiate the EM with the persistent unit:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package uk.ac.city.douglas.varv.Job.dao;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import uk.ac.city.douglas.varv.Account.domain.Customer;
import uk.ac.city.douglas.varv.Job.domain.Boat;
import uk.ac.city.douglas.varv.Job.domain.BoatVariant;
import uk.ac.city.douglas.varv.Job.domain.BoatVariantEngine;
import uk.ac.city.douglas.varv.Job.domain.Engine;
import uk.ac.city.douglas.varv.Job.domain.Storage;
/**
*
* #author douglaslandvik
*/
#Stateless
public class VarvRepositoryJPQL implements VarvRepository {
#PersistenceContext(unitName="ramsoPU")
private EntityManager em;
#Override
public List<Customer> getAllCustomers(){
TypedQuery query = em.createQuery("SELECT c FROM Customer AS c",Customer.class);
return query.getResultList();
}
#Override
public List<Boat> getAllBoats() {
TypedQuery query = em.createQuery("SELECT b FROM Boat AS b ORDER by b.brand ASC",Boat.class);
return query.getResultList();
}
#Override
public void addEmployee() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
#Override
public void saveBoat(Boat boat) {
em.persist(boat);
}
#Override
public void saveCustomer(Customer customer) {
em.persist(customer);
}
#Override
public void eraseCustomerById(int id) {
Customer customer = em.find(Customer.class, id);
em.remove(customer);
}
#Override
public List<Storage> getAllStorages() {
TypedQuery query = em.createQuery("SELECT s FROM Storage AS s",Storage.class);
return query.getResultList();
}
#Override
public List<Engine> getAllEngines() {
TypedQuery query = em.createQuery("SELECT e FROM Engine AS e",Engine.class);
return query.getResultList();
}
#Override
public List<BoatVariant> getAllBoatVariants() {
TypedQuery query = em.createQuery("SELECT b FROM BoatVariant AS b",BoatVariant.class);
return query.getResultList();
}
#Override
public void saveBoatVariant(BoatVariant boatVariant) {
em.persist(boatVariant);
}
#Override
public List<Boat> getAllBoatsByBrand(String brand) {
TypedQuery query = em.createQuery("SELECT b FROM Boat AS b WHERE b.brand LIKE :brand ORDER BY b.brand", Boat.class);
query.setParameter("brand", brand+"%");
return query.getResultList();
}
#Override
public List<Boat> getAllBoatsByModel(String model) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
#Override
public List<Boat> getAllBoatsById(int id) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
#Override
public List<String> getAllBoatBrands() {
TypedQuery query = em.createQuery("SELECT DISTINCT b.brand FROM Boat AS b", String.class);
return query.getResultList();
}
#Override
public Boat findBoatById(int id) {
return em.find(Boat.class, id);
}
#Override
public Customer findCustomerById(int id) {
return em.find(Customer.class, id);
}
#Override
public List<Engine> findEnginesByBrand(String brand) {
TypedQuery query = em.createQuery("SELECT e FROM Engine AS e WHERE e.brand LIKE :brand ORDER BY e.brand", Engine.class);
query.setParameter("brand", brand+"%");
return query.getResultList();
}
#Override
public void addEngine(Engine engine) {
em.persist(engine);
}
#Override
public Engine findEngineById(int id) {
return em.find(Engine.class, id);
}
#Override
public void removeEngineById(int id) {
Engine engine = em.find(Engine.class, id);
em.remove(engine);
}
#Override
public List<BoatVariant> findAllBoatVariantByCustomerId(int customerId) {
TypedQuery query = em.createQuery("SELECT bv FROM BoatVariant AS bv WHERE bv.customerId = :customerId ", BoatVariant.class);
query.setParameter("customerId", customerId);
return query.getResultList();
}
//nya jävlar
#Override
public void addBoatVariantEngine(BoatVariantEngine boatVariantEngine) {
em.persist(boatVariantEngine);
}
#Override
public void removeBoatVariantEngineById(BoatVariantEngine boatVariantEngine) {
em.remove(boatVariantEngine);
}
#Override
public List<BoatVariantEngine> findBoatVariantEngineById(int boatId, int customerId, int engineId) {
TypedQuery query = em.createQuery("SELECT bve FROM BoatVariantEngine AS bve WHERE bve.customerId = :customerId AND bve.boatId = :boatId AND bve.engineId = :engineId ", BoatVariantEngine.class);
query.setParameter("customerId", customerId);
query.setParameter("boatId", boatId);
query.setParameter("engineId", engineId);
return query.getResultList();
}
#Override
public BoatVariant findAllBoatVariantByCustomerIdAndBoatId(int customerId, int boatId) {
TypedQuery query = em.createQuery("SELECT bv FROM BoatVariant AS bv WHERE bve.customerId = :customerId AND bve.boatId = :boatId", BoatVariant.class);
query.setParameter("customerId", customerId);
query.setParameter("boatId", boatId);
return (BoatVariant) query.getSingleResult();
}
#Override
public void removeBoatVariant(BoatVariant boatVariant) {
em.remove(boatVariant);
}
}
The pom file is below:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>uk.ac.city.douglas</groupId>
<artifactId>varv</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>varv</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.asayama.gwt.jquery</groupId>
<artifactId>gwt-jquery</artifactId>
<version>0.1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.bluecatcode.mockito</groupId>
<artifactId>mockito-1.10.19-extended</artifactId>
<version>1.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>varv</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>varv</warName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

The name of the persistence unit file must be persistence.xml but in your case the name of the file is persistent.xml, that's why the container can't detect the persistence unit name.
So change the persistence file name to persistence.xml. Also don't forget to call the service class VarvRepositoryJPQL using #EJB for example:
#EJB
VarvRepositoryJPQL varvRepository;
and then call it.

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

Spring RestTemplate with Jaxb2RootElementHttpMessageConverter cannot recognize JAXB annotations

I spent a lot of time to debug this problem, but I could not find a solution. So then I used a workaround but I want to share the problem in order to help someone in the same situation.
The application, in a nutshell, is a TeamCity plugin, which communicates with nexus repository.
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<artifactId>some-plugin</artifactId>
<groupId>com.something</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>some-plugin-server</artifactId>
<packaging>jar</packaging>
<properties>
<jaxb.version>2.3.0</jaxb.version>
</properties>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${jaxb.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.teamcity</groupId>
<artifactId>server-api</artifactId>
<version>${teamcity-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.teamcity</groupId>
<artifactId>server-web-api</artifactId>
<version>${teamcity-version}</version>
<type>war</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.teamcity</groupId>
<artifactId>tests-support</artifactId>
<version>${teamcity-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
There is mentiond parent but it contains nothing more than repositories and pluginRepositories settings.
Annotations above my java classes:
#XmlRootElement(name = "versioning")
#XmlAccessorType(XmlAccessType.FIELD)
public class Versioning implements Serializable {
private static final long serialVersionUID = 2L;
private String latest;
private String release;
#XmlElementWrapper(name="versions")
#XmlElement(name = "version")
private List<String> versions;
public String getLatest() {
return latest;
}
public void setLatest(String latest) {
this.latest = latest;
}
public String getRelease() {
return release;
}
public void setRelease(String release) {
this.release = release;
}
public List<String> getVersions() {
return versions;
}
public void setVersions(List<String> versions) {
this.versions = versions;
}
}
#XmlRootElement(name = "metadata")
#XmlAccessorType(XmlAccessType.FIELD)
public class MetaData implements Serializable {
private static final long serialVersionUID = 1L;
#XmlElement(name = "versioning")
private Versioning versioning;
public Versioning getVersioning() {
return versioning;
}
public void setVersioning(Versioning versioning) {
this.versioning = versioning;
}
}
The classes represents metadata.xml from nexus. From pom.xml there is no clear that teamcity dependencies adding spring 4 into my project:
Spring configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="constructor">
<bean id="versionService" class="com.aliter.teamcity.service.impl.VersionServiceImpl" autowire="byName"/>
<bean id="appController" class="com.aliter.teamcity.AppController"/>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>
</beans>
In the VersionServiceImpl.class, there is a method sending request into nexus:
private MetaData getMetaData(String url) {
HttpEntity<String> request = new HttpEntity<>(getAuthorization());
try {
ResponseEntity<MetaData> metaDataResponseEntity = restTemplate.exchange(url, HttpMethod.GET, request, MetaData.class);
return metaDataResponseEntity.getBody();
} catch (RestClientException e) {
LOGGER.error("Metadata xml cannot be obtained.", e);
return null;
}
}
private HttpHeaders getAuthorization() {
String plainCreds = "username:password";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_XHTML_XML, MediaType.TEXT_HTML));
headers.add("Authorization", "Basic " + base64Creds);
return headers;
}
I got the exception: RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.aliter.teamcity.model.MetaData] and content type [application/xml]
RestTemplate explicitly by this: private static final boolean jaxb2Present =
ClassUtils.isPresent("javax.xml.bind.Binder", RestTemplate.class.getClassLoader()); registers Jaxb2RootElementHttpMessageConverter, it was there I checked that. I did debug and stopped when spring Http response handler trying to recognize through Jaxb2RootElementHttpMessageConverter JAXB annotations above my classes and from method:
#Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return (clazz.isAnnotationPresent(XmlRootElement.class) || clazz.isAnnotationPresent(XmlType.class)) &&
canRead(mediaType);
}
It returns that annotations are not present above the classes, but they are even there. I used JDK 8.

Validation of RequestParam using custom #ValidCode annotation doesn't work

Validation of RequestParam using custom validation annotation #ValidCode doesn't work. But If use #Pattern instead it starts working. What am I doing wrong, please?
The controller:
#Validated
#RestController
public class MyEndpoint {
#RequestMapping(path = "/test", method = GET)
public MyResponse findItems(#ValidCode #RequestParam("code") String code) {
return new MyResponse("A001");
}
}
My validation annotation:
#Retention(RUNTIME)
#Target({FIELD, PARAMETER})
#Pattern(regexp = "^[A-Z0-9]{4}?$")
public #interface ValidCode {
String message() default "{ValidCode.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Web MVC config:
#Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
#Bean
public Validator validator() {
return new LocalValidatorFactoryBean();
}
#Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
methodValidationPostProcessor.setValidator(validator());
return methodValidationPostProcessor;
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
<relativePath></relativePath>
</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-validation</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
...
</dependencies>
</project>
BUT if I change controller method following way validation starts working:
#RequestMapping(path = "/test", method = GET)
public MyResponse findItems(#Pattern(regexp="^[A-Z0-9]{2}?$") #RequestParam("code") String code) {
return new MyResponse("A001");
}
Many thanks in advance.

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;
}

unable to load home view using AbstractAnnotationConfigDispatcherServletInitializer in Spring MVC

i am new to Spring MVC, i have created spring 3.2 project using maven in eclipse. i am implementing AbstractAnnotationConfigDispatcherServletInitializer class for java based configuration.
i have following plugins and dependencies in pom.xml
<build>
<plugins>
<!--source level should be 1.6 (which is not Maven default) for java EE
6 projects, so let's change it -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- When using xml-less approach, you need to disable Maven's warning
about missing web.xml -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!--We need servlet API for compiling the classes. Not needed in runtime
-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--adding spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<!-- Add Taglib support -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
Initializer class is as......
public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer{
#Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebappConfig.class};
}
#Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
WebappConfig class is as......
#Configuration
#ComponentScan(basePackages={"com.sandip.controllers"})
#EnableWebMvc
public class WebappConfig extends WebMvcConfigurerAdapter{
//add view Resolver, Tell SpingMVC where to find view scripts
#Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
and finally i have a HomeController as..........
#Controller
public class HomeContoller {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String home() {
return "home";
}
}
when i run this my project with jetty after maven build it display the following output in browser.....
springZeroXml is my project name there is no errro in console, please help ..........
I did lot of googleing and find out i need to override the addViewControllers method of WebMvcConfigurerAdapter class in WebAppConfig. and code is..........
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}

Resources