做web开发的同学,肯定都熟悉web.xml这个文件。比如servlet、filter、Listener等都是在这个文件里配置的。比如你用了struts、spring等框架,都需要在web.xml中进行配置。我们来看一个web.xml的配置。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" id="leopard"> <display-name>example</display-name> <absolute-ordering> <name>leopard_webfragment</name> </absolute-ordering> </web-app>
我擦,擦亮眼睛来看?这是什么配置?好像没有见过!你刚说的servlet、filter、listerner的配置呢?
是的,你没有看错?Leopard的xml文件就是这么简单了。细节被我们隐藏了,隐藏到leopard_webfragment里了。这就是servlet3.0的好处之一。
那么leopard_webfragment里究竞配置了些什么东西呢?
<?xml version="1.0" encoding="UTF-8"?>
<web-fragment xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd"
version="3.0">
<name>leopard_webfragment</name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<error-page>
<error-code>500</error-code>
<location>/error.do</location>
</error-page>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>false</el-ignored>
<page-encoding>utf-8</page-encoding>
</jsp-property-group>
</jsp-config>
<!-- -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/leopard-web/applicationContext.xml
</param-value>
</context-param>
<context-param>
<param-name>contextClass</param-name>
<param-value>io.leopard.web.LeopardXmlWebApplicationContext</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>web</servlet-name>
<servlet-class>io.leopard.web.mvc.LeopardDispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Character Encoding filter -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 用户信息包装 -->
<filter>
<filter-name>leopardFilter</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>leopardFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-fragment>
你想要的我们都帮你配置了?这就是约定优于配置的好处!
789

被折叠的 条评论
为什么被折叠?



