• 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框架开发技术与案例教程》

  • 相关阅读:
    课堂作业02
    模仿JavaAppArguments.java示例,编写一个程序,此程序从命令行接收多个数字,求和之后输出结果。
    Feign使用Hystrix无效原因及解决方法
    解决Spring Boot 使用RedisTemplate 存储键值出现乱码 xacxedx00x05tx00
    consul怎么在windows下安装
    java运行jar命令提示没有主清单属性
    Maven parent.relativePath
    Maven的pom.xml文件结构之基本配置packaging和多模块聚合结构(微服务)
    redis开启远程访问
    kibana使用
  • 原文地址:https://www.cnblogs.com/dudududu/p/8473293.html
Copyright © 2020-2023  润新知