小弟初学struts2 看书看到struts2和spring整合部分..
按照书上.我都配置好了 贴出配置和代码web.xml------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2_13</display-name>
<!-- 与spring 整合 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 根据默认配置文件来初始化Spring容器 -->
<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>
<welcome-file-list>
<!-- 13.4.2 -->
<welcome-file>13.4/13.4.2/login.jsp</welcome-file> </welcome-file-list>
</web-app>
applicationContext.xml---
<?xml version="1.0" encoding="GBK"?>
<!-- 定义Spring配置文件的根元素,并指定Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myService" class="spring_13_4.spring_13_4_2.MyServiceImpl" />
<bean id="loginAction" class="spring_13_4.spring_13_4_2.LoginAction" scope="prototype">
<property name="msi" ref="myService"></property>
</bean>
</beans>struts.xml---
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 通过常量配置struts2国际化
<constant name="struts.custom.il8n.resources" value="globalMessages" /> -->
<!-- 通过常量配置struts2所使用的编码 -->
<constant name="strtus.i18n.encoding" value="GBK" />
<package name="13.4.2" extends="struts-default" namespace="/13.4.2">
<action name="login" class="spring_13_4.spring_13_4_2.LoginAction">
<result name="success">/13.4/13.4.2/success.jsp</result>
<result name="error">/13.4/13.4.2/error.jsp</result>
</action>
</package>
</struts>LoginAction.java 部分代码
private String name;
private String pass;
private String tip;
private IMyService msi;
public String execute() throws Exception {
if(msi.login(name, pass)){
setTip("登录成功!");
return SUCCESS;
}
setTip("登录失败!");
return ERROR;
}jar包
commons-logging-1.1.jar
freeer-2.3.8.jar
ognl-2.6.11.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-core-2.5.6.jar
spring-test-2.5.6.jar
spring-web-2.5.6.jar
struts2-core-2.0.6.jar
xwork-core-2.2.1.jar问题是 在LoginAction 中 mis 一直是null 是哪里配错了吗 还是jar包少了??大侠们帮看下
按照书上.我都配置好了 贴出配置和代码web.xml------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2_13</display-name>
<!-- 与spring 整合 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 根据默认配置文件来初始化Spring容器 -->
<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>
<welcome-file-list>
<!-- 13.4.2 -->
<welcome-file>13.4/13.4.2/login.jsp</welcome-file> </welcome-file-list>
</web-app>
applicationContext.xml---
<?xml version="1.0" encoding="GBK"?>
<!-- 定义Spring配置文件的根元素,并指定Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myService" class="spring_13_4.spring_13_4_2.MyServiceImpl" />
<bean id="loginAction" class="spring_13_4.spring_13_4_2.LoginAction" scope="prototype">
<property name="msi" ref="myService"></property>
</bean>
</beans>struts.xml---
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 通过常量配置struts2国际化
<constant name="struts.custom.il8n.resources" value="globalMessages" /> -->
<!-- 通过常量配置struts2所使用的编码 -->
<constant name="strtus.i18n.encoding" value="GBK" />
<package name="13.4.2" extends="struts-default" namespace="/13.4.2">
<action name="login" class="spring_13_4.spring_13_4_2.LoginAction">
<result name="success">/13.4/13.4.2/success.jsp</result>
<result name="error">/13.4/13.4.2/error.jsp</result>
</action>
</package>
</struts>LoginAction.java 部分代码
private String name;
private String pass;
private String tip;
private IMyService msi;
public String execute() throws Exception {
if(msi.login(name, pass)){
setTip("登录成功!");
return SUCCESS;
}
setTip("登录失败!");
return ERROR;
}jar包
commons-logging-1.1.jar
freeer-2.3.8.jar
ognl-2.6.11.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-core-2.5.6.jar
spring-test-2.5.6.jar
spring-web-2.5.6.jar
struts2-core-2.0.6.jar
xwork-core-2.2.1.jar问题是 在LoginAction 中 mis 一直是null 是哪里配错了吗 还是jar包少了??大侠们帮看下
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.1.jar
ognl-2.6.11.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-core-2.5.6.jar
spring-test-2.5.6.jar
spring-web-2.5.6.jar
struts2-core-2.2.1.jar
struts2-spring-plugin-2.2.1.jar
xwork-core-2.2.1.jar不过还是 msi 是null 的问题
execute() 方法 需要配置吗??不是默认的吗?
问题是 在LoginAction 中 mis 一直是null 是哪里配错了吗 还是jar包少了??大侠们帮看下你上面贴出来的配置方式,我很少用,不过你用转接的方式,不知道是不是没有设置getter和setter,你加上这个方法试试,应该可以,不行的话,你就直接用你的myService吧,因为你已经注入了,不需要在spring中配置action的转接了。有问题可以留言给我。
LoginAction 代码
public class LoginAction implements Action {
public void setMsi(IMyService msi) {
this.msi = msi;
}
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
private String name;
private String pass;
private String tip;
private IMyService msi;
public String execute() throws Exception {
if(msi.login(name, pass)){
setTip("登录成功!");
return SUCCESS;
}
setTip("登录失败!");
return ERROR;
}}