springboot+maven+springmvc+jsp realizes web development and page jump
Core code:
pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.7.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency><!--tomcat embed--> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <finalName> webmaven </finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Springboot's tomcat starter (main method execution, requires tomcat embedded)
/** * @SpringBootApplication=Use the following three together * @Configuration @EnableAutoConfiguration @ComponentScan * */ @SpringBootApplication public class SpringBootTomcatStarter { public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootTomcatStarter.class, args); } }
control class
@Controller public class HelloController { //Read the configuration from application.properties, if you can't get it, the default value is Hello //@Value("${application.helloname:Hello2}") private String helloname; @RequestMapping("hello1") @ResponseBody public String hello1() { System.out.println("hello1"); return "Hello World! java"; } @RequestMapping("hello2") @ResponseBody public Map<String, Object> hello2() { System.out.println("hello2"); Map<String, Object> m = new HashMap<String, Object>(); m.put("name", "hello2"); return m; } @RequestMapping("hello3") public ModelAndView hello3() { System.out.println("hello3"); ModelAndView mav = new ModelAndView("hello"); mav.addObject("name", "hello3"); return mav; } //@PostMapping @GetMapping("hello4")//==@RequestMapping(value = "/hello3", method = RequestMethod.GET) public String hello4(Model model) { System.out.println("hello4"); model.addAttribute("name", "hello4"); return "hello"; } }
application.properties
server.port=8080 server.context-path=/webmaven application.message:hello #spring.mvc.view.prefix: /WEB-INF/view/ #spring.mvc.view.suffix: .jsp spring.mvc.view.prefix=/WEB-INF/view/ spring.mvc.view.suffix=.jsp
jsp location: src/main/webapp/WEB-INF/view/hello.jsp