由于最近刚刚开始接触Spring+SpringMVC+mybatis的SSM框架来开发Web应用,页面使用的是Jsp。
所以经常会从Jsp页面调用到后端Java中Action类,因此希望总结一下,下次遇到类似问题不用再翻代码去查看:
1. 最常见的form表单提交:
一般情况下,比如在登陆界面,因为主要只涉及到登陆的功能,我们会使用form表单提交的方式来向后端Action传值以及跳转页面
举例:
前端Jsp页面中的表单提交方法:
<form action="<%=basePath%>login/isLogin" method="post">
<input type="text" name="username"/>
<input type="text" name="password"/>
<input class="continue" type="submit" value="登录">
</form>
后端Java中Action类的接收方法:
@RequestMapping("/login")
public class LoginAction {
@RequestMapping(value = "/isLogin", method = RequestMethod.POST)
public ModelAndView isLogin(HttpServletRequest request, HttpSession session, User user) {
if (user == null) {
logger.error("could not find user");
// return error page
}
ModelAndView mv = new ModelAndView();
User u = userService.getUser(user);
if (u == null) {
mv.addObject("msg", "用户名和密码不匹配 或 此用户不存在");
mv.setViewName("/index.jsp");
return mv;
}
mv.setViewName("/Successful.jsp");
session.setAttribute("userLogin", u);
return mv;
}
以上的案例的请求是POST请求,由于在后端Java类中存在user的VO类,其中也包括了name和password,所以能够直接获取到User类。
2. 通过<a href="#"></a>的方式调用后端Java中的Action类
如果页面中需要对某个对象进行比较详细的处理或者查询,我们也可以使用<a href="#"></a>的方式进行页面跳转和传值
举例:
前端Jsp页面的<a href="#"></a>方法:
<a href="<%=basePath%>inquiry/Detail/${uniq_code}" title="Detail">Detail</a>
后端Java中Action类的接收方法:
@RequestMapping("/inquiry")
public class TxnInqAction {
@RequestMapping(value = "Detail/{uniq_code}", method = RequestMethod.GET)
public ModelAndView detailList(HttpServletRequest request, HttpSession session,
@PathVariable("uniq_code") String uniqCode) {
ModelAndView mv = new ModelAndView();
if (uniqCode == null) {
mv.setViewName("pages/TxnInq_list.jsp");
}
………………
mv.setViewName("pages/TxnDetail.jsp");
return mv;
}
RequestMethod.GET)
public ModelAndView detailList(HttpServletRequest request, HttpSession session,
@PathVariable("uniq_code") String uniqCode) {
ModelAndView mv = new ModelAndView();
if (uniqCode == null) {
mv.setViewName("pages/TxnInq_list.jsp");
}
………………
mv.setViewName("pages/TxnDetail.jsp");
return mv;
}
}
通过<a href="#"></a>方法来调用后端Java Action类的请求是Get请求,一般性用的比较少因为Get请求可以直接从URL中看到传的真实参数
3. 通过JS的方法调用后端Java中的Action类
如果前端同一个页面有多个POST请求需要传参和跳转,可以使用该方法。
举例:
前端JS方法调用:
<a href="#" onClick="deleteAuth(${userId})" title="Delete">删除人员</a>
<script type="text/javascript">
function deleteAuth(authId)
{
$.confirm({
title: "请确认",
content: "是否删除此用户?",
buttons: {
确定: function () {
document.dpage.action="<%=basePath%>user/delete/"+authId;
document.dpage.submit() ;
},
取消: function () {
}
},
useBootstrap: false,
});
}
</script>
后端Java Action类中的接收方法:
@RequestMapping(value = "/delete/{userid}", method = RequestMethod.POST)
public ModelAndView deleteUser(HttpServletRequest request, HttpSession session,
@PathVariable("userid") String userId) {
ModelAndView mv = new ModelAndView();
userService.userSessionTransfer(session, mv, logger);
userService.deactivateUser(userId);
List<User> userList = userService.findAll();
mv.addObject(CharacterCfg.USER.USERLIST, userList);
mv.setViewName("pages/user_cfg/user_list.jsp");
return mv;
}
POST)
public ModelAndView deleteUser(HttpServletRequest request, HttpSession session,
@PathVariable("userid") String userId) {
ModelAndView mv = new ModelAndView();
userService.userSessionTransfer(session, mv, logger);
userService.deactivateUser(userId);
List<User> userList = userService.findAll();
mv.addObject(CharacterCfg.USER.USERLIST, userList);
mv.setViewName("pages/user_cfg/user_list.jsp");
return mv;
}
该方法与方法2的区别就只是在于一个是GET请求一个是POST请求。
下面这个是Ajax请求的写法:
function fullSyncSku(pid, shopId, deviceType){ var val = new Object(); val.shopId = shopId; val.pid = pid; val.deviceType = deviceType; $.ajax({ type: 'post', async: true, url: "${ctx}/shop/devicescale/fullSyncSku", data: val, }).done(function (json) { if (0 == json.code) { BootstrapDialog.show({ title: '操作状态', message: json.message, buttons: [{ label: '确定', action: function (dialogItself) { dialogItself.close(); } }] }); return; } }) }
目前只使用到了以上3种方式,如有新用方法会不断添加。