首页  

springboot简单例子     所属分类 spring 浏览量 1489
支持restful 接口 及 jsp视图


pom文件 


 <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.0.RELEASE</version>
  </parent>


<dependencies>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
           <groupId>org.apache.tomcat.embed</groupId>
           <artifactId>tomcat-embed-jasper</artifactId>
           <scope>provided</scope>           
</dependency>

</dependencies>




spring-boot-starter-parent 统一进行依赖版本管理


config 及 controller 类 

@Controller
@SpringBootApplication
@SpringBootConfiguration
@EnableAutoConfiguration
public class Hello {
	
	@Value("${hello.msg}")
    private String msg;
	
	@RequestMapping("/")
	@ResponseBody 
	public String home() {
		return "hello spring boot "+new Date();
	}
	// 没有配置 视图前后缀的时候使用 
	@RequestMapping("/hello")
	public String hello() {
		return "/WEB-INF/jsp/hello.jsp";
	}
	
	@RequestMapping("/hello2")
	public String hello2() {
		return "hello2";
	}
	
	@RequestMapping("/hello3")
	public String hello3(HttpServletRequest request) {
	    request.setAttribute("msg", msg);
		return "hello3";
	}

	public static void main(String[] args) {
		SpringApplication.run(Hello.class, args);
	}
}


application.properties 可选
配置 视图前后缀 
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

使用 jsp视图 增加依赖 



<dependency>
           <groupId>org.apache.tomcat.embed</groupId>
           <artifactId>tomcat-embed-jasper</artifactId>
           <scope>provided</scope>           
</dependency>



上一篇     下一篇
如何防止表单重复提交

Autowired和Resource注解的区别

springIOC容器相关注解

利用shell命令分析java线程堆栈

软件架构设计几大原则

网页上显示html和xml代码的几种方法