SSH框架整合实现Java三层架构实例(一)

HTML前台发送请求代码:

<tr>
    <td>选择收派时间td>
    <td>
        <input type="text" name="takeTimeId" class="easyui-combobox" required="true"
            data-options="url:'../../taketime_findAll.action', 
            valueField:'id',textField:'name'" />
    td>
tr>

TakeTimeAction代码:

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class TakeTimeAction2 extends BaseAction<TakeTime> {
    @Autowired
    private TakeTimeService2 takeTimeService;
    @Action(value="taketime_findAll",results={@Result(name="success",type="json")})
    public String findAll(){
        //调用业务层,查询所有收派时间
        List taketime = takeTimeService.findAll();
        //压入值栈返回
        ActionContext.getContext().getValueStack().push(taketime);
        return SUCCESS;
    }
}

抽取的Action公共类BaseAction代码:

public abstract class BaseAction<T> extends ActionSupport implements
        ModelDriven<T> {
    // 模型驱动
    protected T model;
    @Override
    public T getModel() {
        return model;
    }
    // 构造器 完成model实例化
    public BaseAction() {
        // 构造子类Action对象 ,获取继承父类型的泛型
        // AreaAction extends BaseAction
        // BaseAction
        Type genericSuperclass = this.getClass().getGenericSuperclass();
        // 获取类型第一个泛型参数
        ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
        Class modelClass = (Class) parameterizedType
                .getActualTypeArguments()[0];
        try {
            model = modelClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
            System.out.println("模型构造失败...");
        }
    }
    // 接收分页查询参数
    protected int page;
    protected int rows;
    public void setPage(int page) {
        this.page = page;
    }
    public void setRows(int rows) {
        this.rows = rows;
    }
    // 将分页查询结果数据,压入值栈的方法
    protected void pushPageDataToValueStack(Page pageData) {
        Map result = new HashMap();
        result.put("total", pageData.getTotalElements());
        result.put("rows", pageData.getContent());
    ActionContext.getContext().getValueStack().push(result);
    }
}

收派时间接口TakeTimeService代码:

public interface TakeTimeService2 {
    //查询所有收派时间
    List findAll();
}

收派接口实现类TakeTimeServiceImpl代码:

@Service
@Transactional
public class TakeTimeServiceImpl2 implements TakeTimeService2 {
    @Autowired
    private TakeTimeRepository2 takeTimeRepository;
    @Override
    public List findAll() {
        return takeTimeRepository.findAll();
    }
}

dao层TakeTimeRepository代码:

public interface TakeTimeRepository2 extends JpaRepository<TakeTime, Integer> {}

你可能感兴趣的:(经验分享,后台编程,学亮说JAVA)