[bigdata-075] maven+spring+mvc web开发 示例
发布时间:2020-12-14 03:11:24 所属栏目:大数据 来源:网络整理
导读:1. 在eclipse新建一个maven项目 2. 在spring.io里找到spring mvc的示例代码 http://spring.io/guides/gs/serving-web-content/ Serving Web Content with Spring MVC 3. maven项目依次设置如下项 ? ? parent ? ? ? ? groupIdorg.springframework.boot/groupI
1. 在eclipse新建一个maven项目
2. 在spring.io里找到spring mvc的示例代码 http://spring.io/guides/gs/serving-web-content/ Serving Web Content with Spring MVC 3. maven项目依次设置如下项 ? ? <parent> ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? <artifactId>spring-boot-starter-parent</artifactId> ? ? ? ? <version>1.5.2.RELEASE</version> ? ? </parent> ? ? <dependencies> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-thymeleaf</artifactId> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-devtools</artifactId> ? ? ? ? ? ? <optional>true</optional> ? ? ? ? </dependency> ? ? </dependencies> ? ? <properties> ? ? ? ? <java.version>1.8</java.version> ? ? </properties> ?<build> ? ? ? ? <plugins> ? ? ? ? ? ? <plugin> ? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? ? ? <artifactId>spring-boot-maven-plugin</artifactId> ? ? ? ? ? ? </plugin> ? ? ? ? </plugins> ? ? </build> 4. 创建一个GreetingController控制器,在任意的包下都可以,代码如下: ------------------------- package com.ttss.demo.demo_spring_mvc; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class GreetingController { @RequestMapping("/greeting") ? ?public String greeting(@RequestParam(value="name",required=false,defaultValue="World") String name,Model model) { ? ? ? ?model.addAttribute("name",name); ? ? ? ?return "greeting"; ? ?} } ------------------------- 5. 创建sroucefold,也就是src/main/resources 在这里创建html模板,也就是 src/main/resources/templates/greeting.html 内容如下 ------------------------ <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> ? ? <title>Getting Started: Serving Web Content</title> ? ? <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> ? ? <p th:text="'Hello,' + ${name} + '!'" /> </body> </html> ------------------------ 6. 修改main函数所在的主类,修改如下: ------------------------ package com.ttss.demo.demo_spring_mvc; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App? { ? ? public static void main( String[] args ) ? ? { ? ? ? ? SpringApplication.run(App.class,args); ? ? } ? ?? } ------------------------ 7. 在项目的根目录下执行 mvn clean package,就会在target目录下生成 ?demo-spring-mvc-0.0.1-SNAPSHOT.jar 以命令 java -jar demo-spring-mvc-0.0.1-SNAPSHOT.jar 然后在浏览器打开本地地址 http://localhost:8080/greeting,既可看到结果。 http://localhost:8080/greeting?name=User也可以看到结果 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |