Spring MVC:noHandlerFound - spring

I am doing sample Spring mvc with Mybatis+Spring 3+Tomcat+Eclipse Indigo I got this problem
What's wrong? Any kind of advice for successful run this project. Previous problem was clear and Run this error message
org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringMVC/] in DispatcherServlet with name 'spring'
My web.xml here
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/servlet.xml
/WEB-INF/app.xml
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
My servlet.xml is
<context:annotation-config />
<context:component-scan base-package="logen.board.controller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
app.xml is here
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:logen/board/dao/BoardDao.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="BoardDao" class="logen.board.entity.BoardEntity"></bean>
and my controller
#Controller
public class Boardcontroller {
#Autowired
private SqlSession sql;
#Autowired
private BoardEntity bn;
#RequestMapping("/list.do")
private ModelAndView list(){
BoardDao dao = sql.getMapper(BoardDao.class);
ModelAndView mv = new ModelAndView();
mv.setViewName("jsp/index");
mv.addObject("list", dao.getList());
return mv;
}
}

Related

Cannot resolve MVC view "..."

I started studying Spring MVC and ran into such a problem that when creating a get request using the Controller annotation, the server gives an error 404. I searched for many solutions to this problem, but nothing came up. Perhaps the error is related to the server (Apache Tomcat).
Code from controller
message_page.jsp locate in /WEB-INF/views/
#Controller
public class HelloController {
#GetMapping("/get-message")
public String sayMessage(){
return "message_page";
}
}
File applicationContextMVC.xml located inside directory /WEB-INF
<context:component-scan base-package="ru.obukhov.springlearn"/>
<mvc:annotation-driven/>
<bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".html"/>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
<property name="enableSpringELCompiler" value="true"/>
</bean>
<bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="order" value="1"/>
<property name="viewNames" value="*"/>
</bean>
web.xml exactly the same inside /WEB-INF
<display-name>spring-mvc-app1</display-name>
<absolute-ordering/>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContextMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I try install new appache Tomcat server version: 10 instead of 9, but it's not help

NullPointerException Autowired JSF + Spring Web con Anotaciones

I have a web application with JSF + Spring but when I reference an Autowired object got an NullPointerException error.
My web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>
<!-- Spring Core-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/login.xhtml</welcome-file>
</welcome-file-list>
My spring-context.xml
<context:annotation-config/>
<context:component-scan base-package="pe.abc.defghi"/>
<aop:aspectj-autoproxy />
<tx:annotation-driven/>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/META-INF/settings.properties</value>
</list>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="PU_JMAD"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
My interface:
public interface PerfilService {
public List<Perfil> buscar();
public Perfil buscarXID(Integer id);
}
My Impl:
#Service("perfilService")
#Qualifier("impl1")
public class PerfilServiceImpl implements PerfilService {
#Override
public List<Perfil> buscar() {
return null;
}
#Override
public Perfil buscarXID(Integer id) {
return null;
}
}
My Bean:
#Component
#ManagedBean(name = "listados")
#ApplicationScoped
public class ListadoBean {
#Autowired
#Qualifier("impl1")
PerfilService perfilService;
public List<Perfil> getPerfiles() {
perfiles = perfilService.buscar();
return perfiles;
}
}
Error is produced when I call perfiles = perfilService.buscar();
because perfilService is null

use spring in vaadin

