<script type="text/javascript" src="/ddssm/javascipt/jquery-3.3.1.js"></script>
<script type="text/javascript" src="/ddssm/javascipt/jquery-3.3.1.min.js"></script>
最少要引入的js。
我这里是用的SpringMVC+Spring+Mybatis框架
还有几个需要的jar包:json-lib、commons-io、commons-fileupload这几个jar包是得有的,我的jar包都发给大家看。少了什么自己加。
可以直接先用用看看有没有问题,然后慢慢对照。。。
先解决一下中文乱码问题。web.xml
https://blog.csdn.net/niceLiuSir/article/details/78445630?locationNum=5&fps=1 我是在这里找到的
<!-- 统一字符编码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
jsp中ajax部分
$(function() {$.ajax({
type : "post",//这是get/post
url : "/ddssm/studentcontroller/open",//要去的地址
data : { //这是需要传出的值
stid : $("#stid").val(),
stname : $("#stname").val()
},
success : function(msg) {
var list = eval(msg);//放进list
var opi = "<option>--请选择--</option>";
var opn = "<option>--请选择--</option>";
$.each(list, function(index, entity) {//遍历list
opi += "<option>" + entity.stid + "</option>";
opn += "<option>" + entity.stname + "</option>";
});
$("#stid").html(opi); //放进需要的地方
$("#stname").html(opn);
}
});
})
然后是jsp下面body部分
<form action="/ddssm/studentcontroller/insertOne" method="post">
<table>
<tr>
<td>stid:<select id="stid"></select></td>
</tr>
<tr>
<td>stname:<select id="stname"></select></td>
</tr>
</table>
</form>
接着跳转Controller层部分
@Controller
@RequestMapping("/studentcontroller")
public class StudentController {
@Autowired
private LoginServices loginservices;
@RequestMapping("/open") //student类接收stid和stname的值
public void open(HttpServletResponse response,Student student) throws IOException {
List<Student> list = studentservices.selectAll();//这里是全查
jsonarray.json(response, list);//查出来了返回ajax
}
}
这里是我自己写的jsonarray
public class jsonarray {
public static void json(HttpServletResponse response,Object obj) throws IOException{
JSONArray json = JSONArray.fromObject(obj);
PrintWriter writer = response.getWriter();
writer.write(json.toString());
writer.close();
}
}