标签:代码 snapshot ott hand config 注意 地址 classes err
注:此篇有些细节没写出,此文主要写重要的环节和需要注意的地方,轻喷
选择Gradle , 勾选java 和 web。之后就是设定项目路径和名称,这里就不啰嗦了。
build.gradle文件下导入所需要的包,刷新一下。
1 group ‘com.benjious.createTest‘ 2 version ‘1.0-SNAPSHOT‘ 3 4 apply plugin: ‘java‘ 5 apply plugin: ‘war‘ 6 7 sourceCompatibility = 1.8 8 9 repositories { 10 mavenCentral() 11 } 12 13 dependencies { 14 testCompile group: ‘junit‘, name: ‘junit‘, version: ‘4.11‘ 15 testCompile group: ‘junit‘, name: ‘junit‘, version: ‘4.12‘ 16 17 18 compile‘org.springframework:spring-context:4.3.6.RELEASE‘ 19 compile‘org.springframework:spring-webmvc:4.3.6.RELEASE‘ 20 compile‘org.springframework:spring-test:4.3.6.RELEASE‘ 21 } 22
设置项目结构
贴出各个类的代码
1 public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 2 3 @Override 4 protected Class<?>[] getRootConfigClasses() { 5 return new Class[]{RootConfig.class}; 6 } 7 8 @Override 9 protected Class<?>[] getServletConfigClasses() { 10 return new Class[]{WebConfig.class}; 11 } 12 13 @Override 14 protected String[] getServletMappings() { 15 return new String[] { "/" }; 16 } 17 } 18
1 //exclude : 过滤掉 2 @Configuration 3 @ComponentScan(basePackages = {"createtest"},excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)}) 4 public class RootConfig { 5 6 } 7
1 @Configuration 2 @EnableWebMvc 3 @ComponentScan("createtest.web") 4 public class WebConfig extends WebMvcConfigurerAdapter { 5 6 7 @Bean 8 public ViewResolver viewResolver() { 9 InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 10 resolver.setPrefix("/WEB-INF/view/"); 11 resolver.setSuffix(".jsp"); 12 return resolver; 13 } 14 15 16 @Override 17 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 18 configurer.enable(); 19 } 20 21 }
1 @Controller 2 public class HomeControl { 3 4 @RequestMapping("/home") 5 public String home() { 6 return "home"; 7 } 8 9 }
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <html> 3 <head> 4 <title>Home</title> 5 </head> 6 <body> 7 <p>home界面</p> 8 </body> 9 </html>
首先配置好Tomcat ,下面是配置好的界面
运行项目, 访问 http://localhost:8080/home/
这里需要的是在配置 Tomcat 中,设置 Application Context ,例如 Application Context 为 “/home ” ,那么 该项目的服务器的根地址为 :
http://localhost:8080/home/ , 那么要显示 home.jsp 界面应该为: http://localhost:8080/home/home .
IDEA创建一个Spring MVC 框架Java Web项目,Gradle构建
标签:代码 snapshot ott hand config 注意 地址 classes err
原文地址:https://www.cnblogs.com/Benjious/p/8987527.html