美文网首页
maven Web项目集成Spring MVC(11)

maven Web项目集成Spring MVC(11)

作者: 又是那一片天 | 来源:发表于2017-08-25 14:27 被阅读0次
基础配置build
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <!-- <version>2.3.2</version>不指定最新版本 -->
                <configuration>
                    <!-- 根据实际情况设置自己的jdk -->
                    <source>1.8</source><!-- 源码使用的开发版本 -->
                    <target>1.8</target><!-- 编译版本 -->
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
maven web项目中没有servlet-api这些都是要导入打包不用加入
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
要集成Spring MVC要导入相关jar
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
在resources文件夹新建Spring 配置文件并配置(这个文件夹是存放资源文件的地方)
<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!-- 使用注解 -->
    <mvc:annotation-driven />
    <!-- 视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 重定向时是否添加上下文路径 -->
        <property name="redirectContextRelative" value="true"></property>
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 要扫描的包 -->
    <context:component-scan base-package="handler"></context:component-scan>
</beans>
在web.xml配置spring mvc
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>TestWeb</display-name>
<!-- spring mvc处理链接 -->
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!-- 容器加载启动该Servlet -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/show/*</url-pattern>
    </servlet-mapping>
</web-app>
  • 配置完成 可以正常书写代码

相关文章

网友评论

      本文标题:maven Web项目集成Spring MVC(11)

      本文链接:https://www.haomeiwen.com/subject/pgpudxtx.html