springmvc的xml版本和注解版本

感觉还是注解方式比较实用

xml方式

web.xml

<!-- 读取spring mvc的核心配置文件 -->

<servlet>
<servlet-name>yxl</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>yxl</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

yxl-servlet.xml

<!-- 默认映射 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>

<!-- 第二种映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="shxt.do">indexController</prop>
</props>
</property>
</bean>

<!-- 第三种映射 -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>

<bean id="indexController" name="/index" class="cn.com.controller.IndexController"></bean>

视图解析器
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

注解方式

web.xml

<!-- 读取spring mvc的核心配置文件 -->
<servlet>
<servlet-name>yxl</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置参数 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/yxl.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>yxl</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

yxl.xml

<!-- 启动注解 -->
<mvc:annotation-driven/>
<context:component-scan base-package="cn.com.controller"></context:component-scan>
<!-- 排除静态资源 -->
<mvc:default-servlet-handler/>
<!-- 配置视图解析器 帮助文档 21.5.2 -->
<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>

上一篇:JavaScript移除数组元素减少长度的方法


下一篇:HTML5 离线缓存管理库