Spring+struts+ejb(二)

三、ejb与spring struts集成

1、spring管理jndi的查找

<bean id="userManager" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName" value="userManager/local"></property>
	</bean>	

2、开发struts actionFor和action

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配置文件

<?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的创建

<bean name="/user_add" class="com.dcy.ejb.web.action.UserAction" scope="prototype">
		<property name="userManager" ref="userManager"></property>
	</bean>

5、配置web.xml配置文件

<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页面

<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>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值