springboot的web开发springmvc+jsp

9 篇文章 0 订阅
3 篇文章 0 订阅

 

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

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

核心代码:

 

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值