spring框架配置
前提:导入需要的包
核心配置:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>test</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<listener>
<listener-class>com.fltrp.itest2.testbuilding.web.listener.SysInitListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 应用上下文配置文件 -->
<param-value>classpath:resources/spring/applicationContext-*.xml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ico</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.swf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.mp3</url-pattern>
</servlet-mapping>
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter> <!-- 1)配置认证过滤器 -->
<filter-name>CAS Authentication Filter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter
</filter-class>
<init-param>
<param-name>casServerLoginUrl</param-name>
<!-- 此处请填写应用的登录地址 http://127.0.0.1:8080 -->
<param-value>http://127.0.0.1:8080</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http://127.0.0.1</param-value>
</init-param>
</filter>
<span><span class="comments"><!-- 配置核心servlet --></span></span> <servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:resources/spring/spring-dispatcher.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- <error-page>
<error-code>404</error-code>
<location>/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500.jsp</location>
</error-page> -->
<session-config>
<session-timeout>40</session-timeout>
</session-config>
</web-app>
2、应用框架配置
web.xml中设定spring-dispatcher.xml即配置用于开启基于注解的springMVC功能
spring-dispatcher.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解探测器 扫描base-package对应目录其中注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean-->
<context:component-scan base-package="com.test.web.controller" />
<!-- 支持上传文件配置 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 以字节为单位的最大上传文件的大小 -->
<property name="maxUploadSize" value="100000000" />
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
<!-- 国际化操作 拦截器 必需配置,可以和其它国际化方式通用 -->
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<!-- 基于Session的国际化配置 -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
<!-- 分页拦截器 -->
<bean id="paginationInterceptor"
class="com.fltrp.core.web.interceptor.PaginationInterceptor" />
<!-- 配置拦截器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="interceptors">
<list>
<ref bean="paginationInterceptor" />
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 配freemarker-->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/pages/ftl" />
<property name="defaultEncoding" value="UTF-8" />
<property name="freemarkerSettings">
<props>
<prop key="tag_syntax">square_bracket</prop>
<prop key="template_update_delay">1</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="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.######</prop>
<prop key="whitespace_stripping">true</prop>
<prop key="classic_compatible">true</prop>
<prop key="auto_import">/function/index.ftl as p,/function/spring.ftl as spring</prop>
</props>
</property>
</bean>
<!-- 多视图解析器配置 -->
<!-- map 中的key 为视图解析器的名称,如:jsp, tiles; defaultViewResolverName 的值必须为map中的一个存在的key值 -->
<bean id="viewResolver" class="com.fltrp.core.web.view.resolver.MultipleViewResolver">
<property name="defaultViewResolverName" value="jsp" />
<property name="resolvers">
<map>
<entry key="jsp">
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix">
<value>/WEB-INF/pages/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</entry>
<!-- 输出为JSON数据 -->
<entry key="json">
<bean class="com.fltrp.core.web.view.resolver.JsonViewResolver">
<property name="jsonView">
<bean class="com.fltrp.core.web.view.MyMappingJacksonJsonView" />
</property>
</bean>
</entry>
<!-- 输出为xml数据 -->
<entry key="xml">
<bean class="com.fltrp.core.web.view.resolver.XmlViewResolver">
<property name="xmlView">
<bean id="marshallingView" class="com.fltrp.core.web.view.MyMarshallingView">
<property name="marshaller">
<bean id="xStreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="autodetectAnnotations" value="true" />
</bean>
</property>
<property name="contentType" value="application/xml" />
</bean>
</property>
</bean>
</entry>
<entry key="ftl">
<bean
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<property name="suffix" value=".htm" />
<property name="contentType" value="text/html;charset=utf-8" />
<property name="exposeRequestAttributes" value="false" />
<property name="exposeSessionAttributes" value="false" />
<property name="exposeSpringMacroHelpers" value="true" />
</bean>
</entry>
</map>
</property>
</bean>
</beans>
3、welcom.ftl页面
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
4、Control写法
@RequestMapping(value = "/TestPaper")
@Controller
public class TestController extends BaseController {
@Resource
private TestService testService;
@RequestMapping(value = "/list")
public String list(Model model) {
return json串/.jsp/.xml/.ftl;
}