专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 2020年最新Javaweb项目视频教程

2020年最新Javaweb项目视频教程

更新时间:2020-03-24 09:43:13 来源:动力节点 浏览2161次


  Spring开发Web项目


  Web项目初始化SpringIoc容器


  先新建一个Web项目,然后再导入开发项目必须的jar包,需要的jar包有:spring-java的6个jar和spring-web.jar。


  导入jar包后我们要在项目的web.xml文件中配置spring-web.jar提供的监听器,该监听器可以在服务器启动时初始化Ioc容器。

  <listener>

  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  然后再用context-param告诉监听器Ioc容器的位置,在param-value里面通过classpath可指出要用哪些SpringIoc容器

  <context-param>

  <!--监听器的父类ContextLoader中有一个属性contextConfigLocation,该属性值保存着配置文件xml的位置-->

  <param-name>contextConfigLocation</param-name>

  <param-value>

  classpath:applicationContext.xml,

  classpath:applicationContext-*

  </param-value>

  </context-param>

  通过"*"可引入星号前面名称相同,但后面名称不同的所有SpringIoc容器。

  <param-value>

  classpath:applicationContext-*

  </param-value>

  <!--上面代码等同于下面的代码-->

  <param-value>

  classpath:applicationContext-Controller,

  classpath:applicationContext-Dao,

  classpath:applicationContext-Service

  </param-value>

  拆分Spring配置文件


  有两种拆分方法,一种是根据三层结构拆分,另一种是根据功能结构拆分。例如

  <param-value>

  			<!--Servlet文件-->

  classpath:applicationContext-Controller,

  <!--Service文件-->

  classpath:applicationContext-Service,

  <!--Dao文件-->

  classpath:applicationContext-Dao

  </param-value>

  就是根据三层结构,在一个xml配置文件中配置所有Servlet文件,在一个xml配置文件中配置所有Service文件,在一个xml配置文件中配置所有Dao文件。有时候还需要在一个xml配置文件中配置所有数据库文件。


  从SpringIoc容器中获取数据


  先建好各个层的文件,然后在SpringIoc容器中通过bean实例化每个层的对象。

  <!--在Dao层的xml配置文件定义如下-->

  <beanid="studentDao"class="dao.impl.StudentDaoImpl">

  <!--在Service层的xml配置文件定义如下-->

  <beanid="studentService"class="service.impl.StudentServiceImpl">

  <propertyname="studentDao"ref="studentDao"></property>

  </bean>

  <!--在Controller层的xml配置文件定义如下-->

  <beanid="studentServlet"class="servlet.QueryStudentByIdServlet">

  <propertyname="studentService"ref="studentService"></property>

  </bean>

  在web.xml配置文件中定义好运行Servlet文件所需的代码后,然后在Servlet文件通过重写init方法来获取bean,最后运行服务器即可。

  @WebServlet(name="QueryStudentByIdServlet")

  publicclassQueryStudentByIdServletextendsHttpServlet{

  IStudentServicestudentService;

  //通过springioc容器的set注入将studentService注入给Servlet

  publicvoidsetStudentService(IStudentServicestudentService){

  this.studentService=studentService;

  }

  //servlet初始化方法:在初始化时,获取SpringIoc容器中的bean对象

  @Override

  publicvoidinit()throwsServletException{

  //在Web项目中用此方法获取Spring上下文对象,需要spring-web.jar

  ApplicationContextcontext=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

  //在Servlet容器中,通过getBean获取Ioc容器中的Bean

  studentService=(IStudentService)context.getBean("studentService");

  }

  protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{

  }

  protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{

  Stringname=studentService.queryStudentById();

  request.setAttribute("name",name);

  request.getRequestDispatcher("result.jsp").forward(request,response);

  }

  }


2020年最新Javaweb项目视频教程


    以上就是动力节点Java培训机构小编介绍的“2020年最新Javaweb项目视频教程”的内容,希望对大家有帮助,如有疑问,请在线咨询,有专业老师随时为你服务。


提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>