1. 程式人生 > >用Eclipse 搭建一個Maven Spring SpringMVC 項目

用Eclipse 搭建一個Maven Spring SpringMVC 項目

hibernate 文件 res version 日誌 創建 oca skip www

1: 先創建一個maven web 項目:

可以參照之前的文章: 用Maven 創建一個 簡單的 JavaWeb 項目

創建好之後的目錄是這樣的;

技術分享

2: 先配置maven 修改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.0</modelVersion> <groupId>com.etoak</groupId> <artifactId>mss01</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding
> <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format> <spring.version>4.2.5.RELEASE</spring.version> <log4j.version>1.2.17</log4j.version> <slf4j.version>1.7.7</slf4j.version> </properties>
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- javaEE --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> </dependency> <!-- servlet start --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- servlet end --> <!-- spring start --> <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-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>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> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</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-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.10.1.RELEASE</version> </dependency> <!-- spring end --> <!-- log4j start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.2.4.Final</version> </dependency> <!-- log4j end --> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <skipTests>true</skipTests> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> <finalName>mss01</finalName> <!-- 這裏是 項目名 --> </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://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>mss01</display-name>
     <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 加載順序: 上下文參數  >>> 監聽器  >>> 過濾器  >>> 攔截器  -->
    
        <!-- 日誌記錄 -->
    <context-param>
        <!-- 日誌配置文件路徑 -->
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:config/properties/log4j.properties</param-value>
    </context-param>
    <context-param>
        <!-- 日誌頁面的刷新間隔 -->
        <param-name>log4jRefreshInterval</param-name>
        <param-value>6000</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:config/xml/application.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <filter>
        <filter-name>encoding</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>
    </filter>

    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 配置靜態資源映射,防止靜態資源被攔截後導致controller返回的頁面的js,css失效   這是第一種方式  還有一個方式在spring-mvc.xml中 -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
        <url-pattern>*.css</url-pattern>
        <url-pattern>/assets/*"</url-pattern>
        <url-pattern>/images/*</url-pattern>
    </servlet-mapping>
    <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*:config/xml/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!-- 設置session 存活時長  60 min  -->
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
</web-app>

4: 根據web.xml 添加一些文件:

再添加文件之前先來一張添加好之後的項目架構:

技術分享

下面是: application.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"
    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 
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop.xsd 
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd
       ">
          <!-- 上面沒有帶上版本號 系統會自動匹配合適的 -->

    <!-- 采用註釋的方式配置bean -->
    <context:annotation-config />
    <!-- 自動掃描註解 -->
    <context:component-scan base-package="com.etoak.mssm" />

    <!-- 因為是簡單的maven Spring SpringMvc  項目 所以 也就沒有配置 數據源之類的; -->

</beans>

下面是: sring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="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.xsd 
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
       <!-- 上面沒有帶上版本號 系統會自動匹配合適的 -->
       
       
    <!-- 開啟註解模式驅動 -->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 自動掃描web包 ,將帶有註解的類 納入spring容器管理 -->
    <context:component-scan base-package="com.etoak.mss"/>
    
    <!-- 視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <!-- 默認的註解映射的支持 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=utf-8</value>
                        <value>application/json;charset=utf-8</value>
                        <value>application/x-www-form-urlencoded</value>
                    </list>
                    <!-- 設置 這些是為了 IE 瀏覽器 ajax 請求返回 json時  變成下載 -->
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>    
    
    <!--配置靜態資源映射,防止靜態資源被攔截後導致controller返回的頁面的js,css失效   這是第二種方式  還有一種方式在web.xml中 -->
    <!-- <mvc:resources location="/static/css/" mapping="/css/**"></mvc:resources>
    <mvc:resources location="/static/js/" mapping="/js/**"></mvc:resources>
    <mvc:resources location="/static/images/" mapping="/images/**"/> -->
</beans>

為了體現出spring mvc 的作用 寫了兩個jsp頁面 (index.jsp 和 welcome.jsp);

其中 index.jsp 放置在webapp 目錄下面 可以直接訪問 ; welcome.jsp放置在 WEB-INF 目錄下面 只能通過 controller 跳轉來訪問.

先來張圖:

技術分享

下面是index.jsp的代碼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String welcomePagePath = basePath+"index/welcome";
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index Page</title>
</head>
<body>
    This is index page .
    <br/>
    <br/>
    <a href="<%=welcomePagePath %>"> Click on me to go to welcome page . </a>
</body>
</html>

這是 welcome.jsp的代碼

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome Page </title>
</head>
<body>
    This is welcome page .
</body>
</html>

下面就是處理請求,控制跳轉的controller了

package com.etoak.mss.welcome.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="/index")
public class WelcomeController {


    @RequestMapping(value="/welcome",method=RequestMethod.GET)
    public String welcome(){
        System.out.println("進來了!");
        return "welcome";
    }
}

ok 至此 整個項目 已經搭建完成:

先進行兩個Maven 操作:clean 和 install ;

技術分享

然後再在tomcat 上運行;

是不是看到下面的頁面?

技術分享

技術分享

控制臺 也打印了

技術分享

好了 上面就是 maven 和 Spring SpringMvc 的整合 ;

接下來 準備再寫一個 maven spring spring Mvc mybatis 整合項目;

用Eclipse 搭建一個Maven Spring SpringMVC 項目