三、ejb与spring struts集成

1、spring管理jndi的查找
[java] view plaincopy
<bean id="userManager" class="org.springframework.jndi.JndiObjectFactoryBean"> 
        <property name="jndiName" value="userManager/local"></property> 
    </bean>    
2、开发struts actionFor和action
[java] view plaincopy
public class UserForm extends ActionForm{ 
 
    private String username; 
    private String password;     
    //getter setter 方法 
 
public class UserAction extends Action { 
 
    private UserManagerLocal userManager; 
 
    public UserManagerLocal getUserManager() { 
        return userManager; 
    } 
 
    public void setUserManager(UserManagerLocal userManager) { 
        this.userManager = userManager; 
    } 
 
    @Override 
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) 
            throws Exception { 
         
        UserForm userForm = (UserForm)form; 
        User user = new User(); 
        user.setUsername(userForm.getUsername()); 
        user.setPassword(userForm.getPassword()); 
         
        userManager.addUser(user); 
         
        return mapping.findForward("add_success"); 
    }    
3、配置struts配置文件
[html] view plaincopy
<?xml version="1.0" encoding="ISO-8859-1" ?> 
 
<!DOCTYPE struts-config PUBLIC 
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" 
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> 
 
<struts-config> 
 
 
    <form-beans>     
        <form-bean 
            name="userForm" 
            type="com.dcy.ejb.web.forms.UserForm"> 
             
       </form-bean> 
    </form-beans> 
 
    <action-mappings> 
        <action 
            path="/user_add" 
            type="org.springframework.web.struts.DelegatingActionProxy" 
            name="userForm" 
            scope="request"> 
          
            <forward name="add_success" path="/success.jsp"></forward>    
         </action> 
    </action-mappings> 
 <message-resources parameter="MessageResources" /> 
</struts-config> 
4、spring管理action的创建
[html] view plaincopy
<bean name="/user_add" class="com.dcy.ejb.web.action.UserAction" scope="prototype"> 
        <property name="userManager" ref="userManager"></property> 
    </bean> 
5、配置web.xml配置文件
[html] view plaincopy
<servlet> 
    <servlet-name>action</servlet-name> 
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> 
    <init-param> 
      <param-name>config</param-name> 
      <param-value>/WEB-INF/struts-config.xml</param-value> 
    </init-param> 
    <init-param> 
      <param-name>debug</param-name> 
      <param-value>2</param-value> 
    </init-param> 
    <init-param> 
      <param-name>detail</param-name> 
      <param-value>2</param-value> 
    </init-param> 
    <load-on-startup>2</load-on-startup> 
  </servlet> 
 
 
  <!-- Standard Action Servlet Mapping --> 
  <servlet-mapping> 
    <servlet-name>action</servlet-name> 
    <url-pattern>*.do</url-pattern> 
  </servlet-mapping> 
   
  <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>/WEB-INF/applicationContext*.xml,classpath*:applicationContext-*.xml</param-value> 
    </context-param> 
    <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
   
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
6、jsp页面
[html] view plaincopy
<body> 
    This is my JSP page. <br> 
    <form action="user_add.do" method="post"> 
        <input type="text" name="username"/> 
        <input type="text" name="password"/> 
        <input type="submit" value="ti jiao"/> 
    </form> 
  </body>