Java Spring MVC項目搭建(三)——“Hello World”

來源:互聯網
上載者:User

標籤:產生   結構   mvc   cat   png   ppi   src   ram   run   

在Spring 的設定檔裡,我們定義了一個bean ,Spring 會在啟動時候會產生對象。

<bean id = "helloworld" class="com.game.controller.Helloworld">  </bean>

現在我們來看看這個檔案

首先在Java Resource -> src上 右鍵點擊-> new ->package  建立com.game.controller 建立一個包,之後在這個包上右鍵點擊new->class ,類名為Helloworld,之後點擊Finish.之後項目結構是這樣:

下面我看看看Helloworld.java裡面的內容:

package com.game.controller;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class Helloworld {    @RequestMapping("/helloworld")  //此處控制瀏覽器裡訪問路徑 具體為:/SpringDemo/helloworld    public void helloWorld(HttpServletRequest request, HttpServletResponse response) throws IOException {                //輸出字串        response.getWriter().append("hello world");    }}

helloworld內容很簡單通過servlet輸出對象輸出字串“hello world”。下面我們啟動伺服器並且在瀏覽器裡運行看看結果。

首先設定管理員(事先必須安裝好tomcat),在Helloworld.java右鍵選擇 Run Server,選中我們配置好的tomcat 8.5

啟動伺服器:

啟動成功之後,開啟瀏覽器輸入http://localhost:8080/SpringDemo/helloworld :

Java Spring MVC項目搭建(三)——“Hello World”

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.