后端存session前端取session

时间:2024-10-12 11:36:55

需求的问题:
数据库的内容:
在这里插入图片描述
前端页面:
在这里插入图片描述

前端操作:
点击修改时,把选中信息的“课程名称”与数据库内容相比较。
①如果此“课程名称”数据库中有数据,页面应变为:
在这里插入图片描述

②如果此“课程名称”数据库中没有数据,页面应变为:
在这里插入图片描述
想法:
在前端作比较
java代码:
①查询数据库的“课程信息”

// 课程名字下拉框
@RequiresPermissions("testcourselive:courseLive:edit")
@RequestMapping(value = "selectCourseName")
@ResponseBody
public List<String> selectCourseName() {
	// 测试是否成功访问
	System.out.println("----------------selectCourseName run-----------");
	List<String> courseName = courseService.selectCourseName();
	return courseName;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

②把选中信息的“课程名字”传到前端(因为之前一些功能实现,不想修改,就通过session来实现了)

@RequiresPermissions("testcourselive:courseLive:view")
@RequestMapping(value = "form")
public String form(CourseLive courseLive, Model model) {
	System.out.println("=============form run================");
	//创建session
	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	HttpSession session = request.getSession();
	//存session
	session.setAttribute("courseName", courseLive.getCourseName());
	System.out.println("sessionCourseName:" + session.getAttribute("courseName"));
	model.addAttribute("courseLive", courseLive);
	return "modules/testcourselive/courseLiveForm";
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

js代码:

<%
	//jsp中
	String sessionCourseName=(String)session.getAttribute("courseName");
%>
//获取下拉列表
$.ajax({
	url: "${ctx}/testcourselive/courseLive/selectCourseName",
	type: "post",
	dataType: "json",
	success: function(nameList) {
		var count = 0;
		for (var i in nameList) {
			if (nameList[i].courseName == courseName) {
				//alert("下拉列表有重复的名字");
				count++;
			}
		}
		//alert("下拉框是否有重复的,1有0无。"+count);
		if (count == 0 && courseName != "null") {
			//alert("false ok");
			$("#checkTest").prop("checked", false);
			var temp =
				"<input type='text' name='courseName' class='layui-input' value='${ }'>";
			$("#courseName011").children().remove();
			$("#courseName011").html(temp);
			layui.use('form', function() {
				var form = layui.form;
				form.render("checkbox");
			});
		} else {
			var temp = "<select name='courseName'>";
			for (var i in nameList) {
				if (nameList[i].courseName == '${ }') {
					temp += "<option selected>" + nameList[i].courseName +
						"</option>";
				} else {
					temp += "<option>" + nameList[i].courseName +
						"</option>";
				}
			};
			temp += "</select>";
			$("#courseName011").html(temp);
			layui.use('form', function() {
				var form = layui.form;
				form.render();
			});
		}
	},
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

(因为前端使用了layui所以会有一些layui的东西)
html代码:

<div class="layui-form-item">
	<label class="layui-form-label">现有课程:</label>
	<div class="layui-col-md3">
		<input type="checkbox" lay-skin="primary" checked lay-filter="checkTest" id="checkTest">
	</div>
</div>

<div class="layui-form-item">
	<label class="layui-form-label">课程名称:<span class="help-inline">
	<font color="red">*</font></span></label>
	<div class="layui-col-md2" id="courseName011">

	</div>
</div>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

收获:后端存session前端取session
①方法参数中没有HttpServletRequest request

public String form(CourseLive courseLive, Model model) {
	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	HttpSession session = request.getSession();
	session.setAttribute("courseName", courseLive.getCourseName());
	System.out.println("sessionCourseName:" + session.getAttribute("courseName"));
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

②方法参数中有HttpServletRequest request

public String list( HttpServletRequest request, HttpServletResponse response, Model model) {
	HttpSession session = request.getSession();
	session.setAttribute("courseName", courseLive.getCourseName());
}
  • 1
  • 2
  • 3
  • 4