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

spring整合struts2过程详解

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

spring整合struts2过程详解

这篇文章主要介绍了spring整合struts2过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

首先将以下jar包加入到lib文件夹中:

基础目录:

Person.java

package com.gong.spring.struts2.beans;

public class Person {
  
  private String username;
  
  public void setUsername(String username) {
    this.username = username;
  }
  
  public void hello(){
    System.out.println("My name is " + username);
  }
  
}

PersonService.java

package com.gong.spring.struts2.services;

public class PersonService {
  
  public void save(){
    System.out.println("PersonService's save....");
  }
  
}

PersonAction.java

package com.gong.spring.struts2.actions;

import com.gong.spring.struts2.services.PersonService;

public class PersonAction {
  
  private PersonService personService;
  
  public void setPersonService(PersonService personService) {
    this.personService = personService;
  }
  
  public String execute(){
    System.out.println("execute....");
    personService.save();
    return "success";
  }
  
}

基本流程如下:在PersonAction装配PersonService,在execute方法中打印相关信息并调用personService的save方法,最后返回"success"。在PersonService中的save方法输出一句话。

applicationContext.xml




  
      
  
  
  
  
  
  
      
  
  

在applicationContext中配置相关bean。

stuts.xml






  
  

  
    
    
    
      /success.jsp
    
    
  

在struts.xml中配置action时,class需要使用applicationContext.xml中bean的id。结果返回给success.jsp。

web.xml




  
  
    contextConfigLocation
    classpath:applicationContext.xml
  
  
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
  
  
    struts2
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  

  
    struts2
    /*
  

在web.xml中需要两个部分:一个是让springIOC容器在web应用服务加载时就进行创建。另一个就是配置struts2的过滤器。

index.jsp

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




Insert title here


  
  Person Save
  

sucess.jsp

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




Insert title here


  
  Success Page
  

test.jsp

<%@page import="com.gong.spring.struts2.beans.Person"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>




Insert title here


  
  <% 
    //1. 从 appication 域对象中得到 IOC 容器的实例
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);
    
    //2. 从 IOC 容器中得到 bean
    Person person = ctx.getBean(Person.class);
    
    //3. 使用 bean
    person.hello();
  %>
  

启动tomacat服务器之后:

点击Person Save:

会跳转到succes.jsp,并在控制台输出相应的语句。

说明spring整合struts2基本是成功的了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

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

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

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