功能说明:
页面按钮点击显示可用,再次点击显示不可用,同时点击按钮修改数据库对应的字段,变为1或者0。并在JSP页面中显示。
界面显示:
JSP界面:id给一个数字作为区分,在ajax的success方法使用。value的值从数据库中取出做判断,如果等于零,使用中,否则使用不可。js点击事件传参数,传一个序列编号,一个id。
<c:forEach items="${templateDtoList}" var="template" varStatus="status">
<input id="tmpactive${status.index}" type="button" value="${template.ACTIVE eq '0'?'使用中':'使用不可'}" onclick="changeActive(${status.index},${template.TEMPLATEID})"/>
</c:forEach>
<script type="text/javascript">
function changeActive(index,tmpId) {
$.ajax({
type: "post",
async: true,
url: "/Template/cgactivestatus",
data: {
changetmpId: tmpId
},
dataType: "json",
success: function (data) {
if ($("#tmpactive" + index).val() == "使用中") {
$("#tmpactive" + index).val("使用不可")
$("#activeshow" + index).text(1)
} else {
$("#tmpactive" + index).val("使用中")
$("#activeshow" + index).text(0)
}
},
error: function (e) {
console.log(e)
}
})
}
Controller层:@Transactional(rollbackFor = Exception.class)方法只要出现Exception时就会回滚,如果不加的话,Exception只有属于RuntimeException时才会回滚。
@Controller
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/Template")
public class TemplateController {
@Autowired
private TemplateService templateService;
@Autowired
private MeetingShowService meetingShowService;
@Autowired
private ApplicationService applicationService;
@RequestMapping("/cgactivestatus")
@ResponseBody
public int cgActivestatus(@RequestParam String changetmpId) {
int num = templateService.changeActivestatus(changetmpId);
return num;
}
}
Services层:
@Service
public class TemplateService {
@Autowired
private TemplateDao templatedao;
public int changeActivestatus(String changeId) {
return templatedao.changeActivestatus(changeId);
}
}
Dao层:
@Component
public interface TemplateDao {
int changeActivestatus(String changeId);
}
Mapper.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.smt.societymeeting.dao.TemplateDao">
<resultMap id="BaseResultMap" type="com.smt.societymeeting.dto.TemplateDto">
<id column="TEMPLATEID" jdbcType="VARCHAR" property="TEMPLATEID" />
<result column="TEMPLATENAME" jdbcType="VARCHAR" property="TEMPLATENAME" />
<result column="TEMPLATETYPE" jdbcType="VARCHAR" property="TEMPLATETYPE" />
<result column="MEETINGID" jdbcType="VARCHAR" property="MEETINGID" />
<result column="TEMPLATEMESS" jdbcType="VARCHAR" property="TEMPLATEMESS" />
<result column="ACTIVE" jdbcType="VARCHAR" property="ACTIVE" />
<result column="FORMZUURL" jdbcType="VARCHAR" property="FORMZUURL" />
<result column="APPLICATIONTYPE" jdbcType="VARCHAR" property="applicationtype" />
<result column="S2" jdbcType="VARCHAR" property="s2" />
<result column="S3" jdbcType="VARCHAR" property="s3" />
<result column="S4" jdbcType="VARCHAR" property="s4" />
<result column="S5" jdbcType="VARCHAR" property="s5" />
<result column="S6" jdbcType="VARCHAR" property="s6" />
<result column="S7" jdbcType="VARCHAR" property="s7" />
<result column="S8" jdbcType="VARCHAR" property="s8" />
<result column="S9" jdbcType="VARCHAR" property="s9" />
</resultMap>
<update id="changeActivestatus" parameterType="java.lang.String" >
UPDATE template
SET ACTIVE = ( CASE ACTIVE WHEN '1' THEN '0' WHEN '0' THEN '1' END)
where TEMPLATEID= #{TEMPLATEID,jdbcType=VARCHAR}
</update>
</mapper>