假設有這個一個業務:在jsp頁面上寫入數據,然后把這個數據傳遞到后台。
效果如下:
輸入信息后點擊確定,把這些信息保存到后台。
點擊確定后。來到這里:
這就是效果。
-----------------------------------------------------------------------------------------------------------------------------------
我們給出具體的案例然后給出分析:
1.jFactoryCreate.jsp這個頁面就是輸入信息的頁面。
<%@ page language="java" pageEncoding="UTF-8"%> <%@ include file="../../base.jsp"%> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form method="post"> <div id="menubar"> <div id="middleMenubar"> <div id="innerMenubar"> <div id="navMenubar"> <ul> <li id="save"><a href="#" onclick="formSubmit('insertfactory.action','_self');">確定</a></li> <li id="back"><a href="list.action">返回</a></li> </ul> </div> </div> </div> </div> <div class="textbox" id="centerTextbox"> <div class="textbox-header"> <div class="textbox-inner-header"> <div class="textbox-title">新增廠家信息</div> </div> </div> <div> <div> <table class="commonTable" cellspacing="1"> <tr> <td class="columnTitle_mustbe">廠家名稱:</td> <td class="tableContent"><input type="text" name="fullName" /></td> <td class="columnTitle_mustbe">廠家簡稱</td> <td class="tableContent"><input type="text" name="factoryName" /></td> </tr> <tr> <td class="columnTitle_mustbe">聯系人:</td> <td class="tableContent"><input type="text" name="contacts" /></td> <td class="columnTitle_mustbe">電話</td> <td class="tableContent"><input type="text" name="phone" /></td> </tr> <tr> <td class="columnTitle_mustbe">手機</td> <td class="tableContent"><input type="text" name="mobile" /></td> <td class="columnTitle_mustbe">傳真</td> <td class="tableContent"><input type="text" name="fax" /></td> </tr> <tr> <td class="columnTitle_mustbe">驗貨員</td> <td class="tableContent"><input type="text" name="inspector" /></td> <td class="columnTitle_mustbe">排序號</td> <td class="tableContent"><input type="text" name="orderNo" /></td> </tr> <tr> <td class="columnTitle_mustbe">備注</td> <td class="tableContent"><textarea name="cnote" style="height:120px;" ></textarea></td> </tr> </table> </div> </div> </form> </body> </html>
上面的代碼中有這么一句 onclick="formSubmit('insertfactory.action','_self');點擊確定后就會來到insertfactory.action這里。
2.實體類 :Factory 類:
package cn.itcast.jk.domain; import java.util.Date; import org.omg.CORBA.PRIVATE_MEMBER; public class Factory { private String id; private String fullName; private String factoryName; private String contacts; private String phone; private String mobile; private String fax; private String cnote; private String inspector; private Integer orderNo; private String createBy; private String createDept; private Date createTime; /** * @return the id */ public String getId() { return id; } /** * @param id the id to set */ public void setId(String id) { this.id = id; } /** * @return the fullName */ public String getFullName() { return fullName; } /** * @param fullName the fullName to set */ public void setFullName(String fullName) { this.fullName = fullName; } /** * @return the factoryName */ public String getFactoryName() { return factoryName; } /** * @param factoryName the factoryName to set */ public void setFactoryName(String factoryName) { this.factoryName = factoryName; } /** * @return the contacts */ public String getContacts() { return contacts; } /** * @param contacts the contacts to set */ public void setContacts(String contacts) { this.contacts = contacts; } /** * @return the phone */ public String getPhone() { return phone; } /** * @param phone the phone to set */ public void setPhone(String phone) { this.phone = phone; } /** * @return the mobile */ public String getMobile() { return mobile; } /** * @param mobile the mobile to set */ public void setMobile(String mobile) { this.mobile = mobile; } /** * @return the fax */ public String getFax() { return fax; } /** * @param fax the fax to set */ public void setFax(String fax) { this.fax = fax; } /** * @return the cnote */ public String getCnote() { return cnote; } /** * @param cnote the cnote to set */ public void setCnote(String cnote) { this.cnote = cnote; } /** * @return the inspector */ public String getInspector() { return inspector; } /** * @param inspector the inspector to set */ public void setInspector(String inspector) { this.inspector = inspector; } /** * @return the orderNo */ public Integer getOrderNo() { return orderNo; } /** * @param orderNo the orderNo to set */ public void setOrderNo(Integer orderNo) { this.orderNo = orderNo; } /** * @return the createBy */ public String getCreateBy() { return createBy; } /** * @param createBy the createBy to set */ public void setCreateBy(String createBy) { this.createBy = createBy; } /** * @return the createDept */ public String getCreateDept() { return createDept; } /** * @param createDept the createDept to set */ public void setCreateDept(String createDept) { this.createDept = createDept; } /** * @return the createTime */ public Date getCreateTime() { return createTime; } /** * @param createTime the createTime to set */ public void setCreateTime(Date createTime) { this.createTime = createTime; } }
3.給出控制器的代碼如下:
FactoryController.java:
package cn.itcast.jk.controller.basicinfo.factory; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import cn.itcast.jk.controller.BaseController; import cn.itcast.jk.domain.Factory; import cn.itcast.jk.service.FactoryService; @Controller public class FactoryController extends BaseController { @Resource FactoryService factoryService; //保存新增加的數據 @RequestMapping("/basicinfo/factory/insertfactory.action") public String insertfactory(Factory factory ) { factoryService.insert(factory); return "redirect:/basicinfo/factory/list.action"; } }
這樣就可以了.
但是好奇的是。我們在jFactoryCreate.jsp輸入信息,fullName,contacts,phone等等這些信息,那么怎么把這些信息封裝到
@RequestMapping("/basicinfo/factory/insertfactory.action") public String insertfactory(Factory factory )
使factory這個類里面填好數據。
答案:我們在jsp頁面上點擊確定之后,根據"formSubmit('insertfactory.action','_self'就能定位到public String insertfactory(Factory factory )這個方法,很明顯就可以根據方法里面的參數Factory factory來注入數據。當然前提是jsp里面的input的name要和fatory這個實體類里面的屬性名字要一模一樣。
具體參看下一篇文章:23SpringMvc_各種參數綁定方式-就是<input那種