首页 > 代码库 > [Spring MVC] - Spring MVC环境搭建

[Spring MVC] - Spring MVC环境搭建

1) 复制Spring所需要的lib包


       

(这是SSH所需要的lib包,如果只使用spring,可以移除一些包)

 

2) 配置web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" 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">    <display-name>Test Spring MVC - 1</display-name>        <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring.xml</param-value>    </context-param>    <servlet>        <servlet-name>dispatcher</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value></param-value>        </init-param>        <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>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>

这里,classpath:spring.xml,意思是使用spring.xml做为配置文件。文件放在src下。

这样,使用spring+spring mvc,可以统一一个配置文件。

 

3) spring.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:tx="http://www.springframework.org/schema/tx"    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-3.0.xsd     http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.0.xsd    http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">    <!--注解说明 -->    <context:annotation-config />        <!-- 默认的注解映射的支持 -->    <mvc:annotation-driven />        <!-- 把标记了@Controller注解的类转换为bean -->    <context:component-scan base-package="com.my" />        <!-- 视图解释类 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/"/>        <property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  -->        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />    </bean>    </beans>

这几项配置都是必需的。

这里使用的是annotation,无配置方法使用spring mvc

 

[Spring MVC] - Spring MVC环境搭建