JavaWeb学习:综合案例保存客户

一、访问保存页面路径

<A class=style2 href="customer_saveUI.action" target=main>- 新增客户</A>

二、CustomerAction的saveUI方法

public String saveUI() {
    return "savaUI";
    }

三、配置struts.xml

    <package name="crm" extends="struts-default" namespace="/">
        <global-allowed-methods>regex:.*</global-allowed-methods>
        <action name="customer_*" class="com.xxx.web.action.CustomerAction" method="{1}">
            <result name="findSuccess">/jsp/customer/list.jsp</result>
            <result name="savaUI">/jsp/customer/add.jsp</result>
        </action>
    </package>

四、add.jsp

    <FORM id=form1 name=form1
        action="${pageContext.request.contextPath }/customer_save.action"
        method=post>
        
        <TABLE cellSpacing=0 cellPadding=0 width="98%" border=0>
            <TBODY>
                <TR>
                    <TD vAlign=top width="100%" bgColor=#ffffff>
                        <TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
                            <TR>
                                <TD class=manageHead>当前位置:客户管理 &gt; 添加客户</TD>
                            </TR>
                            <TR>
                                <TD height=2></TD>
                            </TR>
                        </TABLE>
                        <TABLE cellSpacing=0 cellPadding=5  border=0>
                            <TR>
                                <td>客户名称:</td>
                                <td>
                                <INPUT class=textbox id=sChannel2
                                                        style="WIDTH: 180px" maxLength=50 name="cust_name">
                                </td>
                                <td>客户级别 :</td>
                                <td>
                                <INPUT class=textbox id=sChannel2
                                                        style="WIDTH: 180px" maxLength=50 name="cust_level">
                                </td>
                            </TR>
                            <TR>
                                <td>信息来源 :</td>
                                <td>
                                <INPUT class=textbox id=sChannel2
                                                        style="WIDTH: 180px" maxLength=50 name="cust_source">
                                </td>
                                <td>所属行业 :</td>
                                <td>
                                <INPUT class=textbox id=sChannel2
                                                        style="WIDTH: 180px" maxLength=50 name="cust_industry">
                                </td>
                            </TR>
                            <TR>
                                <td>固定电话 :</td>
                                <td>
                                <INPUT class=textbox id=sChannel2
                                                        style="WIDTH: 180px" maxLength=50 name="cust_phone">
                                </td>
                                <td>移动电话 :</td>
                                <td>
                                <INPUT class=textbox id=sChannel2
                                                        style="WIDTH: 180px" maxLength=50 name="cust_mobile">
                                </td>
                            </TR>
                            <tr>
                                <td rowspan=2>
                                <INPUT class=button id=sButton2 type=submit
                                                        value=" 保存 " name=sButton2>
                                </td>
                            </tr>
                        </TABLE>
                    </TD>
                </TR>
            </TBODY>
        </TABLE>
    </FORM>

五、CustomerAction的save方法

public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
    public String find() {
    CustomerService customerService=new CustomerServiceImpl();
    List<Customer> list =customerService.find();
    //页面跳转,传值
    ServletActionContext.getRequest().setAttribute("list", list);
    //return NONE;
    return "findSuccess";
    }
    
    public String saveUI() {
    return "savaUI";
    }
    
    private Customer customer=new Customer();
    
    @Override
    public Customer getModel() {
        
        return customer;
    }
    public String save() {
    CustomerService customerService=new CustomerServiceImpl();
    customerService.save(customer);
    return "saveSuccess";
    }
}

六、编写Service

  @Override
    public void save(Customer customer) {
    CustomerDao customerDao = new CustomerDaoImpl();

    customerDao.save(customer);
    }

七、编写DAO

    @Override
    public void save(Customer customer) {
    Session session=HibernateUtils.getCurrentSession();
     Transaction tran= session.beginTransaction();
     session.save(customer);
     tran.commit();
    }

八、配置页面跳转

    <package name="crm" extends="struts-default" namespace="/">
        <global-allowed-methods>regex:.*</global-allowed-methods>
        <action name="customer_*" class="com.xxx.web.action.CustomerAction" method="{1}">
            <result name="findSuccess">/jsp/customer/list.jsp</result>
            <result name="savaUI">/jsp/customer/add.jsp</result>
            <result name="saveSuccess" type="redirectAction">customer_find.action</result>
        </action>
    </package>

 注意:customer_find.action和customer_save.action在同一package下的可以这样写

<result name="saveSuccess" type="redirectAction">customer_find.action</result>

不在同一package下的

            <result name="saveSuccess" type="redirectAction">
                <param name="namespace">/xxxx</param>
                <param name="actionName">xxx_xxx</param>
            </result>

 

posted @ 2020-11-27 09:28  一杯水M  阅读(112)  评论(0编辑  收藏  举报