1、让spring管理控制器Action,并利用依赖注入为控制器注入业务逻辑组件
业务逻辑代码
package service;
public class MyService {
public boolean validLogin(String username,String password){
//简单的验证逻辑:用户名为张三且密码为123456则登录成功
if("张三".equals(username)&&"123456".equals(password)){
return true;
}
return false;
}
}
控制器Action代码
package controller;
import com.opensymphony.xwork2.ActionSupport;
import service.MyService;
public class LoginAction extends ActionSupport{
private String username;
private String password;
private MyService myService;
//用于spring依赖注入
public void setMyService(MyService myService) {
this.myService = myService;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute(){
if(myService.validLogin(getUsername(), getPassword())){
return SUCCESS;
}
return ERROR;
}
}
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="myService" class="service.MyService"></bean>
<!-- 注意:控制器Action的scope要设置为prototype -->
<bean id="loginAction" class="controller.LoginAction" scope="prototype">
<property name="myService" ref="myService"></property>
</bean>
</beans>
struts.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.devMode" value="true"></constant>
<package name="default" extends="struts-default">
<!-- 使用spring容器管理的Action控制器:此处class为spring容器中bean的id -->
<action name="login" class="loginAction">
<result name="error">/error.jsp</result>
<result>/welcome.jsp</result>
</action>
</package>
</struts>
2、利用spring的自动装配方式,Action会自动重spring容器中获取所需的逻辑组件
业务逻辑和控制器Action代码一样,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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--这里只需要配置业务逻辑的bean-->
<bean id="myService" class="service.MyService"></bean>
</beans>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.devMode" value="true"></constant>
<package name="default" extends="struts-default">
<!-- 这里class属性不再是spring中bean的id,而是控制器的全类名-->
<action name="login" class="controller.LoginAction">
<result name="error">/error.jsp</result>
<result>/welcome.jsp</result>
</action>
</package>
</struts>
从控制台中可以看出struts2-spring-plugin-2.3.24.1.jar使用自动装配的方式是通过名字来自动装配的
通过设置struts.objectFactory.spring.autoWire常量改变spring插件的自动装配方式,该常量接受如下几个值:
- name:使用byName自动装配
- type:使用byType自动装配
- auto:spring插件自动检测需要哪种装配方式
- constructor:与type类似,区别是使用构造器来构造注入所需的参数,而不是使用setter方式注入
详细请参看《轻量级java ee 企业应用实战》李刚