zoukankan      html  css  js  c++  java
  • maven项目中搭建spring框架

    1.首先看一下,项目结构:

    2.在pom.xml添加依赖,这样spring所需要的jar包就自动下载到项目中了

    <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.0</modelVersion>
      <groupId>com.zp</groupId>
      <artifactId>test2</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
    
       <dependencies>
       <!-- 依赖servlet,导入servlet所需jar包  --> 
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.0.1</version>
          <scope>provided</scope>
        </dependency>
        <!-- 依赖spring,导入spring所需jar包  --> 
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>4.0.0.RELEASE</version>  
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.9.2.RELEASE</version>
        </dependency>
        <!-- json的jar包 -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.11</version>
        </dependency>
        
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.12</version>
        </dependency>
       </dependencies>
      
        <build>
        <plugins>
           <!-- 声明jdk编译版本1.7  -->
           <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
              <version>3.1</version>
             <configuration>
                  <source>1.7</source>
                  <target>1.7</target>
             </configuration>
          </plugin>
          <!-- tomcat插件 -->
          <plugin>
            <groupId>org.apche.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
          </plugin>
        </plugins>
      </build>
    </project>

    3.web.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <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_3_0.xsd"
        id="schedule-console" version="3.0">
      
      <!-- 配置spring的servlet -->  
      <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
      
        <!-- Spring的log4j监听器 -->  
        <listener>  
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
        </listener>  
        
        <!-- 字符集 过滤器  -->  
        <filter>  
            <filter-name>CharacterEncodingFilter</filter-name>  
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
            <init-param>  
                <param-name>encoding</param-name>  
                <param-value>UTF-8</param-value>  
            </init-param>  
            <init-param>  
                <param-name>forceEncoding</param-name>  
                <param-value>true</param-value>  
            </init-param>  
        </filter>  
        <filter-mapping>  
            <filter-name>CharacterEncodingFilter</filter-name>  
            <url-pattern>/*</url-pattern>  
        </filter-mapping>
        
      <!-- 欢迎页 -->
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
      <!-- 设置session有效时间 -->
      <session-config>  
      <session-timeout>9999</session-timeout>  
    </session-config>
        
        </web-app>

    4.applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"  
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.9.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
        
          <!-- viewresolver -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/">
            </property>
            <property name="suffix" value=".jsp">
            </property>
        </bean>
        
        <!-- 使用如下两个class激活@RequestMapping annotation -->
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>   
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    
        <!-- 扫描service,controller -->
        <context:component-scan 
            base-package="com.zp.test"/>
    
        <!-- handlermapping @RequestMapping,
        @ResponseBody,@ExceptionHanlder -->
        <mvc:annotation-driven/>
        
    </beans>

    5.编写jsp页面

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <script type="text/javascript" src="js/jquery-1.10.1.min.js"></script>
    <script>
    function test(){
        
        $.ajax({
             url:"/test2/test.do",
             data:{},
             type:"post",
             dataType:"json",
             success:function(data){
                 alert("success");
             },
             error:function(){
                 alert("error");
             }
         });
        
        
    }
    
    </script>
    <body>
    <input type="button" value="test" onclick="test();"/>
    </body>
    </html>

    6.编写Controller

    package com.zp.test.controller;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    
    @Controller
    public class TestController {
        
          @RequestMapping("/test.do")
          @ResponseBody
          public Map<String,Object> test(){
              System.out.println("测试");
              String s = "ok";
              Map<String,Object> resMap=new HashMap<String,Object>();
              resMap.put("code", s);
              return resMap;
          }
          
    
    }

    7.右键工程——run as——run configuration...——goals:tomcat:run——点击run

    启动服务,运行项目

    代码上传至github

    地址:https://github.com/Neonuu/test2

  • 相关阅读:
    Maven下Flex国际化配置
    Adobe AIR and Flex
    jQuery: 刨根问底 attr and prop两个函数的区别
    HTML5[8]: 图文混排,图片与文字居中对齐
    HTML5[7]: 实现网页版的加载更多
    HTML5[6]:多行文本显示省略号
    HTML5[5]:在移动端禁用长按选中文本功能
    HTML5[4]:去除不必要的标签,完全使用css实现样式
    HTML5[3]:中文换行
    HTML5[2]:使用viewport控制手机浏览器布局
  • 原文地址:https://www.cnblogs.com/Neonuu/p/5180553.html
Copyright © 2011-2022 走看看