i want to use spring in vaadin
it's my config:
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>VaadinApplicationServlet</servlet-name>
<servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
<init-param>
<param-name>UI</param-name>
<param-value>com.MyUI</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>VaadinApplicationServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/Activiti"/>
<property name="username" value="postgres"/>
<property name="password" value="10"/>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="databaseType" value="postgres"/>
<property name="dataSource" ref="dataSource"/>
<property name="transactionManager" ref="transactionManager"/>
<property name="databaseSchemaUpdate" value="true"/>
<property name="deploymentResources"
value="classpath* : #{Init.path_Process}"/>
<property name="history" value="audit"/>
<property name="jobExecutorActivate" value="false"/>
</bean>
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration"/>
</bean>
<bean id="repositoryService" factory-bean="processEngine"
factory-method="getRepositoryService"/>
<bean id="runtimeService" factory-bean="processEngine"
factory-method="getRuntimeService"/>
<bean id="taskService" factory-bean="processEngine"
factory-method="getTaskService"/>
<bean id="historyService" factory-bean="processEngine"
factory-method="getHistoryService"/>
<bean id="managementService" factory-bean="processEngine"
factory-method="getManagementService"/>
<bean id="formService" factory-bean="processEngine"
factory-method="getFormService"/>
<bean id="identityService" factory-bean="processEngine"
factory-method="getIdentityService"/>
<bean id="Init" class="util.Init"/>
<context:annotation-config/>
<context:component-scan base-package="com"/>
<context:spring-configured/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
in MyUI class:
#java
#Component
#Configurable
public class MyUI extends UI {
protected void init(VaadinRequest vaadinRequest) {
...
#Autowired
private IdentityService identityService;
...
}}
this config work in junit and Ok!
but
when run in vaadin and tomcat , java.lang.NullPointerException error for identityService
where is my problem?
thanks
com.MyUI is created by the Vaadin servlet and that servlet does not know about Spring. What's happening is that your UI instance is created by reflection and isn't a Spring managed bean.
You need to use a Vaadin plugin that integrates with Spring. Please check the vaadin4spring project for more details.
Maybe you should update the class to org.vaadin.spring.servlet.SpringAwareVaadinServlet?
Try to autowire your bean explicite (e.g. in the constructor):
if (VaadinServlet.getCurrent() != null) {
try {
WebApplicationContextUtils
.getRequiredWebApplicationContext(VaadinServlet.getCurrent().getServletContext())
.getAutowireCapableBeanFactory().autowireBean(this);
} catch (BeansException e) {
LOG.error("Could not inject beans!" + this.getClass(), e); //$NON-NLS-1$
}
}
You may use ru.xpoft.vaadin.SpringVaadinServlet, check Vaadin website-addons
<servlet>
<servlet-name>MyCustom Application</servlet-name>
<servlet-class>ru.xpoft.vaadin.SpringVaadinServlet</servlet-class> ...............

Odata4J and Spring Autowiring

I am not able to auto-wire JpaTransactionManager transactionManagerOdata into the Odata ExampleProducerFactory servlet.
TransactionManagerOdata is configured OK and it can be auto-wire in any other Spring class.
In my case transactionManagerOdata is always null.
Please, do you have any suggestion how to configure Odata4J ExampleProducerFactory, so Spring is aware of this servlet.
Thank you
web.xml
<servlet>
<servlet-name>OData</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
<param-value>org.odata4j.jersey.producer.resources.ODataApplication</param-value>
</init-param>
<init-param>
<param-name>odata4j.producerfactory</param-name>
<param-value>com.sungard.webapp.odata.apex.producer.ExampleProducerFactory</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>OData</servlet-name>
<url-pattern>/example.svc/*</url-pattern>
</servlet-mapping>
ExampleProducerFactory.java
#Configurable(autowire= Autowire.BY_NAME)
public class ExampleProducerFactory implements ODataProducerFactory
{
#Autowired(required=true)
private JpaTransactionManager transactionManagerOdata;
public ODataProducer create(Properties properties)
{
EntityManagerFactory emf = transactionManagerOdata.getEntityManagerFactory();
JPAProducer tmpProd = new JPAProducer(emf, "", 500);
return tmpProd;
}
}
ApplicationContext.xml
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManagerOdata">
<property name="entityManagerFactory" ref="entityManagerFactoryOdata" />
</bean>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactoryOdata">
<property name="persistenceUnitName" value="persistenceUnitOdata" />
<property name="persistenceXmlLocation"
value="classpath*:META-INF/persistence-spring-odata.xml" />
<property name="dataSource" ref="dataSource" />
</bean>

Spring View Mapping Problem

I've got a problem with my View Mapping in the Spring Web MVC.
Dispatcher-servlet.xml:
...
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<context:component-scan base-package="de.bigbohne.smartmeter.controller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
</bean>
...
My Controller:
#Controller
public class FrontPage {
#RequestMapping("/index.htm")
public ModelAndView Index(Model mdl){
ModelAndView mav = new ModelAndView("frontPage.jsp");
return mav;
}
}
I get an 404 Error saying that it's missing /SmartMeter/WEB-INF/views/index
In my oppinion it must be: /SmartMeter/WEB-INF/views/frontPage.jsp
What am I missing? (I'm using Jetty 7.2 and Spring 3.0.5)
Edit 1:
web.xml:
<servlet>
<servlet-name>FrontController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FrontController</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Try to change
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
to
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
You need to specify a suffix to the view resolver:
<property name="suffix" value=".jsp" />

Resources