Spring详解(五)Spring和Web

Spring详解(五)Spring和Web,第1张

Spring详解(五)Spring和Web

文章目录
  • 第六章 Spring和Web
      • 实例:在网页上新建和查询学生信息
    • 1.使用容器对象的问题
    • 2.需要一个什么样的容器对象
    • 3.ContextLoaderListener
    • 4.ContextLoaderListener 源代码
      • 实例:修改查询只实例化对象一次
      • 实例:使用spring提供的方法进行简化

第六章 Spring和Web 实例:在网页上新建和查询学生信息

readme

ch18-spring-web:完成学生注册功能

步骤:
1.新建 maven
2.修改破灭.xml
    spring,mybatis 以外的依赖,servlet ,jsp依赖
3.创建实体类,Student,对应Student2表
4.创建dao接口 和mapper文件
5.创建mybatis主配置文件
6.创建Service和实现类
7.创建Servlet,接收请求的参数,调用service对象
8.创建jsp提交请求参数
9.创建jsp,作为视图,显示请求的处理结果
10.创建spring配置文件

添加的依赖

	
    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    
    
    
      javax.servlet.jsp
      jsp-api
      2.2.1-b03
      provided
    

applicationContext.xml






    


    
    
        
        
        
    


    

        
        
    

    

        

        
    

    
        
    

com.sunny.service.impl.StudentServiceImpl

public class StudentServiceImpl implements StudentService {
    private StudentDao dao;

    public void setDao(StudentDao dao) {
        this.dao = dao;
    }

    @Override
    public int addStudent(Student student) {

//        对象来自现实生活  逻辑可以写名字不能为空  如果有就不能添加
//        Student student1 = findStudentById(student.getStuid());
//        if(student ==null){
//            int rows = dao.insertStudent(student);
//
//        }
        int rows = dao.insertStudent(student);


        return rows;
    }

    @Override
    public Student findStudentById(Integer id) {
        Student student= dao.selectById(id);
        return student;
    }
}

com.sunny.controller.QueryStudentServlet

public class QueryStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String stuId = request.getParameter("stuid");

        String config = "applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        System.out.println("在servlet中创建的对象容器========"+ctx);
        StudentService service = (StudentService) ctx.getBean("studentService");

        Student student = service.findStudentById(Integer.valueOf(stuId));

        System.out.println("student对象====="+student);

        request.setAttribute("stu",student);
        request.getRequestDispatcher("/show.jsp").forward(request,response);
    }
}

com.sunny.dao.StudentDao

public interface StudentDao {
    int insertStudent(Student student);

    Student selectById(@Param("studentId") Integer id);
}

mybatis.xml




    



    

    
        
    
    


        
    

jsp

index.jsp


    添加学生


    
        

添加学生



查询学生

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4669171.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-07
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存