1. 程式人生 > >maven 快速搭建 SpringMVC 專案

maven 快速搭建 SpringMVC 專案

eclipse,maven

1.新建maven專案,選擇webapp

2.在pom檔案中新增以下內容  1.8是java版本 專案名稱為demo

<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.1.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.1.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.1.0</version>
			</plugin>
		</plugins>
		<finalName>demo</finalName>
	</build>

3.修改web.xml檔案 配置名為demo的servlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	xmlns:web="http://java.sun.com/xml/ns/javaee"
	
	id="WebApp_ID" version="3.0" metadata-complete="true">

  <display-name>displayname</display-name>
  
  <servlet>
    <servlet-name>demo</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
  </servlet>
  <servlet-mapping>
    <servlet-name>demo</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


</web-app>

4.新建demo-servlet.xml檔案  配置viewResolver 在本例中頁面編譯後的位置是/WEB_INF/classes/views/ 如果出現找不到頁面的情況

可以 在dos中,進入專案目錄,mvn package 打包 檢視頁面在包中的位置

<?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"  
    xmlns:p="http://www.springframework.org/schema/p"  
    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"  
    default-lazy-init="true">  
      
      
    <!-- 添加註解驅動 -->  
    <mvc:annotation-driven />  
    <!-- 預設掃描的包路徑 -->  
    <context:component-scan base-package="com.my.demo" />  

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置jsp路徑字首 -->  
        <property name="prefix" value="/WEB-INF/classes/views/"></property>  
        <!-- 配置URl字尾 -->  
        <property name="suffix" value=".jsp"></property>  
    </bean>  
      
</beans>  
5.新建controller 
package com.my.demo.web;

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

@Controller
public class HelloWorldController {
	
	@RequestMapping("/login")
	public String loginFunction(){
		return "login";
	}

}
6.在resources下,新建views資料夾。新建login.jsp檔案
<%@ page contentType="text/html;charset=UTF-8" language="java"  pageEncoding="UTF-8"  %>
<html>  
    <head>  
    <title>標題</title>  
  
    </head>   
    <body>  
    	 歡迎來到Springmvc
    </body>  
</html>  

7.tomcat添加當前專案 debug啟動  如果出現404,檢視tomcat的path是不是“demo”

http://localhost:8080/demo/login 訪問頁面


目錄結構如下