您好,我是 Spring 新手,我正在使用 Eclipse Kepler 和 Spring mvc 开发一个动态 Web 应用程序。我将该项目作为动态 Web 项目打开。所以我有 web.xml 和 spring-servlet.xml 配置文件。当我尝试将 mysql datadase 与 jdbc 连接器连接时,我看到 Beans.xml 文件中有一些关于连接的新 Spring 配置。
所以我有 3 个 xml 配置文件:web.xml、spring-srvlet.xml 和 Beans.xml。我的问题是:我可以将 bean.xml 的内容放在 web.xml 中吗?还是春天 servlet.xml?如果我这样做,我应该在我的控制器 java 类中进行哪些更改,我在该类中调用 Beans.xml 文件。
Eclipse 开普勒,Spring 3.2.4 Apache Tomcat 7
任何帮助将不胜感激。
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>DomainYonetim</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.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"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd">
<context:component-scan
base-package="Ekle" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/domain_yonetim"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/Ekle/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
<!-- Initialization for data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/domain_yonetim"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<!-- Definition for studentJDBCTemplate bean -->
<bean id="domainJDBCTemplate"
class="Ekle.domainJDBCTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
最后是我的控制器类:
package Ekle;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@Controller
public class DomainEkleController {
@ModelAttribute("Domain")
public Domain getDomain()
{
return new Domain();
}
@RequestMapping(value="/DomainEkle")
public ModelAndView domainEkle() {
String message = "Hello World, Spring 3.0!";
ModelAndView domain_ekle= new ModelAndView("DomainEkle", "message", message);
return domain_ekle;
}
@RequestMapping(value="/DomainEkle",method=RequestMethod.POST)
public ModelAndView domain_eklendi_fonksiyon(@ModelAttribute("Domain")Domain domain, ModelMap model)
{
ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
DomainJDBCTemplate domainJDBCTemplate=(DomainJDBCTemplate)context.getBean("studentJDBCTempate");
domainJDBCTemplate.listDomains();
model.addAttribute("domain", domain.getDomain_adi());
model.addAttribute("sunucu", domain.getSunucu_no());
model.addAttribute("tarih", domain.getTarih());
System.out.println(domain.getTarih()+"-"+domain.getDomain_adi());
String message="Domain Kaydi Yapilmistir!";
ModelAndView dm_eklendi=new ModelAndView("DomainEkle","message",message);
return dm_eklendi;
}
}