在Spring MVC的運作機制中,主要有兩個地方會用到XML配置,
1. 在web.xml中設置DispatcherServlet,讓前端所有Request都先經過這支Servlet才決定要交由哪個控制器處理
2. Spring本身的配置文件 xxx-servlet.xml
而這兩個XML都可以用Annotation標記的方式來取代其功能,底下簡單以一個範例來記錄如何改寫:
改寫後的專案配置會是這樣:
1. 在POM中加入下列的dependency:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
2. 在原先NetBeans所建好的Maven專案中,預設已經在POM的build標籤內導入下列的Plugin:
<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>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
其中,maven-war-plugin是為了讓maven在沒有web.xml的環境下也可以順利build Web專案
而maven-compiler-plugin則是用來指定建置前後的JDK版本
3. 建立Initialization Class HelloWorldInitializer.java
package allen0818.springmvcprojwithoutxml.configuration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class HelloWorldInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(HelloWorldConfiguration.class);
ctx.setServletContext(container);
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
這邊是用來取代原先在web.xml註冊DispatcherServlet的動作
4. 建立Configuration Class HelloWorldConfiguration.java
package allen0818.springmvcprojwithoutxml.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
/**
*
* @author allen0818
*/
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "allen0818.springmvcprojwithoutxml")
public class HelloWorldConfiguration {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
這邊則是用來取代原先Spring的配置文件,其中有幾個地方要注意:
(1) @Configuration 是為了告知Spring Container這個Class中含有Spring配置會用到的Bean(ex: viewResolver)
(2) @EnableWebMvc 是用來取代原先XML中所定義的 mvc:annotation-driven,表示我們將用Annotation來配置Spring
(3) @ComponentScan(basePackages = "allen0818.springmvcprojwithoutxml")告知Spring需要搜尋哪個範圍內的Annotation標記
5. 建立Controller HelloWorldController
package allen0818.springmvcprojwithoutxml.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
*
* @author allen0818
*/
@Controller
@RequestMapping("/")
public class HelloWorldController {
@RequestMapping(method = RequestMethod.GET)
public String sayHello(ModelMap model) {
model.addAttribute("greeting", "Hello World from Spring 4 MVC");
return "welcome";
}
@RequestMapping(value = "/helloagain", method = RequestMethod.GET)
public String sayHelloAgain(ModelMap model) {
model.addAttribute("greeting", "Hello World Again, from Spring 4 MVC");
return "welcome";
}
}
6. 建立view welcome.jsp
<%--
Document : welcome
Created on : 2016/5/9, 上午 11:33:07
Author : allen0818
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome Page</title>
</head>
<body>
Greeting : ${greeting}
</body>
</html>
呈現效果如下:
參考資料:
1. http://websystique.com/springmvc/spring-4-mvc-helloworld-tutorial-annotation-javaconfig-full-example/
2. http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/mvc.html
