栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring-搭建web项目

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring-搭建web项目

spring-搭建web项目
  • 1.创建maven,web项目
  • 2.加入依赖
  • 3.拷贝ch07-spring-mybatis中的代码和配置文件
  • 4.创建一个jsp发起请求,有参数id,name,email,age。
  • 5.创建Servlet,接收请求参数,调用Service,调用dao完成注册
  • 6.创建一个jsp作为显示结果的页面

  • 1.之前做的是javase项目有main方法的,执行代码是执行main方法的,在main里面创建的容器对象。
    ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
  • 2.web项目是在tomcat服务器上运行的。tomcat一启动,项目是一致运行的。
  • 3.需求:web项目中容器对象只需要创建一次,把容器对象放入到全局作用域servletContext中。
    怎么实现:使用监听器,当全局作用域对象被创建时,创建容器,存入ServletContext.
    监听器作用:
    1)创建容器对象,执行
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    2)把容器对象放入到ServletContext,ServletContext.setAttribute(key,ctx)
    监听器可以自己创建,也可以 使用框架中提供好的ContextLoaderListener.
    在web项目中使用spring,完成学生注册功能
1.创建maven,web项目


2.加入依赖

拷贝ch07-spring-mybatis中依赖,并加入 jsp,servlet依赖




  4.0.0

  com.putao
  ch11-spring-web
  1.0-SNAPSHOT
  war

  
    UTF-8
    1.8
    1.8
  

  
    
    
      junit
      junit
      4.11
      test
    
    
    
      org.springframework
      spring-context
      5.2.5.RELEASE
    
    
    
      org.springframework
      spring-tx
      5.2.5.RELEASE
    
    
      org.springframework
      spring-jdbc
      5.2.5.RELEASE
    
    
    
      org.mybatis
      mybatis
      3.5.1
    
    
    
      org.mybatis
      mybatis-spring
      1.3.1
    
    
    
      mysql
      mysql-connector-java
      5.1.9
    
    
    
      com.alibaba
      druid
      1.1.12
    

    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    

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


    
      org.springframework
      spring-web
      5.2.5.RELEASE
    
  

  
    
    
      
        src/main/java
        
          ***.xml
        
        false
      
    
    
    
      
        maven-compiler-plugin
        3.8.1
        
          1.8
          1.8
        
      
    
  

3.拷贝ch07-spring-mybatis中的代码和配置文件
  • 1)实体类
package com.putao.domain;

public class Student {
    //属性名和列名一样
    private Integer id;
    private String name;
    private String email;
    private Integer age;

    public Student() {
    }

    public Student(Integer id, String name, String email, Integer age) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", email='" + email + ''' +
                ", age=" + age +
                '}';
    }
}

  • 2)dao类,dao.xml类
package com.putao.dao;

import com.putao.domain.Student;

import java.util.List;

public interface StudentDao {

    int insertStudent(Student student);
    List selectStudents();
}




    
        insert into student values(#{id},#{name},#{email},#{age})
    

    

  • 3)service类,service实现类
 package com.putao.service;

import com.putao.domain.Student;

import java.util.List;

public interface StudentService {

    int addStudent(Student student);
    List queryStudents();
}
package com.putao.service.impl;

import com.putao.dao.StudentDao;
import com.putao.domain.Student;
import com.putao.service.StudentService;

import java.util.List;

public class StudentServiceImpl implements StudentService {

    //引用类型
    private StudentDao studentDao;

    //使用set注入,赋值
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    @Override
    public int addStudent(Student student) {
        int nums = studentDao.insertStudent(student);
        return nums;
    }

    @Override
    public List queryStudents() {
        List students = studentDao.selectStudents();
        return students;
    }
}

  • 4)配置文件
    spring.xml




    

    


         
        
        
        
    

    

        
        
        
    

    
    

        
    
        
    

    
        
    

jdbc.properties

jdbc.url=jdbc:mysql://localhost:3306/springdb?characterEncoding=utf-8
jdbc.username=root
jdbc.passwd=root
jdbc.max=30

mybatis.xml






    

        
    
    
    
    
        
    

4.创建一个jsp发起请求,有参数id,name,email,age。

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


    

注册学生

id
姓名:
email:
年龄:
5.创建Servlet,接收请求参数,调用Service,调用dao完成注册
package com.putao.controller;

import com.putao.domain.Student;
import com.putao.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

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

        String strId = request.getParameter("id");
        String strName = request.getParameter("name");
        String strEmail = request.getParameter("email");
        String strAge = request.getParameter("age");

        //创建spring的容器对象
//        String config= "spring.xml";
//        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        //org.springframework.context.support.ClassPathXmlApplicationContext@e685c79, started on Tue Sep 28 17:34:38 GMT+08:00 2021
        //org.springframework.context.support.ClassPathXmlApplicationContext@56f7297a, started on Tue Sep 28 17:36:15 GMT+08:00 2021
//        System.out.println("容器对象的信息===="+ctx);
        WebApplicationContext ctx = null;
        //获取ServletContext中的容器对象,创建好的容器对象,拿来就用
//        String key = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
//        Object attr = getServletContext().getAttribute(key);
//        if (attr != null){
//            ctx = (WebApplicationContext) attr;
//        }

        //使用框架中的方法,获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        //容器对象的信息====Root WebApplicationContext, started on Wed Sep 29 09:43:09 GMT+08:00 2021
        //容器对象的信息====Root WebApplicationContext, started on Wed Sep 29 09:43:09 GMT+08:00 2021
        System.out.println("容器对象的信息===="+ctx);

        //获取service
        StudentService service = (StudentService) ctx.getBean("studentService");
        Student student = new Student();
        student.setId(Integer.parseInt(strId));
        student.setName(strName);
        student.setEmail(strEmail);
        student.setAge(Integer.valueOf(strAge));
        service.addStudent(student);

        //给一个页面
        request.getRequestDispatcher("/result.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

6.创建一个jsp作为显示结果的页面

result.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


result.jsp注册成功


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325299.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号