This is my web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/app-root.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
This is my springmvc-servlet.xml
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.***.***">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
This is my app-root.xml
<import resource="classpath:spring/app-dao.xml"/>
<import resource="classpath:spring/shiro.xml"/>
<import resource="classpath:spring/app-timer.xml"/>
<import resource="classpath:spring/app-context.xml"/>
This is my app-context.xml
<context:component-scan base-package="com.xinshen.meeting">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
This is my app-datasource.xml
<bean id="adminTxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="adminTxManager"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:spring/mybatis-config.xml"/>
<!--扫描entity包,使用别名-->
<property name="typeAliasesPackage" value="com.xinshen.meeting.model"/>
<property name="mapperLocations" value="classpath*:mappers/*.xml"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.***.***.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
when I add #Transactional to Controller ,transaction it works but Service it does`t work!
the following pic is #Transactional on Service
enter image description here
the following pic is #Transactional on Controller
enter image description here
I really can`t finger it out,Thanks for help!
I think You have to enable transaction using configuration add this annotation #EnableTransactionManagement in a config class
Create a class for configuration :
#Configuration
#ComponentScan(basePackages = { "Your services package or base package" })
#EnableTransactionManagement
public class MyConfig {
}
If your service are not implementing interface, you need to set proxy-target-class="true" in tag tx:annotation-driven.
You also should add #Transactional on the service class, or on the function invoked by spring bean directly.
Related
It's My Project
enter image description here
It's My spring-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="com.SpringDemo.Controller"/>
<!-- Thymeleaf Template Resolver -->
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML" />
<property name="order" value="1" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<property name="enableSpringELCompiler" value="true" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="viewNames" value="thymeleaf/*"/>
<property name="templateEngine" ref="templateEngine" />
</bean>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="" />
<!--<property name="suffix" value=".jsp" />-->
<property name="order" value="2" />
<property name="viewNames" value="*.jsp" />
</bean>
<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
</beans>
It's My Controller
#Controller
public class HomeController{
//It's OK
#RequestMapping("/home")
public String showHomePage(Model model){
model.addAttribute("name","spring-mvc");
return "thymeleaf/testTh";
}
//It's WRONG
#RequestMapping("/index")
public ModelAndView test(ModelAndView mv){
mv.addObject("name","erer");
mv.setViewName("index.jsp");
return mv;
}
}
It's My Exception
enter image description here
In's My Web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<!-- 加载springMVC的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I want that all requests are first passed through the controller.
And then return to the page
Please import spring-boot-starter-thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
I think the problems may be located on the returned view name. Since the view name is the same as the /index path, so maybe it will be dispatched by spring dispatcher again. Maybe you can try to change the view as another name to have a try.
I have a problem integrating this two technologies.
We have an GWT application integrated with Spring but, additionally to GWT application we need to display HTML files using Thymeleaf for special modules.
The GWT application works well but when I try to run it with Controllers and Thymeleaf enabled this fail.
I've added this to my web.xml to support the Spring Controllers use:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/spring/controller-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And my controller-context.xml has this beans:
<bean id="uiengineTemplateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/uiengine/target/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
<property name="characterEncoding" value="UTF-8" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolvers">
<array>
<ref bean="uiengineTemplateResolver"/>
</array>
</property>
</bean>
And this at the end:
<context:component-scan base-package="com.enterprise.platform.*.gui" />
There is a way to integrate this two frameworks?
I'll appreciate your help!
Thanks.
I've resolved!
First you need to exclude the main HTML file for GWT from ThymeleafViewResolver so:
controller-context.xml
<bean id="viewResolver" class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="excludedViewNames">
<array>
<value>gwt-project.html</value>
</array>
</property>
</bean>
Allow the static access for the resources of GWT application:
<mvc:resources mapping="/gwt-project/**" location="/gwt-project/" />
Create a Spring Controller with access root method:
MyController.java
#Controller
public class MyController
{
#RequestMapping(value = "/{appBeanId}", method = RequestMethod.GET)
public String root(#PathVariable String appBeanId, Locale locale,
org.springframework.ui.Model model) {
....
}
}
That's it!.
I have problem when using Velocity. I got this error when access "/my-website/login" link:
2014-04-07 16:50:48,900 16:50:48.900 [http-bio-8181-exec-3] WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/my-website/WEB-INF/views/_layouts/login.vm] in DispatcherServlet with name 'dispatcher'
My LoginController.java is
#RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView index(HttpServletRequest request) {
ModelAndView model = new ModelAndView();
model.setViewName("_layouts/login");
return model;
}
My web.xml is here
...
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/themes/*</url-pattern>
</servlet-mapping>
...
and the dispatcher.xml is
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.vm</value>
</property>
</bean>
So, what is problem? Please help me!
You should remove InternalResourceViewResolver and use VelocityViewResolver. Like this
Your dispatcher.xml should be:
<bean class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath">
<value>/WEB-INF/views/</value>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="prefix">
<value></value>
</property>
<property name="suffix">
<value>.vm</value>
</property>
<property name="toolboxConfigLocation">
<value>/WEB-INF/velocity-toolbox.xml</value>
</property>
</bean>
I am trying to implement spring mvc internationalization on my login and Home screen(page comes up after successful login) page.
I tried to do this on login page and its working successfully by using the following code,
webui-servlet.xml
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:ValidationMessages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="clientlanguage"/>
<property name="cookieMaxAge" value="31449600"/>
</bean>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
<bean id="defaultViews" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="true"/>
</bean>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" ></bean>
<mvc:annotation-driven/>
<mvc:interceptors>
<bean class="com.check.util.RequestInterceptor"/>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"></property>
</bean>
</mvc:interceptors>
login.jsp
<a href="?lang=en" >
<img src="/images/language_flags/en.jpg" class="seltdLanHeight" alt="en" />
</a>
web.xml
<display-name>WebUI</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>webui</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>LoadProperty</servlet-name>
<servlet-class>com.check.servlet.LoadProperty</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webui</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/webui-servlet.xml,
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
but when I use the same on home screen page, then language changed but it redirects to login page again.
Please let me know is there any solution that keeps me to stay on the same page from where am changing the language.
Thanks in advance
My Spring JSP is not coming up when I call it. Apparently the resource is not available. Here is my controller code.
#Controller
public class BulletinsController {
private List<Bulletin> bulletins;
private BulletinDAO bulletinDAO;
// getters and setters, including autowiring for the BulletinDAO
#RequestMapping(value = "/getApprovedBulletins", method = RequestMethod.POST)
public ModelAndView getApprovedBulletins() {
try {
bulletins = bulletinDAO.getApprovedBulletins();
mav.setViewName("WEB-INF/jsp/EnterBulletin");
if (bulletins != null) {
mav.addObject("bulletins", bulletins);
}
} catch (Exception e) {
System.out.println(e.getMessage());
mav.setViewName("WEB-INF/jsp/FailurePage");
}
return mav;
}
Here is the relevant part of jcbulboard-servlet.xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/cpc" />
</bean>
<bean id="bulletinDAO" class="com.dao.BulletinDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="bulletinsController" class="com.controller.BulletinsController">
<property name="bulletinDAO" ref="bulletinDAO" />
</bean>
Here is my web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Job Connections Bulletin Board</display-name>
<servlet>
<servlet-name>jcbulboard</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jcbulboard</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>
Try adding a view resolver to your jcbulboard-servlet.xml:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Instead of
mav.setViewName("WEB-INF/jsp/FailurePage");
It would be
mav.setViewName("FailurePage");
Assuming there is a file named:
WEB-INF/jsp/FailurePage.jsp