微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringMVC框架结合aven单独搭建

很久没有单独使用SpringMVC进行开发了,为了记录/ ,/*的问题,搭建一下复现问题。

使用idea,用maven的webapp骨架。

pom依赖:使用了maven内嵌的tomcat进行启动

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring.version>5.0.2.RELEASE</spring.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>springmvc</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
          <configuration>
            <port>8088</port>
            <path>/</path>
            <uriEncoding>UTF-8</uriEncoding>
            <server>tomcat7</server>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
View Code
<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.dispatcherServlet</servlet-class>
      <!--springmvc认会加载/WEB-INF/dispatch-servlet.xml 配置文件,所以要覆盖认的地址-->
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>WEB-INF/applicationContext*.xml</param-value>
      </init-param>
  </servlet>
 <servlet-mapping>
   <servlet-name>dispatcherServlet</servlet-name>
   <url-pattern>*.do</url-pattern>
 </servlet-mapping>
</web-app>

在applicationContext.xml文件中只开启了注解驱动。但是需要留意,在idea添加注解驱动驱动之后会自动引入cache相关的东西,导致启动时候一直报cacheManager找不到。

 

 上面说的那个注解可以暂时不用,用下面开启注解扫描就可以了。

<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--   <mvc:annotation-driven />
   <mvc:default-servlet-handler />-->
   <!-- 启用spring mvc 注解 -->
   <context:annotation-config />
   <context:component-scan base-package="com.indigo"/>
</beans>
@RestController
public class ControllerTest {

    @postconstruct
    public void init(){
        System.out.println("初始化了");
    }
    @GetMapping("/getstring.do")
   public String getString(){
       return "JUST GET";
   }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