在业务控制方法中写入Request,Response等传统web参数

本文介绍了一种在SpringMVC框架下处理POST请求的传统方式,通过HttpServletRequest获取参数,并进行类型转换,然后将处理后的数据存储到session中,最后重定向到注册页面。虽然这种方式在某些场景下仍被使用,但作者并不推荐,因为它增加了业务控制方法与web参数的耦合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以在业务控制方法中书写传统web参数,这种方式我们不提倡,耦合了

@Controller
@RequestMapping(value="/user")
public class UserAction{
    @RequestMapping(value="/add",method=RequestMethod.POST)
    public void add(HttpServletRequest request,HttpServletResponse response) throws Exception{
        System.out.println("HelloAction::add()::POST");
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        double sal = Double.parseDouble(request.getParameter("sal"));
        System.out.println(id + ":" + name + ":" + sal);
        request.getSession().setAttribute("id",id);
        request.getSession().setAttribute("name",name);
        request.getSession().setAttribute("sal",sal);
        response.sendRedirect(request.getContextPath()+"/register.jsp");
    }    
}

 

转载于:https://www.cnblogs.com/loaderman/p/10063298.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值