<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 使用 Maven 和 JSTL 的 Spring MVC Hello World 示例 > 原文: [https://howtodoinjava.com/spring-mvc/spring-3-mvc-hello-world-application-with-maven-and-jstl/](https://howtodoinjava.com/spring-mvc/spring-3-mvc-hello-world-application-with-maven-and-jstl/) 在本 Spring MVC 教程中,我們將使用 Spring MVC 框架構建 Hello World 應用程序。 逐步遵循給定的說明并學習基礎知識。 我們已經使用 [JSTL](https://en.wikipedia.org/wiki/JavaServer_Pages_Standard_Tag_Library) 來編寫視圖文件。 ## 1\. 開發環境 1. [Maven](https://maven.apache.org/) 2. [Eclipse juno](https://www.eclipse.org/juno/) 3. [Tomcat 7](https://tomcat.apache.org/tomcat-7.0-doc/index.html) ## 2\. 創建 Spring MVC 應用程序 #### 2.1. 創建 Maven Web 應用程序 要創建 Maven Web 應用程序,請打開命令提示符,并將當前工作目錄放入 Eclipse 工作區。 現在執行以下命令。 ```java $ mvn archetype:generate -DgroupId=com.howtodoinjava.mvc -DartifactId=firstSpringApplication -DarchetypeartifactId=maven-archetype-webapp -DinteractiveMode=false ``` 請從上述命令中刪除換行符。 我添加它是為了使其可讀。 現在在給定命令下運行。 這會將創建的項目轉換為 Eclipse Web 項目。 ```java $ cd firstSpringApplication $ mvn eclipse:eclipse -Dwtpversion=2.0 ``` #### 2.2. 更新 Maven 依賴項 使用這些依賴關系更新`pom.xml`文件。 `pom.xml` ```java <properties> <spring.version>3.0.5.RELEASE</spring.version> </properties> <!-- Spring 3 dependencies --> <dependency> <groupid>org.springframework</groupid> <artifactId>spring-core</artifactId> <version>${spring.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactId>spring-web</artifactId> <version>${spring.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactId>jstl</artifactId> <version>1.2</version> <scope>runtime</scope> </dependency> <dependency> <groupid>taglibs</groupid> <artifactId>standard</artifactId> <version>1.1.2</version> <scope>runtime</scope> </dependency> ``` ## 3\. 創建分派器 Servlet #### 3.1. 更新`web.xml` 更新`web.xml`以獲取 Servlet 映射和 spring 配置位置。 ```java <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>www.howtodoinjava.com</display-name> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> ``` #### 3.2. Bean 配置文件 我們將需要在`WEB-INF`文件夾中寫入配置文件(`spring-mvc-servlet.xml`)。 我們可以隨意命名,但是請記住,它必須與我們在`web.xml`中聲明的 servlet 名稱匹配。 `spring-mvc-servlet.xml` ```java <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.howtodoinjava.web" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> ``` ## 3\. 創建 Spring MVC 控制器 該請求處理器將接收對特定 URL 的所有請求,并返回模型數據以查看配置的視圖處理器。 `DemoController.java` ```java package com.howtodoinjava.web; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/access") public class DemoController { @RequestMapping(method = RequestMethod.GET) public String printWelcome(ModelMap model) { model.addAttribute("message", "Spring 3 MVC Hello World !! Thanks to www.howtodoinjava.com"); return "helloWorld"; } } ``` ## 4\. 創建視圖 我們的視圖是一個 **jsp** 文件,當從服務器返回相關視圖時,它將在瀏覽器中呈現。 `helloWorld.jsp` ```java <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %> <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %> <html> <head> <title>My first example using Spring 3 MVC</title> </head> <body> <h1>Welcome message : <c:out value="${message}"></c:out></h1> </body> </html> ``` ## 5\. 演示 要運行該應用程序,請在 eclipse 中配置 tomcat 服務器。 現在,右鍵單擊項目,然后選擇運行方式。 在子菜單上,選擇**在服務器**上運行。 現在在瀏覽器中輸入:`http://localhost:8080/firstSpringApplication/access` 我們應該能夠在瀏覽器中看到消息`Spring 3 MVC Hello World !! Thanks to www.howtodoinjava.com`。 如果您在運行或部署給定應用程序時發現任何問題,請給我留言。 [下載源碼](https://docs.google.com/open?id=0B7yo2HclmjI4OS1sd0JvSEE1ckk) 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看