date : 2019年12月1日html
代碼參考 《Spring 實戰》(第四版),本文和書中代碼略有差別java
HelloWebAppInitializer.java
package com.yangrd.springmvc.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class HelloWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
System.out.println("getRootConfig");
return new Class<?>[]{RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
System.out.println("getServletConfig");
return new Class<?> []{WebConfig.class};
}
@Override
protected String[] getServletMappings() {
System.out.println("getServletMappings");
return new String[]{"/"};
}
}
複製代碼
RootConfig.java
package com.yangrd.springmvc.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages = {"com.yangrd.springmvc"},
excludeFilters = {@ComponentScan.Filter(type= FilterType.ANNOTATION,value = EnableWebMvc.class)})
public class RootConfig {
}
複製代碼
WebConfig.java
package com.yangrd.springmvc.config;
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.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan("com.yangrd.springmvc.controller")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
configurer.enable();
}
}
複製代碼
HelloController.java
package com.yangrd.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
/** * */
@Controller //聲明爲一個控制器
public class HelloController {
@RequestMapping(value = "/home",method = GET)//處理對 「/」 的 GET 請求
public String hello(){
return "hello"; //邏輯視圖名爲hello
}
}
複製代碼
views
views
文件夾下新建 hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
hello world
</body>
</html>
複製代碼
Add Configuration...
Tomcat Server
- Local
Deployment
- 點擊 +
,選擇 Artifact
Apply
, OK
File
- Project Structure...
Artifacts
Apply
- OK
Error:(5, 8) java: 沒法訪問javax.servlet.ServletException
找不到javax.servlet.ServletException的類文件
複製代碼
這時須要添加 javax.servlet-apiweb
瀏覽器訪問 http://localhost:8080/homespring
顯示api
hello world瀏覽器