下面讲解springboot-web工程 如何跳转到一个页面 和 获取一个后台返回的字符串,对象,以及json数据...
1.创建一个maven web工程
2.导入依赖
<!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ... --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- servlet依赖 --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId></dependency><!-- tomcat 的支持. --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId><scope>provided</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency>
温馨小提示:这里我是多模块项目
我是在外面引入的SpringBoot所需依赖
<!-- dependencyManagement-解决单继承问题 --><dependencyManagement><dependencies><!--springboot版本管理--><!-- SpringBoot所需依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.0.5.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
3.配置application.properties对jsp支持-前后缀
# 页面默认前缀目录spring.mvc.view.prefix=/WEB-INF/views/# 响应页面默认后缀spring.mvc.view.suffix=.jsp# 自定义属性,可以在Controller中读取application.hello=Hello Angel From application
4.编写jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>user页面</title></head><body>${msg}</body></html>
5.编写controller类
@Controllerpublic class WebController {@RequestMapping("/str")@ResponseBodypublic String json1(){return "字符串";}@RequestMapping("/obj")@ResponseBodypublic Person json2(){return new Person(1L,"对象-日期",new Date());}@RequestMapping("/array")@ResponseBodypublic List<Person> json3(){return Arrays.asList(new Person(1L,"数组-数据1",new Date()),new Person(2L,"数组-数据2",new Date()));}@RequestMapping("/index")public String jsp(Model model){model.addAttribute("msg", "后台传的数据...");return "user/index";}}
温馨小提示:官方推荐使用 @RestController=@Controller+@ResponseBody 如下使用
@RestController //@RestController=@Controller+@ResponseBody 官方推荐就使用public class WebController2 {@RequestMapping("/str2")public String json1(){return "字符串";}@RequestMapping("/obj2")public Person json2(){return new Person(1L,"对象-日期",new Date());}@RequestMapping("/array2")public List<Person> json3(){return Arrays.asList(new Person(1L,"数组-数据1",new Date()),new Person(2L,"数组-数据2",new Date()));}}
这里我用到的domain实体类
public class Person {private Long id;private String name;private Date birthDay;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss",timezone = "GMT+8")public Date getBirthDay() {return birthDay;}public void setBirthDay(Date birthDay) {this.birthDay = birthDay;}public Person(Long id, String name, Date birthDay) {this.id = id;this.name = name;this.birthDay = birthDay;}}
6.编写启动类
@SpringBootApplicationpublic class WebApplication {public static void main(String[] args) {SpringApplication.run(WebApplication.class);}}
7.启动App并测试
注意:这里通过路径返回jsp页面好像有问题,看了一下资料,发现好像是springboot的版本问题,好像1.4版本以下可以用,官方推荐我们使用模板引擎技术,不推荐使用jsp
使用Freemaker模板跳转页面可参考:/qq_38225558/article/details/85761281
最后附上项目整体结构: