I can show images in a SPRING 3 MVC project - css

I can`t show images in a Spring 3 MVC project. I don´t find the error and have been reading a lot of tutorials. My code is the next:
dispatcher-servlet
<mvc:annotation-driven />
<mvc:resources location="/resources/" mapping="/resources/**"/>
<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="com.barrancofv" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
<bean id="jspViewResolver" 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>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="es"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>mymessages</value>
<value>messages</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.barrancofv.model.Empleado</value>
<value>com.barrancofv.model.Documento</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
web.xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml,
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Security.xml
<http use-expressions="true">
<!-- intercept-url seguridad de flujo de paginas -->
<intercept-url pattern="/welcome" access="ROLE_USER" />
<intercept-url pattern="/welcome/*" access="ROLE_USER" />
<intercept-url pattern="/welcome/*/" access="ROLE_USER" />
<intercept-url pattern="/welcome/*/*" access="ROLE_USER" />
<intercept-url pattern="/welcome/*/*/" access="ROLE_USER" />
<intercept-url pattern="/welcome/*/*/*" access="ROLE_USER" />
<intercept-url pattern="/welcome/*/*/*/" access="ROLE_USER" />
<intercept-url pattern="/welcome/*/*/*/*" access="ROLE_USER" />
<intercept-url pattern="/resources/**" access="permitAll" />
<form-login login-page="/login" default-target-url="/welcome" authentication-failure-url="/loginfailed" />
<logout logout-success-url="/logout" />
</http>
In my login.jsp
To put my image, I use this:
<img src="<c:url value="/resources/img/a.jpg" alt="" />
My structure is:
src/main/resources/images/a.jpg
I don´t find the error. Thanks!!!

You must configure a ressource handler in your spring configuration so that it matches your project structure
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources
mapping="/resources/**
location="/images/, classpath:/META-INF/web-resources/" />
And you need to add configure your security so that one can access this images wihtout login (to show them on the login page).
<security:intercept-url pattern="/resources/**" access="permitAll" />

I think its picking up the resources from the webapp folder.
Can you change the resources from src/main/resources/ to src/main/webapp/resources/ and try if that works.

The issue Usha pointed out is partially correct. You will want to move your resources to src/main/webapp/resources OR in your pom you can add src/main/resources as a webResource, which is different than a regular resource.
Secondly, you will not be referencing /resources/images/... in the JSP because your web resources will be placed in the root of the webapp, NOT within a /resources directory.
Finally, you have the DispatcherServlet mapped to the root context (/). In order to serve resources you should configure the default-servlet-handler. You may also want to read the section before that documentation since it talks about serving resources.
In xml config it should be as easy as
<mvc:default-servlet-handler/>

Related

Circular view path [index.jsp]: would dispatch back to the current handler URL [/index.jsp] again

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.

Spring Security authentication failure page not work

I use spring-security to manage user authentication in my spring-mvc project,but now I found when I typed a wrong username or password and login,it will show a HTTP Status 401,Authentication Failed:Bad credentials page on my web browser listed in the below.However I have configured the authentication-failure-url in my spring-security.xml file,but it doesn't work,I do not know why.
Could anyone can help me?
This is my error page that the browser shown:
This is my spring-security.xml configuration code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<security:http pattern="/interface/**" security="none"/>
<security:http pattern="/forward.html" security="none"/>
<security:http pattern="/js/**" security="none"/>
<security:http pattern="/css/**" security="none"/>
<security:http pattern="/images/**" security="none"/>
<security:http pattern="/file/**" security="none"/>
<security:http pattern="/favicon.ico" security="none"/>
<security:http pattern="/login**" security="none" />
<security:http auto-config="true" access-denied-page="/accessDenied" use-expressions="true">
<security:intercept-url pattern="/login**" access="isAnonymous()" /> -->
<security:intercept-url pattern="/common/exceptionInfo.jsp" access="isAnonymous()" />
<security:intercept-url pattern="/**" access="isFullyAuthenticated()" />
<security:session-management session-authentication-error-url="/login" invalid-session-url="/login">
<security:concurrency-control session-registry-ref="sessionRegistry" expired-url="/login"/>
</security:session-management>
<security:custom-filter before="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />
<security:custom-filter before="FORM_LOGIN_FILTER" ref="authFilter" />
<security:custom-filter before="FILTER_SECURITY_INTERCEPTOR" ref="securityFilter"/>
<security:form-login login-page="/login" authentication-failure-url="/login?failure"
authentication-details-source-ref="customAuthenticationDetailsSource"
authentication-success-handler-ref="customAuthenticationSuccessHandler"/>
<security:logout invalidate-session="true" logout-url="/logout" success-handler-ref="customLogoutSuccessHandler"/>
</security:http>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider ref="customAuthenticationProvider"/>
</security:authentication-manager>
<bean id="customUserDetailsService" class="com.lucumt.security.CustomUserDetailsService"/>
<bean id="customAuthenticationProvider" class="com.lucumt.security.CustomAuthenticationProvider">
<property name="userDetailsService" ref="customUserDetailsService"/>
</bean>
<bean id="customAuthenticationDetailsSource" class="com.lucumt.security.CustomAuthenticationDetailsSource"/>
<bean id="customAuthenticationSuccessHandler" class="com.lucumt.security.CustomAuthenticationSuccessHandler">
<property name="defaultTargetUrl" value="/home"/>
</bean>
<bean id="customLogoutSuccessHandler" class="com.lucumt.security.CustomLogoutSuccessHandler">
<property name="defaultTargetUrl" value="/login"/>
</bean>
<bean id="securityFilter" class="com.lucumt.security.CustomFilterSecurityInterceptor">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="accessDecisionManager" ref="accessDecisionManager"/>
<property name="securityMetadataSource" ref="securityMetadataSource"/>
</bean>
<bean id="accessDecisionManager" class="com.lucumt.security.CustomAccessDecisionManager"/>
<bean id="securityMetadataSource" class="com.lucumt.security.CustomInvocationSecurityMetadataSource">
<constructor-arg ref="authorityDao"/>
</bean>
<bean id="concurrencyFilter" class="org.springframework.security.web.session.ConcurrentSessionFilter">
<property name="sessionRegistry" ref="sessionRegistry" />
<property name="expiredUrl" value="/login?times" />
</bean>
<bean id="authFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<property name="sessionAuthenticationStrategy" ref="sessionStrategy" />
<property name="authenticationManager" ref="authenticationManager" />
<property name="authenticationDetailsSource" ref="customAuthenticationDetailsSource"/>
<property name="authenticationSuccessHandler" ref="customAuthenticationSuccessHandler"/>
</bean>
<bean id="sessionStrategy" class="com.lucumt.security.CustomConcurrentSessionControlStrategy">
<constructor-arg ref="sessionRegistry"/>
</bean>
<bean id="authorityDao" class="com.lucumt.dao.impl.AuthorityDaoImpl"/>
<bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl"/>
</beans>
As you can seen,I have configured the authentication-failure-urlas below,but it doesn't work!
<security:form-login login-page="/login" authentication-failure-url="/login?failure"
authentication-details-source-ref="customAuthenticationDetailsSource"
authentication-success-handler-ref="customAuthenticationSuccessHandler"/>
However,if I remove the session-management configuration as below,then the authentication-failure-url will work!
<security:session-management session-authentication-error-url="/login" invalid-session-url="/login">
<security:concurrency-control session-registry-ref="sessionRegistry" expired-url="/login"/>
</security:session-management>
<security:custom-filter before="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />
<security:custom-filter before="FORM_LOGIN_FILTER" ref="authFilter" />
The reason why I add session-management is that I want to limit the number of user that can log in at the same time via Java code.
Now that I want to perseve session-management and also want to the authentication-failure-url work,could anyone help me?Thanks in advance!
Place this in your Dispatcher-servlet.xml
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">login/login</prop>
</props>
</property>
</bean>
Its Resolve Your Problem completely.

Integrating Spring WebFlow with Thymeleaf

I am developing SpringMVC application using Thymeleaf Templates fragment. I want to add simple flow. This is my project structure and configuration:
My spring-servlet.xml file:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- Deklaracja pakietów kontrolerów: -->
<context:component-scan base-package="pl.etestownik.controller"
scoped-proxy="targetClass" />
<mvc:annotation-driven
ignore-default-model-on-redirect="true" />
<mvc:default-servlet-handler />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer" />
</property>
</bean>
<!-- Thymeleaf konfiguracja resolverów: -->
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="characterEncoding" value="UTF-8" />
<property name="cacheable" value="false" />
<property name="order" value="0"></property>
</bean>
<!--
<bean id="thymeleafViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8" />
<property name="contentType" value="text/html; charset=UTF-8" />
<property name="order" value="1" />
</bean>
-->
<bean id="thymeleafViewResolver" class="org.thymeleaf.spring4.view.AjaxThymeleafViewResolver">
<property name="viewClass" value="org.thymeleaf.spring4.view.FlowAjaxThymeleafView" />
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<property name="additionalDialects">
<set>
<bean class="nz.net.ultraq.thymeleaf.LayoutDialect" />
</set>
</property>
</bean>
<import resource="webflow.xml" />
<!-- Spring WebFlow -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/flows" />
<property name="suffix" value=".html" />
<property name="order" value="2"></property>
</bean>
</beans>
webflow.xml file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.4.xsd">
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="flowRegistry" ref="flowRegistry" />
<property name="order" value="0" />
</bean>
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
<property name="flowExecutor" ref="flowExecutor" />
</bean>
<webflow:flow-registry id="flowRegistry"
base-path="/flows" flow-builder-services="flowBuilderServices">
<webflow:flow-location id="addQuiz"
path="/adding-quiz/add-quiz-flow.xml"/>
</webflow:flow-registry>
<webflow:flow-executor id="flowExecutor"
flow-registry="flowRegistry" />
<webflow:flow-builder-services id="flowBuilderServices"
view-factory-creator="mvcViewFactoryCreator" />
<bean id="mvcViewFactoryCreator"
class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
<property name="viewResolvers" ref="thymeleafViewResolver" />
</bean>
</beans>
and add-quiz-flow.xml:
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.4.xsd"
start-state="quizName">
<view-state id="quizName" view="flows/adding-quiz/quizName" >
<transition on="addQuestion" to="quizQuestion" />
</view-state>
<view-state id="quizQuestion" view="flows/adding-quiz/quizQuestion">
<transition on="nextQuestion" to="quizQuestion" />
<transition on="finish" to="finish" />
</view-state>
<end-state id="finish"/> <!-- Jakies "zapisano do bazy, czy cos -->
And now:
Property viewResolvers in mvcViewFactoryCreator bean is set to viewResolver(so it is pointing at InternalResourceViewResolver).My flow works fine, but it is doesn't include thymleaf template fragment,There is only simple pages, without header and footer.
As I read in thymeleaf docs : Integrating Thymeleaf and WebFlow
I am supposed to add thymeleafViewResolver and change property in mvcViewFactoryCreator from
<property name="viewResolvers" ref="viewResolver" />
to
<property name="viewResolvers" ref="thymeleafViewResolver" />
In this configuration whole applcation works fine (templates are included), but when trying to start flow, typing:http://localhost:8070/addQuiz?quizName I'm, getting folowing error:
`org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/adding-quiz/quizName", template might not exist or might not be accessible by any of the configured Template Resolvers`
Do you have any idea, where I'm going wrong?
Ok, it seems that I've handled the problem. There were few mistakes:
Firstly, I've changed
<view-state id="quizName" view="/adding-quiz/quizName">
to
<view-state id="quizName" view="/flows/adding-quiz/quizName">
After that I found a bug in html file. Now, configuration looks working properly.

