springboot的web开发springmvc+jsp

阅读更多

 

springboot+maven+springmvc+jsp实现web开发,页面跳转

 参考:https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp

核心代码:

 

pom.xml

 


		org.springframework.boot
		spring-boot-starter-parent
		1.4.7.RELEASE
		
	
	
		UTF-8
		UTF-8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-tomcat
			provided
		
		
			org.apache.tomcat.embed
			tomcat-embed-jasper
			provided
		
	

	
		webmaven
		
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
	

 

 

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 hello2() {
    	System.out.println("hello2");
    	Map m = new HashMap();
    	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

 

你可能感兴趣的:(springboot的web开发springmvc+jsp)