标签:des c style class blog code
--WEB-INF | +--web.xml | +--classes | +--lib其中web.xml用来初始化配置信息,classes存放工程中的java代码编译成的class文件,lib中存放引用到jar包。下面来看web.xml的配置。
public interface Filter{ public void init(FilterConfig filterConfig) throws ServletException; public void doFilter ( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException; public void destroy(); }在web.xml中的配置如下:
<filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter>
栈式调用
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { System.err.println("before"); arg2.doFilter(arg0, arg1); System.err.println("after"); }另外,需要用<filter-mapping>来配置请求对应的<filter>。
<listener> <listener-class>com.alibaba.citrus.webx.context.WebxContextLoaderListener</listener-class> </listener>Listener用来监听客户端的请求、服务器操作等并定义在这些时间发生时应该做的操作,有四种目标事件:
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); ServletContext servletContext = wac.getServletContext(); // TODO
public void contextInitialized(ServletContextEvent event) { this.contextLoader = createContextLoader(); this.contextLoader.initWebApplicationContext(event.getServletContext()); }初始化spring的时候默认寻找的路径是/WEB-INF/applicationContext.xml,可以通过参数contextConfigLocation进行配置,比如:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:cybertronspringconfig/spring-*.xml</param-value> </context-param>
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/log4j.xml</param-value> </context-param>在需要使用的时候需要从ServletContext中获取:
servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
<servlet> <servlet-name>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping>这样的话就可以用DispatcherServlet来处理/service/的所有请求。而DispatcherServlet是springMVC的东西,将不同的请求分发给不同的Handler处理。其中load-on-startup用来描述是否在启动的时候加载该servlet。
标签:des c style class blog code
原文地址:http://blog.csdn.net/wszt_gzt/article/details/27334851