乐者为王

Do one thing, and do it well.

一步一步整合WebWork和Spring

把webwork-2.1.7.jar、spring-1.2.1.jar以及webwork2-srping.jar复制到WEB-INF/lib目录下。

添加以下内容到web.xml文件中:

1
2
3
4
5
6
7
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
    <listener-class>com.atlassian.xwork.ext.ResolverSetupServletContextListener</listener-class>
</listener>

applicationContext.xml中的内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<beans>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </bean>

    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation" value="/WEB-INF/sql-map-config.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="userManager" class="com.codemany.netlink.service.impl.UserManagerImpl" singleton="true">
        <property name="sqlMapClient" ref="sqlMapClient" />
    </bean>
</beans>

配置文件sql-map-config.xml中的内容如下:

1
2
3
<sqlMapConfig>
    <sqlMap resource="com/codemany/netlink/dao/impl/User.xml" />
</sqlMapConfig>

在xwork.xml中添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<package name="default" extends="webwork-default"
        externalReferenceResolver="com.atlassian.xwork.ext.SpringServletContextReferenceResolver">

    <interceptors>
        <interceptor name="reference-resolver"
                class="com.opensymphony.xwork.interceptor.ExternalReferencesInterceptor" />

        <interceptor-stack name="interceptors">
            <interceptor-ref name="params" />
            <interceptor-ref name="reference-resolver" />
        </interceptor-stack>
    </interceptors>

    <default-interceptor-ref name="default-interceptor" />

    <action name="login" class="com.codemany.netlink.action.LoginAction">
        <external-ref name="userManager">userManager</external-ref>
        <result name="success" type="dispatcher">/success.jsp</result>
        <result name="error" type="dispatcher">/error.jsp</result>
    </action>
</package>

在LoginAction.java中添加代码:

1
2
3
4
private UserManager userManager = null;

public void setUserManager(UserManager userManager) {
    this.userManager = userManager;

建立UserManager.java接口文件:

1
2
3
public interface UserManager {
    public User login(String username, String password) throws UserLoginException;
}

UserManagerImpl.java实现代码:

1
2
3
4
5
6
public class UserManagerImpl extends SqlMapClientDaoSupport implements UserManager {

    public User login(String username, String password) throws UserLoginException {
        // do something
    }
}

Comments