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

HTML前台发送请求代码:

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

TakeTimeAction代码:

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

 

抽取的Action公共类BaseAction代码:

 1 public abstract class BaseAction<T> extends ActionSupport implements
 2         ModelDriven<T> {
 3     // 模型驱动
 4     protected T model;
 5     @Override
 6     public T getModel() {
 7         return model;
 8     }
 9     // 构造器 完成model实例化
10     public BaseAction() {
11         // 构造子类Action对象 ,获取继承父类型的泛型
12         // AreaAction extends BaseAction<Area>
13         // BaseAction<Area>
14         Type genericSuperclass = this.getClass().getGenericSuperclass();
15         // 获取类型第一个泛型参数
16         ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
17         Class<T> modelClass = (Class<T>) parameterizedType
18                 .getActualTypeArguments()[0];
19         try {
20             model = modelClass.newInstance();
21         } catch (InstantiationException | IllegalAccessException e) {
22             e.printStackTrace();
23             System.out.println("模型构造失败...");
24         }
25     }
26     // 接收分页查询参数
27     protected int page;
28     protected int rows;
29     public void setPage(int page) {
30         this.page = page;
31     }
32     public void setRows(int rows) {
33         this.rows = rows;
34     }
35     // 将分页查询结果数据,压入值栈的方法
36     protected void pushPageDataToValueStack(Page<T> pageData) {
37         Map<String, Object> result = new HashMap<String, Object>();
38         result.put("total", pageData.getTotalElements());
39         result.put("rows", pageData.getContent());
40     ActionContext.getContext().getValueStack().push(result);
41     }
42 }

 

收派时间接口TakeTimeService代码:

1 public interface TakeTimeService2 {
2     //查询所有收派时间
3     List<TakeTime> findAll();
4 }

收派接口实现类TakeTimeServiceImpl代码:

 1 @Service
 2 @Transactional
 3 public class TakeTimeServiceImpl2 implements TakeTimeService2 {
 4     @Autowired
 5     private TakeTimeRepository2 takeTimeRepository;
 6     @Override
 7     public List<TakeTime> findAll() {
 8         return takeTimeRepository.findAll();
 9     }
10 }

dao层TakeTimeRepository代码:

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

 

posted @ 2017-12-26 11:26  学亮编程手记  阅读(1059)  评论(0编辑  收藏  举报