22SpringMvc_jsp页面上的数据传递到控制器的说明

22SpringMvc_jsp页面上的数据传递到控制器的说明

假设有这个一个业务:在jsp页面上写入数据,然后把这个数据传递到后台。

效果如下:

22SpringMvc_jsp页面上的数据传递到控制器的说明

输入信息后点击确定,把这些信息保存到后台。

点击确定后。来到这里:

22SpringMvc_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那种