参考:http://fhd001.iteye.com/blog/1136428 ; http://blog.csdn.net/lmy86263/article/details/51622522
我在这就只是简要的描述了,我只是测试在springboot中返回jsp和json数据。
项目整体结构如下(由于使用了Springboot,所以不用配置Spring的配置文件):
pom.xml文件内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mjduan</groupId> <artifactId>learnSpringBootJsp</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>learnSpringBootJsp Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <!-- 版本1.1.12引用了spring4.0.9 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.1.12.RELEASE</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <version>7.0.70</version> </dependency> </dependencies> <build> <finalName>learnSpringBootJsp</finalName> </build> </project>
实体类User如下所示:
public class User {
private int id; private String name; private int age;.........get set方法省略了..........
..............toString()方法省略了.........
}
TeacherController的内容如下:
package com.mjduan.project.controller;
import java.util.ArrayList;
import java.util.HashMap; import java.util.List; import java.util.Map;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView;import com.mjduan.project.entity.Student;
import com.mjduan.project.entity.User; import com.mjduan.project.util.Out; @EnableAutoConfiguration //如果不要这个会报错 @RequestMapping("/TeacherController") public class TeacherController {@RequestMapping("/returnJsp") public ModelAndView returnJsp(ModelAndView modelAndView){ modelAndView.setViewName("/hello.jsp"); modelAndView.addObject("name", "梅梅"); return modelAndView; } @RequestMapping("/returnUsers") @ResponseBody public Map returnUsers(){ Map<String,Object> map=new HashMap<String, Object>(); User user = new User(); user.setId(102356); user.setAge(23); user.setName("约瑟夫"); Student student1 = new Student(); student1.setId(6); student1.setName("梅梅"); Student student2 = new Student(); student2.setId(5); student2.setName("代代"); List<Student> list = new ArrayList<Student>(2); list.add(student1); list.add(student2); map.put("user", user); map.put("student", list); return map; } public static void main(String[] args) { SpringApplication.run(TeacherController.class, args); } }
webapp下的hello.jsp中的内容就不给出了,自己随便写点东西。
之后运行TeacherController,运行方式为Java Application(该类中有main方法),之后会看到如下图所示:
之后在浏览器中输入:http://localhost:8080/TeacherController/returnUsers 就会在网页中显示json数据了。
在浏览器中输入:http://localhost:8080/TeacherController/returnJsp 网页效果就是hello.jsp中定义的效果。
在下是个小白,只是进行上面所述的简单的测试,没有进行更多深入的了解。