您的位置:首页 > 编程语言 > Java开发

Eclipse+Marven + spring mvc 新建一个 Hello world 项目

2017-08-24 09:54 120 查看
1. 打开Eclipse,菜单 File->New->Marven Project.





2.点击 Next,





3.选择 marven-archetype-webapp 点击 Next。



4. 输入 Group Id (可以理解为.net 里的解决方案名) , Artifact Id (可以理解为.net里的项目名),点击 Finish。新建项目成功!



5. 新建文件夹 main->java、main -> java -hello、main->resources->templates。



6. 在 main-> java 目录下加入 GreetingController.java 文件代码如下:

package hello;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class GreetingController {

@RequestMapping("/greeting")
public String greeting(@RequestParam(value="Obj", required=false, defaultValue="World") s name, Model model) {
model.addAttribute("name", name);
return "greeting";
}

}

7.在main->resources->templates 目录下加入 greeting.html 文件作为视图模板(视图引擎用的 thymeleaf)。

<!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>

8.在main->main->java 目录下加入 Application.java 文件。

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

9. 现在项目中很多地方都会缺少引用,将鼠标移到缺少引用的语句旁,用eclipse 的 fix project 功能修复即可。

10.右键项目,菜单中选择 Run As -> Marven install.

11.右键项目,菜单中选择 Run As -> JavaApplication.

12.根据eclipse console 中提示的端口即可访问 hello world 页面。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: