sql2005数据库
<bean id="Source" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName">
<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:sqlserver://localhost:1433;DatabaseName=zyc_book</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value></value></property>
</bean>
<bean id="TestDAO" class="zyc.TestDAO">
<property name="dataSource">
<ref local="Source"/>
</property>
</bean><bean id="testAction" class="zyc.TestAction">
<property name="commandClass">
<value>zyc.TestForm</value>
</property>
<property name="validator">
<ref local="testValidator" />
</property>
<property name="testDao">
<ref local="TestDAO"/>
</property>
<property name="formView">
<value>register</value>
</property>
<property name="successView">
<value>success</value>
</property>
</bean>package zyc;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;public class TestAction extends SimpleFormController {
private TestDAO testDao;
protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,Object command,BindException errors) throws Exception{
TestForm testForm=(TestForm)command;
testDao=this.getTestDao();
if(testDao==null)
System.out.print("\n\n\n\n\ntest null\n\n\n"); //这里注入成功
if(testDao.isExist(testForm.getUsername()))
return new ModelAndView(this.getSuccessView());
else
return new ModelAndView(this.getFormView());
}
public TestDAO getTestDao() {
return testDao;
}
public void setTestDao(TestDAO testDao) {
this.testDao = testDao;
}
}
package zyc;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;public class TestDAO {
private DataSource dataSource;public DataSource getDataSource() {
return dataSource;
}public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public boolean isExist(String username){
if(this.getDataSource()==null)
{ System.out.print("\n\n\nnull\n\n\n");//这里得到空,注入失败
return true;
}
JdbcTemplate jdbcTemplate=new JdbcTemplate(getDataSource());
int count=jdbcTemplate.queryForInt("select count(*) from book_user where user_name='"+username+"'");
if(count>0)
return false;
else
return true;
}
}testDAO 注入到testAction能注入成功
dataSource 注入到testDAO不成功。为啥?
<bean id="Source" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName">
<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:sqlserver://localhost:1433;DatabaseName=zyc_book</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value></value></property>
</bean>
<bean id="TestDAO" class="zyc.TestDAO">
<property name="dataSource">
<ref local="Source"/>
</property>
</bean><bean id="testAction" class="zyc.TestAction">
<property name="commandClass">
<value>zyc.TestForm</value>
</property>
<property name="validator">
<ref local="testValidator" />
</property>
<property name="testDao">
<ref local="TestDAO"/>
</property>
<property name="formView">
<value>register</value>
</property>
<property name="successView">
<value>success</value>
</property>
</bean>package zyc;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;public class TestAction extends SimpleFormController {
private TestDAO testDao;
protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,Object command,BindException errors) throws Exception{
TestForm testForm=(TestForm)command;
testDao=this.getTestDao();
if(testDao==null)
System.out.print("\n\n\n\n\ntest null\n\n\n"); //这里注入成功
if(testDao.isExist(testForm.getUsername()))
return new ModelAndView(this.getSuccessView());
else
return new ModelAndView(this.getFormView());
}
public TestDAO getTestDao() {
return testDao;
}
public void setTestDao(TestDAO testDao) {
this.testDao = testDao;
}
}
package zyc;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;public class TestDAO {
private DataSource dataSource;public DataSource getDataSource() {
return dataSource;
}public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public boolean isExist(String username){
if(this.getDataSource()==null)
{ System.out.print("\n\n\nnull\n\n\n");//这里得到空,注入失败
return true;
}
JdbcTemplate jdbcTemplate=new JdbcTemplate(getDataSource());
int count=jdbcTemplate.queryForInt("select count(*) from book_user where user_name='"+username+"'");
if(count>0)
return false;
else
return true;
}
}testDAO 注入到testAction能注入成功
dataSource 注入到testDAO不成功。为啥?
解决方案 »
- 伪删除问题
- ScheduledThreadPoolExecutor定时问题,求高手!在线等!!!!!!
- RMI修改java.Policy
- jsp中的request值为null Struts
- hibernate动态关联查询问题(急)
- Myeclipse+Hibernate时驱动JTDS问题和ODBC问题
- webwork(或struts2)的问题
- Ant+Junit自动测试时出错,答者有分
- SSH+DWE搭配效果怎么样?
- 急!!画WEB曲线图,图形显示,大家帮忙!
- Hibernate 使用 proxool 连接池 隔一段时间自动断开连接,郁闷死了,请求大家帮忙
- 算法题:直线上的3个点,求第二点的坐标。
<property name="dataSource">
<ref local="Source"/>
</property>
</bean> <bean id="Source" class="org.apache.commons.dbcp.BasicDataSource" >
异常说 dataSource is required