Jsp调⽤Action的⼏种⽅法
由于最近刚刚开始接触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) {
<("could not find user");
// return error page
jsp用什么前端框架}
ModelAndView mv = new ModelAndView();
User u = User(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 == de) {
BootstrapDialog.show({
title: '操作状态',
message: ssage,
buttons: [{
label: '确定',
action: function (dialogItself) {
dialogItself.close();
}
}]
});
return;
}
})
}
⽬前只使⽤到了以上3种⽅式,如有新⽤⽅法会不断添加。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。