先来阐述我的问题。我按照教程把Action作为spring的bean来管理。然后通过spring的IOC 获得service的IMPl没有问题
后来我发现如果不把Aciton交给spring管理 只要service的IMPl交给spring管理。struts的Action同样可以获得service的IMPL(我找了很多文章。大家都说获得的是空,但是我不知道为什么我可以获得)
我贴出我的部分代码
//@Component
//@Scope("prototype")
public class RegisterAction extends ActionSupport {
private UserModel user;
private UserRegister userRegister;
@Override
public String execute() throws Exception {
System.out.print(user.getUser_name() + user.getUser_password()
+ user.getUser_email());
if(userRegister.userRegister(user)){
return SUCCESS;
}else{
return ERROR;
}
}
public UserModel getUser() {
return user;
}
public void setUser(UserModel user) {
this.user = user;
}
public UserRegister getUserRegister() {
return userRegister;
}
//@Resource
public void setUserRegister(UserRegister userRegister) {
this.userRegister = userRegister;
}
}
这个是ACTION 注释掉的注解是我用来配饰SPRING的其中的UserRegister 已经被spring管理了<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
web。xml的内容
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<package name="default" extends="struts-default">
<action name="Register" class="registerAction">
<result name="success">interface/welcome.jsp</result>
<result name="error">interface/welcome.jsp</result>
</action>
</package>
struts。xml的内容我不是道是我配置隐藏了什么机制还是什么原因 我这个ACTION不交给spring管理就是可以获得UserRegister 这个实体类。请大侠们指教
后来我发现如果不把Aciton交给spring管理 只要service的IMPl交给spring管理。struts的Action同样可以获得service的IMPL(我找了很多文章。大家都说获得的是空,但是我不知道为什么我可以获得)
我贴出我的部分代码
//@Component
//@Scope("prototype")
public class RegisterAction extends ActionSupport {
private UserModel user;
private UserRegister userRegister;
@Override
public String execute() throws Exception {
System.out.print(user.getUser_name() + user.getUser_password()
+ user.getUser_email());
if(userRegister.userRegister(user)){
return SUCCESS;
}else{
return ERROR;
}
}
public UserModel getUser() {
return user;
}
public void setUser(UserModel user) {
this.user = user;
}
public UserRegister getUserRegister() {
return userRegister;
}
//@Resource
public void setUserRegister(UserRegister userRegister) {
this.userRegister = userRegister;
}
}
这个是ACTION 注释掉的注解是我用来配饰SPRING的其中的UserRegister 已经被spring管理了<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
web。xml的内容
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<package name="default" extends="struts-default">
<action name="Register" class="registerAction">
<result name="success">interface/welcome.jsp</result>
<result name="error">interface/welcome.jsp</result>
</action>
</package>
struts。xml的内容我不是道是我配置隐藏了什么机制还是什么原因 我这个ACTION不交给spring管理就是可以获得UserRegister 这个实体类。请大侠们指教
解决方案 »
- jQuery+json+struts2的ajax返回的问题
- freemarker如何引入css文件
- 小女子请教一个传值的问题
- spring2.5+strtus2+ibatis2.3下Action无法注入依赖:SqlMapExecutor找不到
- java的一个语法问题
- SSH整合问题 高手进 小弟都抓狂了
- 100分,求救,weblogic问题,急,急,急,急!!!
- 用mysql保存GBK中文時出錯
- 请问:哪里有在Jbuilder上作EJB开发地Step By Step教程。
- EJB问题,高分相送。
- 布署项目时报错,数据库连接不上,监听器问题,求帮助
- 为什么咱发的帖子没人给回复呢?是没有人会,还是太难?
<package name="default" extends="struts-default">
<action name="Register" class="xxx.xxx.xxx.RegisterAction">
<result name="success">interface/welcome.jsp</result>
<result name="error">interface/welcome.jsp</result>
</action>
</package>
struts。xml的内容更正一下这里的CLASS 应该是这个。。就是说是全路径名
贴下啊
Spring是一种对象管理容器,使用spring管理对象就可以使框架之间解耦,因为所有对象是通过IOC插入的,而不是自己new出来的
这东西一句两句说不清楚,自己去看Spring的文档把
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config />
<context:component-scan base-package="com.DanielsViki" /> <bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="username" value="[email protected]" />
<property name="password" value="zwjun_01" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
<prop key="mail.smtp.port">465</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.socketFactory.fallback">
false
</prop>
<prop key="mail.smtp.socketFactory.class">
javax.net.ssl.SSLSocketFactory
</prop>
</props>
</property>
</bean> <bean id="templateMessage"
class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="www.DanielsViki.com" />
<property name="subject" value="thank for your register" />
</bean>
</beans>
applicationContext-hibernate
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config />
<context:component-scan base-package="com.DanielsViki" /> <bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:dbcp.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${driverClass}</value>
</property>
<property name="url">
<value>${url}</value>
</property>
<property name="username">
<value>${username}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
</bean> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/DanielsViki/model/UserModel.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean> <bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut
expression="execution(* com.DanielsViki.service.impl.*.*(..))"
id="hibernateAOP" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="hibernateAOP" />
</aop:config>
</beans>
这个就是applicationContext
各位是这样吗?使用了struts2-spring-plugin-2.1.8.1.jar 后 action会自动交给spring处理?
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
应该就是这块起得作用吧。
struts 中的action是可以不交给spring管理的(即不需要@Component和@Scope("prototype"))。
这时spring-struts 的插件会自动转配上action依赖的那些@resource javabean。此时的action由strus来管理, 也不能使用spring的事务管理。