• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

global db connection settings in struts2

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am setting postgresql db connection parameters like protocol, url, username in action class of struts2 framework.
How can I declare these connection params globally within the application.
Thanks in advance.
 
Ranch Hand
Posts: 136
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all you can create an interface say ConnectionAware which will be implemented by classes which needs the connection obect



The implementation class will look like this


Then create your own interceptor to which creates the actual connection with the parameters you have set globally and inject the connection into the required classes which implement ConnectionAware interface.


Finally after the creation of your interceptor class, you can include this interceptor in your interceptors tag inside your struts.xml


Now in the action tag you can include the interceptor-ref to point to the newly created interceptor.

Here I have used sun.jdbc.odbc.JdbcOdbcDriver as driverClass and jdbc: odbc:ExcelData as connectionURL
 
Rajani Panchumarthy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Thank you for an elaborate answer.
I am able to make the connection to the db.
However, when I try to submit the form, null values are being inserted into db, i.e., the form parameters are found to be null in the execute method of the action class.
struts.xml file is as follows:
<action name="add" class="AddUser">
<result name="error">/jsppages/addUser.jsp</result>
<result name="input">/jsppages/addUser.jsp</result>
<result>/jsppages/regSuccess.jsp</result>
<interceptor-ref name="resourceInterceptor">
<param name="driverClass">org.postgresql.Driver</param>
<param name="connectionURL">jdbc:postgresql:postgres</param>
<param name="username">postgres</param>
<param name="password">post123</param>
</interceptor-ref>
</action>

the form where the user details are entered invokes the above add action.In this action class, I am getting the user details using getter method and trying to insert into db.But the values that the getter method is returned as null.

Is this behaviour due to the interceptor that is being called as soon as the add.action is invoked??
Could you please let me know if there is anything missing in struts.xml file??
 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just put interceptor-ref before the result....conventions

The value which you populate inside the input method, will not be obtained by default in the execute method. For example if you have a ArrayList namely myList as the form/action attribute and you populate some values into myList in the input method and you want to access them in the execute method also, you will have to
1. After putting the values into myList, put myList object into session from the input method.
2. Get myList object back from session in the execute method and set it to the myList using its setter method.

There is also another option. You can make myList static. I am saying this just for study purpose (Warning: Never try to implement this on a multi-user web application).
 
Rajani Panchumarthy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you.It worked.
reply
    Bookmark Topic Watch Topic
  • New Topic