运行
ApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
CustomerService svc = (CustomerService) ctx.getBean("customerService");获取时候报错,javax.servlet.ServletException: No bean named 'customerService' is defined附代码:
web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/WEB-INF/applicationContext-service.xml,classpath*:/WEB-INF/applicationContext-hibernate.xml,classpath*:/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener></web-app>applicationContext-service.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="customerService" parent="txProxyTemplate">
<property name="target">
<bean class="com.test.service.impl.CustomerServiceImpl">
<property name="CustomerDAO" ref="CustomerDAO" />
</bean>
</property>
</bean>
</beans>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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:test">
</property>
<property name="username" value="test"></property>
<property name="password" value="test"></property>
</bean>
</beans>CustomerServiceImplpackage com.test.service.impl;import com.test.dao.CustomerDAO;
import com.test.hibernate.entity.Customer;
import com.test.service.CustomerService;public class CustomerServiceImpl implements CustomerService { private CustomerDAO dao;
public void addUser(Customer user) {
// TODO Auto-generated method stub
dao.save(user);
} public boolean checkLogin(String loginId, String pass) {
// TODO Auto-generated method stub
Customer cus=dao.getCustomerByLoginId(loginId);
if(cus!=null && cus.getPassword().equals(pass))
return true;
return false;
} public Customer getCustomer(Integer id) {
// TODO Auto-generated method stub
return dao.getCustomerById(id);
}}
Action/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.test.struts.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;import com.test.service.CustomerService;/**
* MyEclipse Struts
* Creation date: 11-05-2009
*
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="/login.jsp" scope="request" validate="true"
*/
public class LoginAction extends DispatchAction {
/*
* Generated Methods
*/ /**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm loginForm = (DynaActionForm) form;// TODO Auto-generated method stub
String loginId = loginForm.getString("user");
String pass = loginForm.getString("password");
ApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
CustomerService svc = (CustomerService) ctx.getBean("customerService");
boolean login = svc.checkLogin(loginId, pass);
System.out.println(login==true?"true":"false");
return null;
}
}
ApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
CustomerService svc = (CustomerService) ctx.getBean("customerService");获取时候报错,javax.servlet.ServletException: No bean named 'customerService' is defined附代码:
web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/WEB-INF/applicationContext-service.xml,classpath*:/WEB-INF/applicationContext-hibernate.xml,classpath*:/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener></web-app>applicationContext-service.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="customerService" parent="txProxyTemplate">
<property name="target">
<bean class="com.test.service.impl.CustomerServiceImpl">
<property name="CustomerDAO" ref="CustomerDAO" />
</bean>
</property>
</bean>
</beans>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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:test">
</property>
<property name="username" value="test"></property>
<property name="password" value="test"></property>
</bean>
</beans>CustomerServiceImplpackage com.test.service.impl;import com.test.dao.CustomerDAO;
import com.test.hibernate.entity.Customer;
import com.test.service.CustomerService;public class CustomerServiceImpl implements CustomerService { private CustomerDAO dao;
public void addUser(Customer user) {
// TODO Auto-generated method stub
dao.save(user);
} public boolean checkLogin(String loginId, String pass) {
// TODO Auto-generated method stub
Customer cus=dao.getCustomerByLoginId(loginId);
if(cus!=null && cus.getPassword().equals(pass))
return true;
return false;
} public Customer getCustomer(Integer id) {
// TODO Auto-generated method stub
return dao.getCustomerById(id);
}}
Action/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.test.struts.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;import com.test.service.CustomerService;/**
* MyEclipse Struts
* Creation date: 11-05-2009
*
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="/login.jsp" scope="request" validate="true"
*/
public class LoginAction extends DispatchAction {
/*
* Generated Methods
*/ /**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm loginForm = (DynaActionForm) form;// TODO Auto-generated method stub
String loginId = loginForm.getString("user");
String pass = loginForm.getString("password");
ApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
CustomerService svc = (CustomerService) ctx.getBean("customerService");
boolean login = svc.checkLogin(loginId, pass);
System.out.println(login==true?"true":"false");
return null;
}
}
解决方案 »
- servlet-数据库-dao实现类之间的对象传递问题,在线等。谢谢指教
- 各位给个意见,(JSP)如何架构B/S系统(CRM系统)比较好(或者说比较流行)
- 两个java类A和B,它们互相引用,结果是两个类都无法编译,因为如果想编译通过其中一个就必须预先编译好另外一个,产生这个问题的根本原因
- jsp中如何实现定时提醒
- 怎样将前一个网页上的一段有连接的文字作为字符串传到后一个网页中??
- 一个简单的jsp问题
- 求救!!Form 递交后,中文字未变,但长度变长。
- web.xml配置问题,我要被折磨致死!!!,救命!!!!
- 这样一个奇怪的问题!!!! 急!在线等待!
- 【求救】java操作mysqldump备份mysql出空文件
- JSP日期计算
- 哪个免费报表工具支持的图形报表多啊
WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServlet().getServletContext.getServletContext());
2、Spring容器修改一下启动方式试一试<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>springInitServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
3、另外你的Spring配置文件怪怪的,为什么Service需要继承Transaction事务管理器
<bean id="customerService" parent="txProxyTemplate"> ???
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
我这配置文件是在classpath下,你上面写的classpath下的WEB-INF??
<property name="target">
<bean class="com.test.service.impl.CustomerServiceImpl">
<property name="CustomerDAO" ref="CustomerDAO" />
</bean>
</property>但是在impl中,却没见到有关于这个名字的属性的注入动作!而且依赖的属性也不是这个"CustomerDAO".所以,至少在这里,有些不懂楼主的写法了!不妨使用构造或是set方法注入一下,看看。