springboot+maven+springmvc+jsp实现web开发,页面跳转
核心代码:
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嵌入--> <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的tomcat启动器(main方法执行,需要tomcat嵌入)
/**
* @SpringBootApplication=下面三个一起用
* @Configuration
@EnableAutoConfiguration
@ComponentScan
*
*/
@SpringBootApplication
public class SpringBootTomcatStarter {
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootTomcatStarter.class, args);
}
}
控制类
@Controller
public class HelloController {
//从 application.properties 中读取配置,如取不到默认值为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位置:src/main/webapp/WEB-INF/view/hello.jsp