RSS

Action注入方式实现Spring与Struts1集成

02 Jan

Project Structure:

clip_image001[1]

LoginAction.java

package com.xavier.user.actions;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

 

import com.xavier.user.forms.LoginActionForm;

import com.xavier.user.manager.UserManager;

 

public class LoginAction extends Action {

           

            private UserManager userManager;

           

            public ActionForward execute(ActionMapping mapping, ActionForm form,

                                    HttpServletRequest request, HttpServletResponse response) {

                        LoginActionForm laf = (LoginActionForm) form;

                        userManager.login(laf.getUsername(), laf.getPassword());

                        return mapping.findForward(“success”);

            }

 

            public void setUserManager(UserManager userManager) {

                        this.userManager = userManager;

            }

 

}

applicationContext-beans.xml

<?xml version=“1.0” encoding=“UTF-8”?>

<beans xmlns=http://www.springframework.org/schema/beans&#8221;

       xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;

       xsi:schemaLocation=http://www.springframework.org/schema/beans

            http://www.springframework.org/schema/beans/spring-beans.xsd&#8221;>

    <bean id=“userManager” class=“com.xavier.user.manager.impl.UserManagerAdd”/>

    <bean name=”/login” class=“com.xavier.user.actions.LoginAction” scope=“prototype”>

            <property name=“userManager” ref=“userManager”/>

    </bean>

</beans>

struts-config.xml

<?xml version=“1.0” encoding=“ISO-8859-1” ?>

<!DOCTYPE struts-config PUBLIC

          “-//Apache Software Foundation//DTD Struts Configuration 1.3//EN”

          http://struts.apache.org/dtds/struts-config_1_3.dtd&#8221;>

<struts-config>

            <form-beans>

                        <form-bean name=“loginForm” type=“com.xavier.user.forms.LoginActionForm”/>

            </form-beans>

            <action-mappings>

                        <action path=“/logininput” forward=“/login.jsp”/>

                        <action path=”/login” type=“org.springframework.web.struts.DelegatingActionProxy” name=“loginForm” scope=“request”>

                                    <forward name=“success” path=“/success.jsp”/>

                        </action>

            </action-mappings>

            <message-resources parameter=“MessageResources”/>

</struts-config>

Other codes, refer to the Jan 1, 2012 blog.

Advertisements
 
Leave a comment

Posted by on 01/02/2012 in Java Web Tips

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: