我想部署一個簡單的彈簧應用程序。它需要一個用戶名(來自jsp),將該值插入數據庫,並在新的jsp頁面上顯示問候語hello!, [username]
。IllegalArgumentException在Spring應用程序中的dataSource
我的環境是:
- 操作系統:Windows XP專業
- Db的:MS訪問(這僅僅是在第一次嘗試的東西出來,打算轉移到MySQL)
- IDE:月食
- 服務器:Tomcat的6
我得到一個錯誤如下:
Feb 25, 2010 11:21:04 AM org.springframework.web.servlet.FrameworkServlet processRequest SEVERE: Could not complete request java.lang.IllegalArgumentException: dataSource is required
at org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSe(JdbcAccessor.java:130)
at org.springframework.jdbc.core.JdbcTemplate.<init>(JdbcTemplate.java:122)
at SpringClasses.Profile.setUsername(Profile.java:32) ………
ApplicationContext的文件是如下:
<?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="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"><ref bean="dataSource"/></property>
</bean>
<bean id="Profile" class="SpringClasses.Profile">
<property name="dataSource"><ref bean="dataSource"/></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" singleton="true" destroy-method="close">
<property name="driverClassName" value="sun.jdbc.odbc.JdbcOdbcDriver"/>
<property name="url" value="jdbc:odbc:usernamedb"/>
<property name="username" value="admin"/>
<property name="password" value=""/>
</bean>
</beans>
</code>
我已受到控制面板中設置的DSN(usernamedb) - >管理員工具 - > ODBC來源 - >用戶DSN.The問題似乎是與dataSource的URL。
從控制器類和豆類相關的Java代碼爲如下
從ProfileFormController.java類:
protected ModelAndView onSubmit(Object command)
{
Profile profile = (Profile) command;
String greeting = "Hello," + profile.getUsername() + "!";
profile.setUsername(profile.getUsername());
return new ModelAndView("greetingDisplay", "greeting", greeting);
}
從Profile.java類:
private String username;
private JdbcTemplate jt;
private DataSource dataSource;
public Profile() {
}
public Profile(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public String setUsername(String username) {
int rowsInserted;
setDataSource(dataSource);
jt = new JdbcTemplate(dataSource);
rowsInserted = jt.update("insert into username_db (username) values(?)",new Object[] {username});
//System.out.println("In Profile.getUsername, num. of rows inserted:"+rowsInserted);
return username;
}
public void setDataSource(DataSource dataSource) {
this.jt = new JdbcTemplate(dataSource);
}
在web.xml中配置Profile bean,如下所示:
在web.xmlpublic ProfileFormController() {
setCommandClass(Profile.class);
setCommandName("profile"); }
增加時,ContextLoaderServlet太:
在ProfileFormController,我設置的構造如下。
我錯過了什麼?非常感謝幫助。
謝謝
Neetu。
顯示如何配置控制器,以及從何處獲取配置文件命令 – 2010-02-25 10:51:12
編輯以提供來自web.xml的配置詳細信息 – Pratyusha 2010-02-25 11:07:48
儘管在問題中提及Access非常有用,但我不認爲該標記有用,因爲你的問題與數據存儲有關。 – 2010-02-25 19:30:32