多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

中國最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2

wkspring教程

Spring MVC 表單處理例子

閱讀 (2051)

Spring MVC 表單處理例子

下面的例子說明了如何編寫一個簡單的基于 web 的應用程序,它利用了使用 Spring 的 Web MVC 框架的 HTML 表單。為了開始使用它,讓我們在恰當的位置使用 Eclipse IDE,然后按照下面的步驟使用 Spring 的 Web 框架來開發一個動態的基于表單的 Web 應用程序:

步驟 描述
1 創建一個名稱為 HelloWeb動態 Web 項目,并且在已創建的項目的 src 文件夾中創建一個包 com.tutorialspoint
2 將上面提到的 Spring 和其他庫拖拽到文件夾 WebContent/WEB-INF/lib 中。
3 com.tutorialspoint 包下創建一個 Java 類 StudentStudentController
4 WebContent/WEB-INF 文件夾下創建 Spring 的配置文件 Web.xmlHelloWeb-servlet.xml
5 WebContent/WEB-INF 文件夾下創建名稱為 jsp 的子文件夾。在這個子文件夾下創建視圖文件 student.jspresult.jsp
6 最后一步是創建所有的源代碼和配置文件的內容,并導出該應用程序,正如下面解釋的一樣。

這里是 Student.java 文件的內容:

package com.tutorialspoint;
public class Student {
   private Integer age;
   private String name;
   private Integer id;
   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
      return age;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }
   public void setId(Integer id) {
      this.id = id;
   }
   public Integer getId() {
      return id;
   }
}

下面是 StudentController.java 文件的內容:

package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;
@Controller
public class StudentController {
   @RequestMapping(value = "/student", method = RequestMethod.GET)
   public ModelAndView student() {
      return new ModelAndView("student", "command", new Student());
   }   
   @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
   public String addStudent(@ModelAttribute("SpringWeb")Student student, 
   ModelMap model) {
      model.addAttribute("name", student.getName());
      model.addAttribute("age", student.getAge());
      model.addAttribute("id", student.getId());      
      return "result";
   }
}

在這里,第一個 service 方法 student(),我們已經在名稱為 “command” 的 ModelAndView 對象中傳遞一個空的 Student 對象,因為 spring 框架需要一個名稱的 “command” 的對象,如果你在 JSP 文件中使用 <form:form> 標簽。所以,當 student() 方法被調用時,它返回 student.jsp 視圖。

第二個 service 方法 addStudent() 將調用 HelloWeb/addStudent URL 中的 POST 方法。你將根據提交的信息準備好你的模型對象。最后一個 “result” 視圖會從 service 方法中返回,它將導致呈現 result.jsp。

下面是 Spring Web 配置文件 web.xml 的內容

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Form Handling</display-name>

    <servlet>
        <servlet-name>HelloWeb</servlet-name>
        <servlet-class>
           org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWeb</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

下面是另一個 Spring Web 配置文件 HelloWeb-servlet.xml 的內容

<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.tutorialspoint" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

下面是 Spring 視圖文件 student.jsp 的內容

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Student Information</h2>
<form:form method="POST" action="/HelloWeb/addStudent">
   <table>
    <tr>
        <td><form:label path="name">Name</form:label></td>
        <td><form:input path="name" /></td>
    </tr>
    <tr>
        <td><form:label path="age">Age</form:label></td>
        <td><form:input path="age" /></td>
    </tr>
    <tr>
        <td><form:label path="id">id</form:label></td>
        <td><form:input path="id" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Submit"/>
        </td>
    </tr>
</table>  
</form:form>
</body>
</html>

下面是 Spring 視圖文件 result.jsp 的內容

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Submitted Student Information</h2>
   <table>
    <tr>
        <td>Name</td>
        <td>${name}</td>
    </tr>
    <tr>
        <td>Age</td>
        <td>${age}</td>
    </tr>
    <tr>
        <td>ID</td>
        <td>${id}</td>
    </tr>
</table>  
</body>
</html>

最后,下面是包含在你的 web 應用程序中的 Spring 和其他庫的列表。你僅僅需要將這些文件拖拽到 WebContent/WEB-INF/lib 文件夾中。

  • commons-logging-x.y.z.jar

  • org.springframework.asm-x.y.z.jar

  • org.springframework.beans-x.y.z.jar

  • org.springframework.context-x.y.z.jar

  • org.springframework.core-x.y.z.jar

  • org.springframework.expression-x.y.z.jar

  • org.springframework.web.servlet-x.y.z.jar

  • org.springframework.web-x.y.z.jar

  • spring-web.jar

一旦你完成了創建源代碼和配置文件后,導出你的應用程序。右鍵單擊你的應用程序,并且使用 Export > WAR File 選項,并且在 Tomcat 的 webapps 文件夾中保存你的 HelloWeb.war 文件。

現在啟動你的 Tomcat 服務器,并且確保你能夠使用標準的瀏覽器訪問 webapps 文件夾中的其他 web 頁面。現在嘗試訪問該 URL http://localhost:8080/SpringWeb/student。如果你的 Spring Web 應用程序一切都正常,你應該看到下面的結果:

在提交必需的信息之后,單擊提交按鈕來提交這個表單。如果你的 Spring Web 應用程序一切都正常,你應該看到下面的結果:

關閉
程序員人生
主站蜘蛛池模板: 自拍偷拍图区 | 久久精品视频免费 | 日韩爱爱网 | jizzjizz日本护士视频 | 中文字幕伦伦精品 | 九九这里有精品 | 性欧美高清极品xx | 视频在线a | 免费高清不卡毛片在线看 | v片免费观看 | 国产a级一级久久毛片 | 伊人网视频在线观看 | 成人在线观看视频免费 | 亚洲国产天堂久久九九九 | 中文字幕视频在线 | 婷婷在线成人免费观看搜索 | 呦视频在线一区二区三区 | 在线欧美色| 午夜视频网站在线观看 | 伊人久久大香 | 天天久| 久久在线免费观看视频 | 一区二区三区在线 | 五月天基地 | 国产精品原创永久在线观看 | 国产精品视频久久 | 国产一级一片免费播放视频 | 日本护士xxxxx高清免费 | 欧美久久超级碰碰碰二区三区 | 中文字幕在亚洲第一在线 | 亚洲欧美国产精品久久久 | 一区二区三区在线观看视频 | 日韩免费 | 国产精品亚洲精品不卡 | 美女啪啪国产 | 国产精品久久久久久久成人午夜 | 手机看片在线精品观看 | 国产精品久久精品视 | 免费一级欧美毛片 | 亚洲成人高清在线观看 | 亚洲精品国产网红在线一区 |