[Maven] 建立步驟 Java SpringMVC 專案

Step 1: Create a Maven Project

右鍵New -> Other -> Maven Project 

通常是直接使用default

選擇maven-archetype-webapp

Artifact Id 是專案名稱 必填 

Group ID & Package ,看你要改什麼都可以

如果建好後,可能會遇到一歇問題,可以參考我的解決方式

Eclipse 建立 Maven 專案出現 src/main 資料夾錯誤解決方式
Eclipse Maven 專案 index.jsp httpServlet was not found 解決方法

javax.servlet.http.HttpServlet 另一個解決方式

專案點右鍵 -> Properties -> Java Build Path -> Add Library...-> 加入Server Runtime -> Apache Tomcat -> Finish.


Step 2: Configure Spring

pom.xml  加入 springframework 這裡使用 4.0.1.RELEASE

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>Exceltest</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Exceltest Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <properties>
		<spring.version>4.0.1.RELEASE</spring.version>
	</properties>
  
  <dependencies>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

	<!-- Spring dependencies -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</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>
    
  </dependencies>
  
  <build>
    <finalName>Exceltest</finalName>
  </build>
  
  
  
</project>

 

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="WebApp_ID" version="3.0">
 
  <display-name>Archetype Created Web Application</display-name>
  
  <servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
 
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
	</context-param>
 
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
  
</web-app>

建立一個 dispatcher-servlet.xml 跟 web.xml 相同位子

這裡的 <context:component-scan base-package="com.programcreek.helloworld.controller" /> 

是看你設定的controller package ,然後需要再WEB-INF下新增一個 views 資料夾

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
	<context:component-scan base-package="com.programcreek.helloworld.controller" />
 
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/views/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
</beans>

Step 3: Create Spring Controller and View

建立一個 HelloWorldController.java  在 src/main/java/

這裡有使用到spring 的 annotation

package com.programcreek.helloworld.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class HelloWorldController {
	String message = "Welcome to Spring MVC!";
	 
	@RequestMapping("/hello")
	public ModelAndView showMessage(
			@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
		System.out.println("in controller");
 
		ModelAndView mv = new ModelAndView("helloworld");
		mv.addObject("message", message);
		mv.addObject("name", name);
		return mv;
	}
}

 

index.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>Spring 4 MVC - HelloWorld Index Page</title>
</head>
<body>
 
	<center>
		<h2>Hello World</h2>
		<h3>
			<a href="hello?name=Eric">Click Here</a>
		</h3>
	</center>
</body>
</html>

建立一個 helloworld.jsp 在 /WEB-INF/views/ 

<%@ 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>Spring 4 MVC -HelloWorld</title>
</head>
<body>
	<center>
		<h2>Hello World</h2>
		<h2>
			${message} ${name}
		</h2>
	</center>
</body>
</html>

專案目錄長這樣

Step 4: Run on Server

專案右鍵 Run As --> Run on Server.

仔細看 index.jsp 的連結 <a href="hello?name=Eric">Click Here</a>

hello 會去對應controller的 @RequestMapping("/hello")   ,使用?name=Eric 把值帶到後台

在controller New 一個 helloworld 的 ModelAndView,並將參數塞入回傳

ModelAndView的helloworld,則會依據dispatcher-servlet.xml 設定的目錄去找對應的JSP ->  helloworld.jsp


完成!