SSH框架整合实现Java三层架构实例(一)
生活随笔
收集整理的這篇文章主要介紹了
SSH框架整合实现Java三层架构实例(一)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
HTML前臺(tái)發(fā)送請(qǐng)求代碼:
<tr><td>選擇收派時(shí)間</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> {@Autowiredprivate TakeTimeService2 takeTimeService;@Action(value="taketime_findAll",results={@Result(name="success",type="json")})public String findAll(){//調(diào)用業(yè)務(wù)層,查詢所有收派時(shí)間List<TakeTime> taketime = takeTimeService.findAll();//壓入值棧返回ActionContext.getContext().getValueStack().push(taketime);return SUCCESS;} }抽取的Action公共類BaseAction代碼:
public abstract class BaseAction<T> extends ActionSupport implementsModelDriven<T> {// 模型驅(qū)動(dòng)protected T model;@Overridepublic T getModel() {return model;}// 構(gòu)造器 完成model實(shí)例化public BaseAction() {// 構(gòu)造子類Action對(duì)象 ,獲取繼承父類型的泛型// AreaAction extends BaseAction<Area>// BaseAction<Area>Type genericSuperclass = this.getClass().getGenericSuperclass();// 獲取類型第一個(gè)泛型參數(shù)ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;Class<T> modelClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];try {model = modelClass.newInstance();} catch (InstantiationException | IllegalAccessException e) {e.printStackTrace();System.out.println("模型構(gòu)造失敗...");}}// 接收分頁查詢參數(shù)protected int page;protected int rows;public void setPage(int page) {this.page = page;}public void setRows(int rows) {this.rows = rows;}// 將分頁查詢結(jié)果數(shù)據(jù),壓入值棧的方法protected void pushPageDataToValueStack(Page<T> pageData) {Map<String, Object> result = new HashMap<String, Object>();result.put("total", pageData.getTotalElements());result.put("rows", pageData.getContent());ActionContext.getContext().getValueStack().push(result);} }收派時(shí)間接口TakeTimeService代碼:
public interface TakeTimeService2 {//查詢所有收派時(shí)間List<TakeTime> findAll(); }收派接口實(shí)現(xiàn)類TakeTimeServiceImpl代碼:
@Service @Transactional public class TakeTimeServiceImpl2 implements TakeTimeService2 {@Autowiredprivate TakeTimeRepository2 takeTimeRepository;@Overridepublic List<TakeTime> findAll() {return takeTimeRepository.findAll();} }dao層TakeTimeRepository代碼:
public interface TakeTimeRepository2 extends JpaRepository<TakeTime, Integer> {}總結(jié)
以上是生活随笔為你收集整理的SSH框架整合实现Java三层架构实例(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring在web开发中的应用
- 下一篇: JavaSE、JavaEE与Spring