SpringMVC的簡單樣本,SpringMVC簡單樣本

來源:互聯網
上載者:User

SpringMVC的簡單樣本,SpringMVC簡單樣本

首先匯入所需的jar包,項目目錄結構如下:

之後需要配置一下web.xml檔案,內容如下:

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 3   <servlet> 4     <servlet-name>springDispatcherServlet</servlet-name> 5     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 6     <init-param> 7       <param-name>contextConfigLocation</param-name> 8       <param-value>classpath:applicationContext.xml</param-value> 9     </init-param>10     <load-on-startup>1</load-on-startup>11   </servlet>12   <servlet-mapping>13     <servlet-name>springDispatcherServlet</servlet-name>14     <url-pattern>/</url-pattern>15   </servlet-mapping>16 </web-app>

然後配置applicationContext.xml:

 1 <beans xmlns="http://www.springframework.org/schema/beans" 2     xmlns:context="http://www.springframework.org/schema/context" 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4     xsi:schemaLocation=" 5         http://www.springframework.org/schema/beans      6         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7         http://www.springframework.org/schema/context  8         http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 9 10     <!-- 配置自動掃描的包 -->11     <context:component-scan base-package="com.sprmvc.po"></context:component-scan>12     <!-- 配置視圖解析器 將HelloWorldController中的傳回值解析為實際的物理視圖 -->13     <bean14         class="org.springframework.web.servlet.view.InternalResourceViewResolver">15         <!-- prefix為首碼,suffix為尾碼 -->16         <property name="prefix" value="/"></property>17         <property name="suffix" value=".jsp"></property>18     </bean>19 </beans>

下面開始建立實體類User.java:

 1 package com.sprmvc.po; 2  3 public class User { 4     private String userName; 5     private String password; 6     public String getUserName() { 7         return userName; 8     } 9     public void setUserName(String userName) {10         this.userName = userName;11     }12     public String getPassword() {13         return password;14     }15     public void setPassword(String password) {16         this.password = password;17     }18 }

然後是控制層代碼:

 1 package com.sprmvc.po; 2  3 import javax.servlet.http.HttpServletRequest; 4  5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RequestMethod; 8  9 @Controller10 public class HelloWorldController {11     @RequestMapping(value="/hello",method = RequestMethod.GET)12     public String printHelloWorld(HttpServletRequest request,User user) {13         request.setAttribute("userName", user.getUserName());14         request.setAttribute("password", user.getPassword());15         return "hello";16     }17 }

這裡的value即訪問路徑,而return的"hello"通過applicationContext.xml中配置的視圖解析器會返回到hello.jsp中

接下來我們建立兩個.jsp頁面,首先是index.jsp:

1 <body>2     <form method="get" action="hello">3     使用者名稱:<input type="text" name="userName">4     密碼:<input type="password" name="password"><br>5         <input type="submit" value="提交">6     </form>7   </body>

這裡的method和action分別與控制層中的method和value的值相對應,即HelloWorldController.java中的第11行。

然後是hello.jsp:

1 <body>  2 <h1>操作成功了</h1>  3 您的使用者名稱為:${userName}<br>4 您的密碼為:${password }  5 </body>  

這樣只需要將項目載入到tomcat下就可以進行訪問了,趕快試試把。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.