zoukankan      html  css  js  c++  java
  • struts2整合spring

    1、引入spring.jar、struts2-spring-plugin-2.1.8.jar、commons-logging.jar

    2、web.xml配置 

      <context-param>

    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>

    3、使用举例

     3.1、客户端

     <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <html>
    <head>
    <title>获取书籍</title>
    </head>
    <body>    
        <s:form action="getSpringBooks.action">        
            <s:textfield name="userName" label="用户名"/>
            <s:textfield name="passWord" label="密码"/>                
            <s:submit value="登录"/>        
        </s:form>    
    </body>
    </html> 

     3.2、struts.xml配置action

     <!-- 手动装配 

            <action name="getSpringBooks" class="springGetBooksAction">
            
                <result name="login">/Login.jsp</result>
                <result name="success">/books.jsp</result>
            </action>
            -->
            <!-- 自动装配 -->
            <action name="getSpringBooks" class="Action.GetBooksAction">
            
                <result name="login">/Login.jsp</result>
                <result name="success">/books.jsp</result>
            </action>


    3.3、action 

     View Code

     
    package Action;

    import module.myService;

    import com.opensymphony.xwork2.ActionSupport;


    public class GetBooksAction extends ActionSupport
    {
        /**
         * 
         
    */
        private static final long serialVersionUID = -1280978797644723018L;

        private String ttt;
        
        private String[] books;
        
        public void setBooks(final String[] books)
        {
            this.books=books;
        }
        public String[] getBooks()
        {
            return this.books;
        }
        
        myService bks;
        
        public myService getBks()
        {
            return bks;
        }
        public void setBks(myService bks)
        {
            this.bks = bks;
        }
        public String execute() throws Exception
        {    
            if(bks==null)
            {
                throw new Exception("myService 没有创建");
                //bks=new bookService();
            }
            
            setBooks(bks.getBooks());
            setTtt("getBooks.Action!");
            return SUCCESS;            
        }
        public void setTtt(final String ttt) {
            this.ttt = ttt;
        }
        public String getTtt() {
            return ttt;
        } 

    3.4、myService和bookService

     package module;


    public interface myService 
    {
        public String[] getBooks();    
    }



    package module;

    public class bookService implements myService
    {
        private String[] books=new String[]{
                "Spring2.0研究",
                "java精通",
                "jee ajax",
                "Struts,Spring,Hibernate深入研究"
                };
        
        public String[] getBooks()
        {
            return books;
        }    
    }

    3.5、 applicationContext.xml的配置

     <?xml version="1.0" encoding="UTF-8"?>

    <beans
        
    xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p
    ="http://www.springframework.org/schema/p"
        xsi:schemaLocation
    ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

        <!-- 手动装配
        <bean id="bookService" class="module.bookService"/>
        
        <bean id="springGetBooksAction" class="Action.GetBooksAction">
            <property name="bks" ref="bookService"/>
        </bean>
        
    -->
        
        <!-- 自动装配 -->
        <bean id="bks" class="module.bookService"/>
        
        
    </beans>
     
  • 相关阅读:
    Linux下gdb调试(tui)
    gdb tui中切换窗口
    gdb调试时的问题Missing separate debuginfos, use: debuginfo-install glibc-XXX
    进程间通信
    深入理解计算机系统结构——链接
    系统调用
    模块机制
    其他文件系统
    Oracle数据库逻辑迁移之数据泵的注意事项
    Oracle 10g DG 数据文件迁移
  • 原文地址:https://www.cnblogs.com/kuailewangzi1212/p/2389621.html
Copyright © 2011-2022 走看看