Struts完成用户新增操作

点击新增客户出现该页面并完成前后台交互

Struts完成用户新增操作_第1张图片

 

代码逻辑分析:

 Struts完成用户新增操作_第2张图片

 

 

jsp 页面部分代码

 1 <TABLE id=table_1 style="DISPLAY: none" cellSpacing=0
 2                                             cellPadding=2 width=155 align=center border=0>
 3                                             <TBODY>
 4                                                 <TR>
 5                                                     <TD class=menuSmall><A class=style2 href="${pageContext.request.contextPath}/CustomerAction_saveUI" 
 6                                                         target=main>- 新增客户A>TD>
 7                                                 TR>
 8                                                 <TR> 
 9                                                     <TD class=menuSmall><A class=style2 href="${pageContext.request.contextPath}/CustomerAction_list"
10                                                         target=main>- 客户列表A>TD>
11                                                 TR>
12                                                 
13                                             TBODY>
14                                         TABLE>

Struts.xml

1 <package name="crm" namespace="/" extends="struts-default" >
2                                                 
3         <action name="CustomerAction_*" class="com.huan.web.action.CustomerAction" method="{1}" >
4             <result name="list" >/jsp/customer/list.jspresult>
5             <result name="saveUI">/jsp/customer/add.jspresult>
6             
7             <result name="saveSuccess" type="redirect">CustomerAction_list.actionresult>
8         action>
9     package>

add.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>



添加客户 


css
    rel=stylesheet>





    
form1 action="${pageContext.request.contextPath }/CustomerAction_add" method=post>
border=0> height=20> border=0>
<IMG src="${pageContext.request.contextPath }/images/new_022.jpg" border=0>
class=manageHead>当前位置:客户管理 > 添加客户
客户名称: class=textbox id=sChannel2 style="WIDTH: 180px" maxLength=50 name="cust_name"> 客户级别 : class=textbox id=sChannel2 style="WIDTH: 180px" maxLength=50 name="cust_level">
信息来源 : class=textbox id=sChannel2 style="WIDTH: 180px" maxLength=50 name="cust_source"> 联系人: class=textbox id=sChannel2 style="WIDTH: 180px" maxLength=50 name="cust_linkman">
固定电话 : class=textbox id=sChannel2 style="WIDTH: 180px" maxLength=50 name="cust_phone"> 移动电话 : class=textbox id=sChannel2 style="WIDTH: 180px" maxLength=50 name="cust_mobile">
class=button id=sButton2 type=submit value=" 保存 " name=sButton2>
border=0> background="${pageContext.request.contextPath }/images/new_025.jpg" height=15> border=0>

Action 类

package com.huan.web.action;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.huan.domain.Customer;
import com.huan.service.CustomerService;
import com.huan.service.impl.CustomerServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


public class CustomerAction extends ActionSupport implements ModelDriven{
    
    private CustomerService cs = new CustomerServiceImpl();
    private Customer customer=new Customer();
    
    public String list() throws Exception {
        //1 接受参数
        String cust_name = ServletActionContext.getRequest().getParameter("cust_name");
        //2 创建离线查询对象
        DetachedCriteria dc =DetachedCriteria.forClass(Customer.class);
        //3 判断参数拼装条件
        //StringUtils.isNotBlank 静态方法 判断
        //当cust_name 不为null  不为 ""(空字符串) ,不为空格时 返回true
        if(StringUtils.isNotBlank(cust_name)){
            dc.add(Restrictions.like("cust_name", "%"+cust_name+"%"));
        }
        //4 调用Service将离线对象传递
        List list = cs.getAll(dc);
        //5 将返回的list放入request域.转发到list.jsp显示
            //引用ServletActionContext类的静态方法getRequest
            //getRequest返回HttpServletRequest对象
        ServletActionContext.getRequest().setAttribute("list", list);
        
        return "list";
    }
    public String saveUI(){
        
        return "saveUI";
    }
    
    public String add(){
        cs.save(customer);
        
        return "saveSuccess";
    }

    @Override
    public Customer getModel() {
        
        return customer;
    }

    
    
}

 

你可能感兴趣的:(Struts完成用户新增操作)