美文网首页
web应用的一些零碎记录

web应用的一些零碎记录

作者: QI的咖啡 | 来源:发表于2018-12-21 15:51 被阅读0次

    一、如何利用idea创建一个maven web项目?

    1、file -- new -- project

    image.png

    2、选择maven的web模板

    image.png

    3、一些配置

    image.png image.png

    一直点next,这样一个空的web工程就创建好了

    4、创建好的web工程目录结构

    image.png

    5、补齐缺失的目录文件夹(看图操作)

    image.png image.png image.png image.png

    6、引入必要的依赖

    6.1、引入springmvc的pom依赖

    <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>
        <spring.version>4.0.6.RELEASE</spring.version>
      </properties>
    
      <dependencies>
        <!-- servlet -->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
          <version>2.5</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
        </dependency>
    
        <!-- Spring framework -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</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>
          <exclusions>
            <exclusion>
              <artifactId>spring-web</artifactId>
              <groupId>org.springframework</groupId>
            </exclusion>
          </exclusions>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jms</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>${spring.version}</version>
          <scope>test</scope>
        </dependency>
    
        <!-- spring aop start -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aspects</artifactId>
          <version>${spring.version}</version>
          <exclusions>
            <exclusion>
              <artifactId>aspectjweaver</artifactId>
              <groupId>org.aspectj</groupId>
            </exclusion>
          </exclusions>
        </dependency>
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjrt</artifactId>
          <version>1.6.11</version>
        </dependency>
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.6.11</version>
        </dependency>
        <dependency>
          <groupId>cglib</groupId>
          <artifactId>cglib</artifactId>
          <version>2.1_3</version>
          <exclusions>
            <exclusion>
              <artifactId>asm</artifactId>
              <groupId>asm</groupId>
            </exclusion>
          </exclusions>
        </dependency>
        <!-- spring aop end --> 
    

    6.2、引入jackson依赖,没有这个jackson,使用@ResponseBody注解会报416错误

     <!-- jackson -->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-core</artifactId>
          <version>2.9.3</version>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.9.3</version>
          <exclusions>
            <exclusion>
              <artifactId>jackson-annotations</artifactId>
              <groupId>com.fasterxml.jackson.core</groupId>
            </exclusion>
          </exclusions>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
          <version>2.9.3</version>
        </dependency>
    
        <!-- fast json 依赖 -->
        <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.28</version>
        </dependency>
    

    6.3、如何引入本地jar包依赖

    6.3.1 jar包一定要放在WEB-INf下的lib目录下,否则会报NoClassDefFoundError(这个是编译正常,运行时找不到正确的jar包)

    6.3.2 pom依赖的正确写法

     <!-- 添加串口通信 -->
        <dependency>
          <groupId>org.rxtx</groupId>
          <artifactId>rxtx</artifactId>
          <version>2.2</version>
          <scope>system</scope>
          <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/rxtx-2.2.jar</systemPath>
        </dependency>
    

    7、web.xml的配置

    7.1 添加springmvcfilter,所有请求都交给spring容器处理

     <!-- springmvc -->
      <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>
            classpath:spring/spring-mvc-servlet.xml
          </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    

    7.2 如果要使用applicationContext.getBean(beanName)的方式获取bean要添加如下配置

     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-mvc-servlet.xml</param-value>
      </context-param>
    
    
      <!-- 添加spring上下文 -->
      <listener>
        <listener-class>com.shangda.message.listener.SuperContextLoaderListener</listener-class>
      </listener>
    

    注:spring/spring-mvc-servlet.xml这个文件在下图目录下


    image.png

    相关文章

      网友评论

          本文标题:web应用的一些零碎记录

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