zoukankan      html  css  js  c++  java
  • spring整合javaweb(第二版)

    引入依赖

    <!--spring web-->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.10.RELEASE</version>
    </dependency>


    整个目录结构:


    entity层
    public class Book {
    private Integer bookid;
    private String bookname;
    private String bookprice;
    private String booktype;

    public Integer getBookid() {return bookid;}
    public void setBookid(Integer bookid) {
    this.bookid = bookid;
    }

    public String getBookname() {
    return bookname;
    }
    public void setBookname(String bookname) {
    this.bookname = bookname;
    }

    public String getBookprice() {
    return bookprice;
    }
    public void setBookprice(String bookprice) {
    this.bookprice = bookprice;
    }

    public String getBooktype() {
    return booktype;
    }
    public void setBooktype(String booktype) {
    this.booktype = booktype;
    }
    }

    dao层
    public interface IBookDAO {
    /**
    *
    * @param book
    * @return
    */
    public int addBook(Book book);
    }


    IBookDAO.xml
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="cn.happy.javaweb.dao.IBookDAO">

    <insert id="addBook" parameterType="Book">
    insert into book(bookname,bookprice,booktype) value(#{bookname},#{bookprice},#{booktype})
    </insert>

    </mapper>



    service层
    public interface IBookService {
    /**
    *
    * @param book
    * @return
    */
    public int addBook(Book book);
    }



    public class BookServiceImpl implements IBookService {
    //植入dao
    private IBookDAO dao;
    public int addBook(Book book) {
    return dao.addBook(book);
    }


    public IBookDAO getDao() {return dao;}
    public void setDao(IBookDAO dao) {
    this.dao = dao;
    }
    }



    jdbc.properties配置文件
    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql:///school
    jdbc.username=root
    jdbc.password=123156

    大配置文件(mybatis-config01.xml)
    
    
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration> <!--根节点 -->

    <!--配置类型别名-->
    <typeAliases>
    <!--将该类中的简单类型作为别名 -->
    <package name="cn.happy.javaweb.entity"></package>
    </typeAliases>

    </configuration>



    success.jsp页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>添加图书</title>
    </head>
    <body>
    <center>
    <form method="post" action="/BookServlet">
    图书名称:<input type="text" name="name"><br/>
    图书价格:<input type="text" name="price"><br/>
    图书类型:<input type="text" name="type"><br/>
    <input type="submit" value="提交">
    </form>
    </center>
    </body>
    </html>

    index.jsp页面
    <%@ page language="java"  pageEncoding="UTF-8" %>
    <html>
    <head>
    <title>添加图书</title>
    </head>
    <body>
    <h2>添加成功</h2>
    </body>
    </html>

    web.xml配置文件

    <!--上下文-->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContextJavaWed.xml</param-value>
    </context-param>
    <!--监听器 监听ServletContext i你提的方法执行过程-->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    servlet层

    public class BookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    String name= request.getParameter("name");
    String price= request.getParameter("price");
    String type= request.getParameter("type");
    Book book=new Book();
    book.setBookname(name);
    book.setBookprice(price);
    book.setBooktype(type);
    //调用添加的方法
    /* ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContextJavaWed.xml");*/

    WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

    System.out.println(context);
    IBookService service = (IBookService)context.getBean("studentService");
    int count = service.addBook(book);
    if (count>0){ //添加成功
    request.getRequestDispatcher("/index.jsp").forward(request,response);
    }else { //添加失败
    request.getRequestDispatcher("/success.jsp").forward(request,response);
    }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }
    }
    借助Tomcat来运行web应用
        ServletContext到底是什么?
        解析:在jsp中,他就是内置对象application的类型  ,在servlet中就是Servlet上下文    
     
          1.对于Web应用来说,ServletContext对象是唯一的,一个Web应用,只有一个ServletContext对象。
      该对象是在Web应用装载时初始化的,即在Web应用装载时自动执行接口ServletContext的初始化方法。
     
          2.该初始化方法在整个应用中只会执行一次。若将Spring容器的创建语句放到ServletContext的初始化方法中执行,
           并将创建好的Spring容器作为ServletContext的属性放入其中。以后再需要Spring容器,直接读取该属性值即可。
     
          3.ServletContext对象生命周期与Web应用的相同。即放在其中的属性为全局属性。
          所以,放入ServletContext中的Spring容器,在整个应用的生命周期中均可被访问。
          这样就可以保证Spring容器在Web应用中的唯一性了。
    
    
     
    
    
     


  • 相关阅读:
    Python元组、列表、字典
    测试通过Word直接发布博文
    Python环境搭建(windows)
    hdu 4003 Find Metal Mineral 树形DP
    poj 1986 Distance Queries LCA
    poj 1470 Closest Common Ancestors LCA
    poj 1330 Nearest Common Ancestors LCA
    hdu 3046 Pleasant sheep and big big wolf 最小割
    poj 3281 Dining 最大流
    zoj 2760 How Many Shortest Path 最大流
  • 原文地址:https://www.cnblogs.com/sujulin/p/7736840.html
Copyright © 2011-2022 走看看