Spring VelocityViewResolver not resolving - spring

I get the following error when trying to set up a Spring project with Velocity.
PageNotFound - No mapping found for HTTP request with URI [/SpringMVCVelocity/Enquiries/viewAllEnquiries] in DispatcherServlet with name 'mvc-dispatcher'
I have set up the spring context.xml as-
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd">
<context:component-scan base-package="ecommerce.dao" />
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/view/"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="false"/>
<property name="prefix" value=""/>
<property name="suffix" value=".vm"/>
<property name="order" value="-1"/>
<property name="exposeSpringMacroHelpers" value="true"></property>
</bean>
<mvc:annotation-driven/>
<!-- Forwards requests to the "/" resource to the "welcome" view -->
<mvc:view-controller path="/" view-name="welcome"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="ecommerce"/>
</bean>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<!-- Saves a locale change using a cookie -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
<!-- Application Message Bundle -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/messages" />
<property name="cacheSeconds" value="0" />
</bean>
<!--
FLOW HANDLING
-->
<!-- Enables FlowHandler URL mapping -->
<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
<property name="flowExecutor" ref="flowExecutor" />
</bean>
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry"/>
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="flowRegistry" ref="flowRegistry" />
<property name="order" value="-1" />
</bean>
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
<webflow:flow-location path="/WEB-INF/view/flows/flow.xml"/>
</webflow:flow-registry>
<webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator" />
<bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
<property name="viewResolvers" ref="viewResolver" />
</bean>
<!--
Bean Injections
-->
<bean id="carModelDao" class="ecommerce.dao.CarModelDao"/>
<bean id="carController" class="ecommerce.controller.CarController">
<property name="carModelDao" ref="carModelDao"/>
</bean>
<bean id="veloController" class="ecommerce.controller.VelocityController">
<property name="carModelDao" ref="carModelDao"/>
</bean>
</beans>
My controller is:
#Controller
#RequestMapping("/Enquiries")
#SessionAttributes({"enqList", "search"})
public class EnquiryController {
private static final Logger logger = Logger.getLogger(EnquiryController.class);
private PagedListHolder<Enquiry> enqList = null;
private int pageSize = 10;
#Autowired
private EnquiryDao enquiryDao;
#RequestMapping("/viewAllEnquiries")
public String getAllEnquiries(#RequestParam(required=false) String page, Model model) {
if ("next".equals(page) && enqList != null) {
enqList.nextPage();
} else if ("previous".equals(page) && enqList != null) {
enqList.previousPage();
} else {
// well there is no page parameter, so it must be a new request
enqList = new PagedListHolder<Enquiry>(enquiryDao.getAllEnquiries());
enqList.setPageSize(pageSize);
}
model.addAttribute("search", new Search());
model.addAttribute("enqList", enqList);
return "viewAllEnquiries";
}
This controller is not even called. It does however successfully manage to resolve the welcome view with:
<mvc:view-controller path="/" view-name="welcome"/>

Problem solved,
I wasn't scanning the controller (context:component-scan) and had not wired the bean either in the Spring context.
So to fix it, I added it via spring injection:
<bean id="enquiryDao" class="ecommerce.dao.EnquiryDao"/>
<bean id="enquiryController" class="ecommerce.controller.EnquiryController">
<property name="enquiryDao" ref="enquiryDao"/>
</bean>

Related

Error with Spring5+hibernate+jpa+hikaricp xml configuration

Please help. The below is my XML configuration file. My application runs properly. But it says no database selected when call database.
The exception looks like below:
2018-03-24 13:44:17 DEBUG SqlExceptionHelper:139 - could not extract ResultSet [n/a]
java.sql.SQLException: No database selected
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="app.sphi" />
<mvc:resources mapping="/res/**" location="/WEB-INF/res/" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb?useUnicode=true" />
<property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- Create default configuration for Hibernate -->
<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</bean>
<!-- Configure the entity manager factory bean -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<!-- Set JPA properties -->
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
</props>
</property>
<!-- Set base package of your entities -->
<property name="packagesToScan" value="app.sphi.model" />
<!-- Set share cache mode -->
<property name="sharedCacheMode" value="ENABLE_SELECTIVE" />
<!-- Set validation mode -->
<property name="validationMode" value="NONE" />
</bean>
<!-- Configure the transaction manager bean -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Enable annotation driven transaction management -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Configure Spring Data JPA and set the base package of the repository interfaces -->
<jpa:repositories base-package="app.sphi.repo"
transaction-manager-ref="transactionManager"
entity-manager-factory-ref="entityManagerFactory" />
</beans>
I will be pleased for any suggestion.
Do not configure both jdbcUrl and dataSourceClassName, pick one style or the other. In this case, I suggest you stick with jdbcUrl.

spring mvc validation meesage param

I'm using spring mvc 4 , and there is a problem with message params in validation messages.
for example the output of #Size is : size must be between min and max.
and key bundle in hibernate validator is size must be between {min} and {max}.
this is my spring mvc config :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="net.devk"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" />
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="checkRefresh" value="true"/>
<property name="useMutableTilesContainer" value="true"/>
<property name="definitions">
<list>
<value>/WEB-INF/views/**/view.xml</value>
<value>/WEB-INF/layouts/layouts.xml</value>
</list>
</property>
</bean>
<!-- Configures the #Controller programming model -->
<!-- enables the support of annotation configuration for Spring MVC controllers,
as well as enabling Spring 3 type conversion and formatting support. Also,
support for JSR-303 Bean Validation is enabled by this tag -->
<mvc:annotation-driven validator="validator" />
<bean id="themeChangeInterceptor"
class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
<property name="paramName" value="theme" />
</bean>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g.
/?locale=de -->
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<ref bean="localeChangeInterceptor" />
<ref bean="themeChangeInterceptor" />
</mvc:interceptors>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources/ directory -->
<!-- <mvc:resources mapping="/static/**" location="/static/" /> -->
<!-- defines the static resources (for example, CSS, JavaScript, images,
and so on) and their locations so Spring MVC can improve the performance
in serving those files -->
<mvc:resources location="/resources/" mapping="/resources/**" />
<!-- Enables the mapping of the DispatcherServlet to the web application’s
root context URL, while still allowing static resource requests to be handled
by the container's default servlet -->
<mvc:default-servlet-handler />
<!-- Saves a locale change using a cookie -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="locale" />
</bean>
<bean
class="org.springframework.ui.context.support.ResourceBundleThemeSource"
id="themeSource">
<property name="basenamePrefix" value="theme.theme-" />
</bean>
<bean class="org.springframework.web.servlet.theme.CookieThemeResolver"
id="themeResolver">
<property name="cookieName" value="theme" />
<property name="defaultThemeName" value="default" />
</bean>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource" />
</bean>
<bean
class="org.springframework.web.multipart.support.StandardServletMultipartResolver"
id="multipartResolver" />
<!-- Configure to plugin JSON as request and response in method handler -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter" />
</list>
</property>
</bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
where is the problem?
finally i figured out the problem ...
i have to set
messageSource.setUseCodeAsDefaultMessage(false);
and then all message params replaced with the correct value !

where to use bean springTaskSessionFactory in JBPM process execution?

i got an example of spring configuration with jbpm here http://docs.jboss.org/drools/release/5.4.0.Final/droolsjbpm-integration-docs/html/ch.spring.html and i have implemented it.There is a bean called springTaskSessionFactory ,i don't get where this bean will be used in human task execution ?
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jbpm="http://drools.org/schema/drools-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://drools.org/schema/drools-spring org/drools/container/spring/drools-spring-1.2.0.xsd">
<!-- persistence & transactions-->
<bean id="htEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="org.jbpm.task" />
</bean>
<bean id="htEm" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="htEmf"/>
</bean>
<bean id="jpaTxMgr" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="htEmf" />
<!-- this must be true if using the SharedEntityManagerBean, and false otherwise -->
<property name="nestedTransactionAllowed" value="true"/>
</bean>
<bean id="htTxMgr" class="org.drools.container.spring.beans.persistence.HumanTaskSpringTransactionManager">
<constructor-arg ref="jpaTxMgr" />
</bean>
<!-- human-task beans -->
<bean id="systemEventListener" class="org.drools.SystemEventListenerFactory" factory-method="getSystemEventListener" />
<bean id="taskService" class="org.jbpm.task.service.TaskService" >
<property name="systemEventListener" ref="systemEventListener" />
</bean>
<bean id="springTaskSessionFactory" class="org.jbpm.task.service.persistence.TaskSessionSpringFactoryImpl"
init-method="initialize" depends-on="taskService" >
<!-- if using the SharedEntityManagerBean, make sure to enable nested transactions -->
<property name="entityManager" ref="htEm" />
<property name="transactionManager" ref="htTxMgr" />
<property name="useJTA" value="false" />
<property name="taskService" ref="taskService" />
</bean>
</beans>

Struts2+Spring 3 validations issue

I am trying to use struts2 validations. I have a setup with Struts2.2.3 and Spring 3.0.5. I have a -validation.xml in place. Fields are getting validated and error message is getting displayed on the server console, but its not getting displayed on the UI. Also action is not getting forwarded to the results specified for the "input" tag.
My action class does not extend from ActionSupport instead it implements Preparable. Is this could be the problem?
So I also tried with my Action class define as
public class CandidateAction extends ActionSupport implements Preparable
on hitting the page i am getting following error
Invalid action class configuration that references an unknown class named [candidateAction]
even though i have a bean defined with name "candidateAction" in the application context.xml
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="dao" class="org.kovid.dao.impl.DaoImpl" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/xyz" />
<property name="username" value="abc" />
<property name="password" value="abc" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="baseAction" scope="prototype" class="org.kovid.action.BaseAction">
</bean>
<bean id="candidateAction" scope="prototype"
class="org.kovid.matrimony.action.CandidateAction">
<constructor-arg ref="dao" />
</bean>
<bean id="simpleSearchAction" scope="prototype"
class="org.kovid.matrimony.action.SimpleSearchAction">
<constructor-arg ref="dao" />
</bean>
<bean id="advanceSearchAction" scope="prototype"
class="org.kovid.matrimony.action.AdvanceSearchAction">
<constructor-arg ref="dao" />
</bean>
<bean id="imageAction" scope="prototype"
class="org.kovid.matrimony.action.ImageAction">
<constructor-arg ref="dao" />
</bean>
</beans>

Autowire doesn't work on servlets

I can't figure out why Autowired of DAOs classes works fine in Test classes but not in servlets. web.xml file load correctly context configuration as i can see by the log, so the problem must be in my applicationContext file.
This is my servlet:
#Controller
public class TestAutowired extends HttpServlet {
private static final long serialVersionUID = 1L;
#Autowired
private IUserDao uDao;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
uDao.getUserById(1);
}
}
And this is my ApplicationContext (configuration.xml):
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring configuration -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=...>
<context:component-scan base-package="com.firststepteam.dao">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<context:component-scan base-package="com.firststepteam.services">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<context:component-scan base-package="com.firststepteam.servlet">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- View resolver -> JSP -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- PropertyPlaceholderConfigurer -->
<bean id="placeHolder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:hostingData.conf" />
</bean>
<!-- DataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.db.driverClassName}" />
<property name="url" value="${jdbc.db.url}" />
<property name="username" value="${jdbc.db.username}" />
<property name="password" value="${jdbc.db.password}" />
</bean>
<!-- JPA E HIBERNATE -->
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="generateDdl" value="true" />
</bean>
<!-- ENTITY MANAGER FACTORY -->
<bean id="dbEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="fs_db" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="dbEntityManagerFactory" />
</bean>
<bean class = "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
</beans>
any hint?
The servlet lifecycle is managed by the container, not Spring.

Resources