王者系列之springmvc maven版本

在后台开发中,maven相当的重要,如果没有maven的话,那你就要陷入无休止的jar的烦恼中了。上篇博客简单的讲解了手动导入jar的springmvc配置,这次就来讲讲如何通过maven来配置springmvc(如果不会maven请百度)。

1 pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>com.luban.projectgroupId>
    <artifactId>firstmavenprojectartifactId>
    <version>0.0.1-SNAPSHOTversion>
    
    <packaging>warpackaging>


    
    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.9version>
            <scope>testscope>
        dependency>


        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-messagingartifactId>
            <version>4.3.9.RELEASEversion>
        dependency>

        <dependency>
            <groupId>commons-logginggroupId>
            <artifactId>commons-loggingartifactId>
            <version>1.2version>
        dependency>


        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jmsartifactId>
            <version>4.3.9.RELEASEversion>
        dependency>


        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>4.3.9.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>4.2.5.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>4.0.2.RELEASEversion>
        dependency>


        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>4.1.6.RELEASEversion>
        dependency>


        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>4.2.5.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>4.2.5.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>4.2.5.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>4.2.5.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-expressionartifactId>
            <version>4.2.5.RELEASEversion>
        dependency>


    dependencies>


    <build>
        
        <finalName>purchaseadminfinalName>
        
        <plugins>
            <plugin>
                
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.5.1version>
                <configuration>
                    
                    <source>1.8source>
                    <target>1.8target>
                    
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
        plugins>

        <resources>
            <resource>
                
                <directory>src/main/javadirectory>
                <includes>
                    
                    <include>**/*.classinclude>
                    <include>**/*.xmlinclude>
                includes>
            resource>
        resources>

    build>
project>

2 在webapp下面配置web.xml


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">
    <display-name>springmvcdisplay-name>
    <welcome-file-list>
        <welcome-file>index.htmlwelcome-file>
        <welcome-file>index.htmwelcome-file>
        <welcome-file>index.jspwelcome-file>
        <welcome-file>default.htmlwelcome-file>
        <welcome-file>default.htmwelcome-file>
        <welcome-file>default.jspwelcome-file>
    welcome-file-list>

    
    
    <servlet>
        <servlet-name>dispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath*:springmvc.xmlparam-value>
        init-param>
        
        <load-on-startup>1load-on-startup>
    servlet>


    <servlet-mapping>
        <servlet-name>dispatcherServletservlet-name>
        
        <url-pattern>/url-pattern>
    servlet-mapping>


web-app>

3 配置springmvc.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">



    
    <context:component-scan base-package="com.quyang.project"/>

    <mvc:annotation-driven/>

    
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            p:prefix="/" p:suffix=""/>
beans>

4 control

@Controller
@RequestMapping("quyang")
public class Item {

    @ResponseBody
    @RequestMapping(value = "/hh",produces = {"application/json;charset=UTF-8"})
    public String name() {
        return "helloworld";
    }

}

5 打开项目根目录,在命令行输入mvn tomcat:run 一会儿就会出现这个:

[INFO] --- tomcat-maven-plugin:1.1:run (default-cli) @ firstmavenproject ---
[INFO] Running war on http://localhost:8080/firstmavenproject
[INFO] Creating Tomcat server configuration at D:\eclipse\myweb\firstmavenproject\target\tomcat
八月 17, 2017 9:33:10 下午 org.apache.catalina.startup.Embedded start

在浏览器中请求:http://localhost:8080/firstmavenproject/quyang/hh就会返回helloworld了。这就是mvn的热部署,即使你没有装tomcat,因为他自己再带了;

你可能感兴趣的:(王者系列之springmvc maven版本)