Jsp调用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;
    }
}


通过<a href="#"></a>方法来调用后端Java Action类的请求是Get请求,一般性用的比较少因为Get请求可以直接从URL中看到传的真实参数


3. 通过JS的方法调用后端Java中的Action类

如果前端同一个页面有多个POST请求需要传参和跳转,可以使用该方法。

举例:

前端JS方法调用:

<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;
    }
}


后端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;
    }


该方法与方法2的区别就只是在于一个是GET请求一个是POST请求。
--------------------- 
原文:https://blog.csdn.net/wellven_chen/article/details/80078444 

猜你喜欢

转载自blog.csdn.net/hold_on_/article/details/89394790