zoukankan      html  css  js  c++  java
  • Spring学习之旅(二)极速创建Spring框架java Web工程项目

    编译工具:eclipse

    1)创建Web工程:spring_web_helloworld

    2)导入所需jar包:

    3)创建实体类:同上篇博文

    4)创建配置文件hellobean.xml。同上篇博文

    不过,注意,本项目中配置文件放的位置为WebContent目录下。

    5)实例化Spring容器:在web.xml中配置Spring容器。在启动Web工程时,自动创建实例化Spring容器。同时,在web.xml中指定Spring的配置文件。在启动Web工程时,自动关联到Spring容器中,并对Bean实施管理。

    在web.xml中的配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>spring_2_web_helloworld</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
      
      <!-- 以下为配置内容 -->
      <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>hellobean.xml</param-value>
      </context-param>
      
      <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
    </web-app>

    6)设计JSP页面,采用脚本代码,获取Spring容器,从容器内获取对象并显示。

    注意,导入相关类和获取Spring核心容器。

    在WebContent目录下新建show.jsp:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import="com.edu.HelloBeans,org.springframework.web.context.WebApplicationContext"%>
    <%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>spring应用示例</title>
    </head>
    <body>
    <%
    //首先通过request对象,获取Web服务器容器
    ServletContext sc=request.getServletContext();
    //利用spring框架提供的静态方法,从Web服务器中获取Spring容器
    WebApplicationContext wact=WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    //声明一个对象
    HelloBeans student;
    //从实例化的容器wact中,分别获取3个学生对象并显示信息
    student=(HelloBeans)wact.getBean("stu1");
    //利用JSP脚本获取数据并显示
    %>
    姓名:<%=student.getName() %><br>
    课程名:<%=student.getCourse() %><br>
    成绩:<%=student.getScore() %><br>
    <hr>
    
    <%
    student=(HelloBeans)wact.getBean("stu2");
    %>
    
    姓名:<%=student.getName() %><br>
    课程名:<%=student.getCourse() %><br>
    成绩:<%=student.getScore() %><br>
    <hr>
    
    <%
    student=(HelloBeans)wact.getBean("stu3");
    %>
    
    姓名:<%=student.getName() %><br>
    课程名:<%=student.getCourse() %><br>
    成绩:<%=student.getScore() %><br>
    <hr>
    </body>
    </html>

    7)部署运行。

    本篇参考书籍《Java EE框架开发技术与案例教程》

  • 相关阅读:
    架构-LAMP特级学习(网站加速解决方案)
    架构-LAMP特级学习(网站大存储量解决方案)
    架构-LAMP特级学习(网站大访问量解决方案)
    Shell-学习笔记
    算法-随手写的冒泡排序
    算法-随手写的二分查找
    cocos2dx 3.x ccPositionTextureColor_vert与ccPositionTextureColor_noMVP_vert
    cocos2dx当节点存在缩放时要注意的问题
    cocos2dx 3.x draw debug
    cocos2dx 3.x ccDrawLine一个坑
  • 原文地址:https://www.cnblogs.com/dudududu/p/8473293.html
Copyright © 2011-2022 走看看