值得注意的是下面使用方式需要在配置好web.xml和springMvc.xml ,詳情可以見我其他的部落格
1.一般套路:
将資料綁定到session,一次會話都可以在前台得到的資料,rq.getSession().setAttribute("","");
1.在前台獲得背景的資料
1從控制層調方法一直經過業務層到資料通路層通路到資料庫後得到一個對象,再将這個對象使用**rq.getSession().setAttribute("","");**的形式綁定到前台,在前台使用${j.name}的形式進行擷取
2.在背景擷取前台的資料
1.使用name屬性值後可以在背景方法裡面直接用形參擷取,同樣可以使用方法的形參擷取
下面一堆name屬性
<input type="hidden" value="${ j.id }" name="id"/>,
<!--添加-->
<form action="system/jobs/update2">
<div class="form-group">
<label for="title">工作職位</label>
<input type="text" class="form-control" id="title" placeholder="工作職位" value="${ job1.title }" name="title">
<input type="hidden" value="${ job1.id }" name="id"/>
<input type="hidden" value="${ job1.htmlurl }" name="htmlurl"/>
</div>
<div class="form-group">
<label for="address">工作地點</label>
<select id="address" class="form-control" name="cid">
<c:forEach items="${ citys }" var="c" >
<option value="${ c.cid }">${ c.cname }</option>
</c:forEach>
</select>
</div>
<div class="form-group">
<label for="jobnum">招聘人數</label>
<input type="number" class="form-control" id="jobnum" placeholder="招聘人數" name="jobnum"/>
</div>
<div class="form-group">
<label for="treatment">薪資待遇</label>
<input type="text" class="form-control" id="treatment" placeholder="薪資待遇" name="treatment">
</div>
<div class="form-group">
<label for="describe">職位描述</label>
<div id="describe">
</div>
<input type="hidden" id="txtDescribe" name="describes" />
</div>
<div class="form-group">
<label for="require">任職要求</label>
<div id="require">
</div>
<input type="hidden" id="txtRequire" name="requires"/>
</div>
<div class="form-group">
<label for="positiontype">職位類型:</label>
<label class="radio-inline">
<input type="radio" name="positiontype" id="positiontype" value="1"
<c:if test="${ job1.positiontype==1 }">
checked = "checked"
</c:if>
>全職
</label>
<label class="radio-inline">
<input type="radio" name="positiontype" id="positiontype" value="0"
<c:if test="${ job1.positiontype==0 }">
checked = "checked"
</c:if>
>兼職
</label>
</div>
<div class="form-group">
<label for="isenabled">是否啟用:</label>
<label class="radio-inline">
<input type="radio" name="isenabled" id="isenabled" value="1" checked="checked"
<c:if test="${ job1.isenabled==true}">
checked = "checked"
</c:if>
>是
</label>
<label class="radio-inline">
<input type="radio" name="isenabled" id="isenabled" value="0"
<c:if test="${ job1.isenabled==false}">
checked = "checked"
</c:if>
>否
</label>
</div>
<div class="btn-toolbar" data-role="editor-toolbar" data-target="#editor">
<a class="btn btn-large" data-edit="bold"><i class="icon-bold"></i></a>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">修改職位</button>
</div>
</form>
這樣在背景就可以使用name的方式獲得前台的資料
2.使用超連結的形式進行傳值
下面傳了id= ,那麼在背景代碼塊中使用function(Integer id)即可得到傳過來的值
3.兩種擷取方式的前背景代碼對比
1.前台擷取背景代碼
背景,查詢之後将資料綁定
@RequestMapping("/update1")
public String update1(Integer id , Model mo){
//這裡把單個查詢設定成了update
Jobs j = service.update1(id);//調用的是單個查詢的功能,但寫成了update
mo.addAttribute("job1",j);
List<Citys> citys = cityService.findAll();
mo.addAttribute("citys",citys);
return "WEB-INF/system/jobs_edit";//跳轉到這個jsp頁面
}
前台,使用el表達式進行擷取,
<div class="form-group">
<label for="address">工作地點</label>
<select id="address" class="form-control" name="cid">
<c:forEach items="${ citys }" var="c" >
<option value="${ c.cid }">${ c.cname }</option>
</c:forEach>
</select>
</div>
前台将資料擷取後是有“暫時儲存”功能的,下面的${ job1.id }等等的資料是從背景擷取的,在後面又加上了name屬性值,留下這個name屬性值就是為了讓背景代碼直接獲得的
<div class="form-group">
<label for="title">工作職位</label>
<input type="text" class="form-control" id="title" placeholder="工作職位" value="${ job1.title }" name="title">
<input type="hidden" value="${ job1.id }" name="id"/>
<input type="hidden" value="${ job1.htmlurl }" name="htmlurl"/>
</div>
再回到背景代碼,上面已經将對象的兩個字段的name屬性值留出來了,下面可以直接在方法裡面傳對象進行獲得,即job對象裡面已經指派了兩個字段
@RequestMapping("/update2")
public String update2(Jobs job,HttpServletRequest rq){
String path = rq.getServletContext().getRealPath("/templates");
File file = new File(path,job.getHtmlurl());
if(file.exists()){
file.delete();
}
service.updateGo(job,path);
return "redirect:show";//跳轉到這個jsp頁面
}