Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere Should I put beans.xml content (Jdbc Connect with Spring-Eclipse Dynamic webapplication)
    text
    copied!<p>Hello I am new to Spring and I am developing a Dynamic web application with Eclipse Kepler and Spring mvc. I opened the project as dynamic web project. So I have web.xml and spring-servlet.xml configuration files. When I tried to connect mysql datadase with jdbc connector I saw that there are some new Spring configurations about the connection in a Beans.xml file.</p> <p>So I have 3 xml configuraton files: web.xml, spring-srvlet.xml and Beans.xml. My question is :Can I put content of bean.xml in web.xml. or spring servlet.xml? And If I do that whic changes should I do in my controller java class that I am calling Beans.xml file in that class.</p> <p>Eclipse Kepler, Spring 3.2.4 Apache Tomcat 7</p> <p>Any Help will de appreciated.</p> <p>web.xml contents</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;display-name&gt;DomainYonetim&lt;/display-name&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;servlet&gt; &lt;servlet-name&gt;spring&lt;/servlet-name&gt; &lt;servlet-class&gt; org.springframework.web.servlet.DispatcherServlet &lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;spring&lt;/servlet-name&gt; &lt;url-pattern&gt;*.html&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;/web-app&gt; </code></pre> <p>spring-servlet.xml contents</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;context:component-scan base-package="Ekle" /&gt; &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt; &lt;property name="url" value="jdbc:mysql://localhost/domain_yonetim"/&gt; &lt;property name="username" value="root"/&gt; &lt;property name="password" value=""/&gt; &lt;/bean&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt; &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /&gt; &lt;property name="prefix" value="/WEB-INF/Ekle/" /&gt; &lt;property name="suffix" value=".jsp" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>Beans.xml contents:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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 "&gt; &lt;!-- Initialization for data source --&gt; &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt; &lt;property name="url" value="jdbc:mysql://localhost/domain_yonetim"/&gt; &lt;property name="username" value="root"/&gt; &lt;property name="password" value=""/&gt; &lt;/bean&gt; &lt;!-- Definition for studentJDBCTemplate bean --&gt; &lt;bean id="domainJDBCTemplate" class="Ekle.domainJDBCTemplate"&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>And finally my Controller Class:</p> <pre><code> 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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload