值得注意的是下面使用方式需要在配置好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)即可得到传过来的值
<a href="system/jobs/update1?id=${ j.id }" class="btn-default tableA">修改</a>
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页面
}