学习springMVC之前,控制层写法,结合spring容器

本文介绍如何在Spring MVC中使用Spring容器管理业务逻辑,通过实例展示了如何在`CheckAccountServlet`中注入`CheckAccountService`并调用其方法。

学习springMVC之前,控制层写法,结合spring容器

package comcontroller;
import com.pojo.Account;
import com.service.CheckAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;



@WebServlet(value = "/check",loadOnStartup = 2)
public class CheckAccountServlet extends HttpServlet {
   
    //声明业务层属性
    private CheckAccountService checkAccountService;
   //重写init方法
   

	 @Override
    public void init() throws ServletException {
        //获取Spring容器对象
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
       
        //获取业务层对象
        checkAccountService= (CheckAccountService) ac.getBean("checkAccountService");
    }





    //重写service方法
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求编码格式
            req.setCharacterEncoding("utf-8");
        //设置响应编码格式
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
         //获取请求数据中的方法名
        String methodName=req.getParameter("methodName");
        //根据请求调用对应的逻辑代码
        if("checkOutInfo".equals(methodName)){
            checkOutInfo(req,resp);
        }else if("checkMoneyInfo".equals(methodName)){
            checkMoneyInfo(req,resp);
        }else if("checInInfo".equals(methodName)){
            checInInfo(req,resp);
        }else if("transferInfo".equals(methodName)){
            transferInfo(req,resp);
        }else{
            System.out.println("没有对应的逻辑方法:"+methodName);
        }
    }





    //转账
    private void transferInfo(HttpServletRequest req, HttpServletResponse resp) {
    }





    //校验收款人信息
    private void checInInfo(HttpServletRequest req, HttpServletResponse resp) {
    }




    //校验金额
    private void checkMoneyInfo(HttpServletRequest req, HttpServletResponse resp) {
    }




    //声明方法:校验转账账户信息
    private void checkOutInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取请求信息
        String outId=req.getParameter("outId");
        String outPwd=req.getParameter("outPwd");
        //处理请求
        //调用业务层方法
        Account account = checkAccountService.checkOutAccountInfoService(outId, outPwd);
        //响应结果
        resp.getWriter().write(account!=null?"true":"false");
    }


}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一写代码就开心

你的打赏将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值