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

       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 页面。

转载于:https://www.cnblogs.com/topwill/p/7421520.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值