67,513
社区成员
发帖
与我相关
我的任务
分享
<?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>SpringStructIBatis</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/service/spring-service.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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
</servlet>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<bean id="configproperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:jdbc.properties" />
</bean>
</beans>
<?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>
<package name="comprehensive" namespace="/rpt/test" extends="base">
<action name="test" class="com.comprehensive.action.TestAction" method="toTestAction" >
<result>/jsp/test.jsp</result>
</action>
</package>
</struts>
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean name="testStudent" class="com.comprehensive.model.Student" />
<bean name="testAction" class="com.comprehensive.action.TestAction" >
<property name="student">
<ref bean="testStudent" />
</property>
</bean>
</beans>
package com.comprehensive.model;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private int num;
private String name;
private String address;
private char sex;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public void print(){
System.out.println("========");
}
}
package com.comprehensive.action;
import com.comprehensive.model.Student;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String toTestAction(){
System.out.println("=======ceshi=======");
//引入的时候会报错
student.print();
return SUCCESS;
}
}
public String toTestAction(){
System.out.println("=======ceshi=======");
//引入的时候会报错
student.print();
return SUCCESS;
}
这里本来你的student都没有关联对象,只是声明了而已!声明就直接掉调用了他的方法了!
你改成这样试试
public String toTestAction(){
System.out.println("=======ceshi=======");
student = new Student();
student.print();
return SUCCESS;
}
package com.comprehensive.action;
import javax.annotation.Resource;
import com.comprehensive.model.Student;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
private static final long serialVersionUID = 1L;
@Resource(name = "testStudent")
private Student student;
public String toTestAction(){
System.out.println("=======ceshi=======");
//引入的时候会报错
student.print();
return SUCCESS;
}
}
@Resource(name = "testStudent") 这个没起到作用。
<?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>
<constant name="struts.objectFactory" value="spring"/>
<package name="comprehensive" namespace="/rpt/test" extends="struts-default">
<action name="test" class="com.comprehensive.action.TestAction" method="toTestAction" >
<result>/jsp/test.jsp</result>
</action>
</package>
</struts>
[/quote]
class就不能在com.comprehensive.action.TestAction这么写了,要用spring的,把com.comprehensive.action.TestAction改成spring的bean名称
<?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>
<constant name="struts.objectFactory" value="spring"/>
<package name="comprehensive" namespace="/rpt/test" extends="struts-default">
<action name="test" class="com.comprehensive.action.TestAction" method="toTestAction" >
<result>/jsp/test.jsp</result>
</action>
</package>
</struts>