shiro intercept all js,picture ,css and can't access to the method

Shiro intercept all js,picture ,css and can't access to the method
This is the web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 前端控制器的配置 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--指定spring配置文件的位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 所有以.html和.json的请求都由dispatcherServlet来处理的配置 -->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!--?Shiro?filter 需放在所有filter之前 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>ftlFilter</filter-name>
<filter-class>com.cms.filter.FtlFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ftlFilter</filter-name>
<url-pattern>*.ftl</url-pattern>
</filter-mapping>
<filter>
<filter-name>manage</filter-name>
<filter-class>com.cms.filter.ManageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>manage</filter-name>
<url-pattern>/manage/*</url-pattern>
</filter-mapping>
<!-- 让web应用程序启动那个时自动添加属性文件 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- 以Listener方式启动LOG4j -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- 以Listener方式启动spring -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
<session-config>
<session-timeout>20</session-timeout>
</session-config>
<error-page>
<error-code>404</error-code>
<location>/404.htm</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.htm</location>
</error-page>
this is the applicationContext.xml
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.cms"></context:component-scan>
<mvc:annotation-driven />
<cache:annotation-driven key-generator="keyGeneratorService"/>
<mvc:resources mapping="/static/**" location="/WEB-INF/static/"
cache-period="31556926" />
<mvc:resources mapping="/upload/**" location="/upload/"
cache-period="31556926" />
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.cms.filter.GlobalInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="default" />
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="config" />
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="folder" />
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="article" />
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="media" />
</set>
</property>
</bean>
<!-- 在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码 -->
<bean id="propertyConfigurer" class="com.cms.util.PropertyUtils">
<property name="locations">
<list>
<value>classpath:shishuocms.properties</value> <!-- 指定外部文件的编码 -->
</list>
</property>
</bean>
<!-- FreeMarker的配置 -->
<bean id="freeMarkerConfigurer"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPaths" value="/WEB-INF/static/" />
<property name="defaultEncoding" value="UTF-8" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">10</prop>
<prop key="defaultEncoding">UTF-8</prop>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="boolean_format">true,false</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="number_format">#.##</prop>
<prop key="whitespace_stripping">true</prop>
</props>
</property>
</bean>
<!-- 配置 FreeMarker视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property>
<property name="cache" value="false" />
<!-- <property name="prefix" value="" /> -->
<property name="suffix" value=".ftl" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
<property name="contentType" value="text/html;charset=utf-8" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClass}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 创建SqlSessionFactory,同时指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--创建数据映射器,数据映射器必须为接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="annotationClass" value="org.springframework.stereotype.Repository" />
<property name="basePackage" value="com.cms.dao" />
</bean>
<!-- shiro -->
<bean id="securityManager"
class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="shiroDbRealm" />
</bean>
<!-- 項目自定义的Realm -->
<bean id="shiroDbRealm" class="com.cms.realm.ShiroDbRealm" ></bean>
<!-- Shiro Filter -->
<bean id="shiroFilter"
class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/" />
<property name="successUrl" value="index.htm" />
<property name="unauthorizedUrl" value="/static/manage/500" />
<property name="filterChainDefinitions">
<value>
/login = anon
/validateCode = anon
/** = authc
</value>
</property>
</bean>
<!-- 开启Shiro注解的Spring配置方式的beans。在lifecycleBeanPostProcessor之后运行 -->
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor" />
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
<!-- Shiro生命周期处理器-->
<bean id="lifecycleBeanPostProcessor"
class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- shiro为集成spring -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">/static/manage/500</prop>
</props>
</property>
</bean>
My login.ftl submit url is
action="${BASE_PATH}/admin/login.json"
The java is
#ResponseBody
#RequestMapping(value = "/login.json", method = RequestMethod.POST)
public JsonVo<String> adminLogin{}
i modify the applicationContext.xml with
<property name="filterChainDefinitions">
<value>
/** = anon
</value>
</property>
and is ok

Spring 3.0: Handler mapping issue

I am having a trouble mapping a specific URL request to one of the controllers in my project.
the URL is : http://HOSTNAME/api/v1/profiles.json
the war which is deployed is: api.war
the error I get is the following:
[PageNotFound] No mapping found for
HTTP request with URI
[/api/v1/profiles.json] in
DispatcherServlet with name 'action'
The configuration I have is the following: web.xml :
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-security.xml</param-value>
</context-param>
<!-- Cache Control filter -->
<filter>
<filter-name>cacheControlFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<!-- Cache Control filter mapping -->
<filter-mapping>
<filter-name>cacheControlFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring security filter -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<!-- Spring security filter mapping -->
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring Controller -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/v1/*</url-pattern>
</servlet-mapping>
The action-servlet.xml:
<mvc:annotation-driven/>
<bean id="contentNegotiatingViewResolver"
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="favorPathExtension" value="true" />
<property name="favorParameter" value="true" />
<!--
default media format parameter name is 'format'
-->
<property name="ignoreAcceptHeader" value="false" />
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.xstream.XStreamMarshaller" />
</constructor-arg>
</bean>
</list>
</property>
</bean>
the application context security:
<sec:http auto-config='true' >
<sec:intercept-url pattern="/login.*" filters="none"/>
<sec:intercept-url pattern="/oauth/**" access="ROLE_USER" />
<sec:intercept-url pattern="/v1/**" access="ROLE_USER" />
<sec:intercept-url pattern="/request_token_authorized.jsp" access="ROLE_USER" />
<sec:intercept-url pattern="/**" access="ROLE_USER"/>
<sec:form-login authentication-failure-url ="/login.html"
default-target-url ="/login.html"
login-page ="/login.html"
login-processing-url ="/login.html" />
<sec:logout logout-success-url="/index.html" logout-url="/logout.html" />
</sec:http>
the controller:
#Controller
public class ProfilesController {
#RequestMapping(value = {"/v1/profiles"}, method = {RequestMethod.GET,RequestMethod.POST})
public void getProfilesList(#ModelAttribute("response") Response response) {
....
}
}
the request never reaches this controller.
Any ideas?
Annotations don't do anything until they are processed. You will need
<context:component-scan base-package="path.to.controllers"/>
as a child of your root "beans" tag in order to get Spring to scan for controllers. Spring will scan for controllers in the base-package and it's descendants.
This tags requires
xmlns:context="http://www.springframework.org/schema/context"
and
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
in your root "beans" tag.

Resources