问题
I am new to struts java application & i want to store database connections & global variables. What is best option to store values (Web.xml or struts-config.xml). and How to access the variables defined in that xml files. Is there any other way to store & access database connections. There should be way so that i can go & edit the files like variables and databases connections easily.
回答1:
Best way to store your database connection is
- Configure DataSource & ConnectionPool on application server.
- Make use of JNDI to obtain DataSource.
- Properties file for database connectivity.
- XML file database connectivity.
For reading property file you can make use of java.util.ResourceBundle
and any XML parser like X-Stream
for XML parsing.
Though if you want can make use of your web.xml to store database connection configuration parameters.
For global variable you can use simple a Constant class where you can define those variable and can use them throughout your application.
来源:https://stackoverflow.com/questions/9159831/where-to-store-database-connection-global-variables-in-struts-1-3