• 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>
  • 相关阅读:
    Android 如何自定义EditText 下划线?
    一步一步理解 Java 企业级应用的可扩展性
    客户案例—北京优络时代科技有限公司
    11个显著提升 ASP.NET 应用程序性能的技巧——第1部分
    如何用 React Native 创建一个iOS APP?(二)
    如何与 DevOps 为伍?
    通过 DevOps 整合开发和应用安全管道
    性能为王:选择模拟监控的10大理由!
    模拟监控和真实用户体验监测,选哪个?
    PHP 之 Laravel 框架安装及相关开源软件
  • 原文地址:https://www.cnblogs.com/WarBlog/p/14046227.html
Copyright © 2020-2023  润新知