日期:2014-05-16  浏览次数:20538 次

从数据库中获取国际化资源文件的key,然后通过Struts2的标签显示

首先在mySql数据库中建立表:

/*
SQLyog Community v8.71 
MySQL - 5.1.53-community 
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;

create table `Resource` (
	`id` int (10),
	`key` varchar (300),
	`remark` varchar (300)
); 
insert into `Resource` (`id`, `key`, `remark`) values('100','student.name','no remark');
insert into `Resource` (`id`, `key`, `remark`) values('101','student.address','no remark');
insert into `Resource` (`id`, `key`, `remark`) values('102','student.age','no remark');
insert into `Resource` (`id`, `key`, `remark`) values('103','student.sex','no remark');
insert into `Resource` (`id`, `key`, `remark`) values('104','student.phone','no remark');

?

?

?

然后,建立Connection连接,写一个连接类:

?

package pack.java.connection.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * 数据库连接类;
 * @author Administrator
 *
 */
public class BaseConnection {
	private final String USERNAME = "root";
	private final String PASSWORD = "mysql";
	private final String URL = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8";
	private final String DRIVER = "com.mysql.jdbc.Driver";
	
	/**
	 * 获取Connection连接;
	 * @return
	 */
	private Connection getConnection(){
		Connection connection = null;
		try {
			Class.forName(DRIVER);
			connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("驱动不存在!");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}
	
	/**
	 * 获取PreparedStatement;
	 * @return
	 */
	private PreparedStatement getPreparedStatement(String sql){
		Connection connection  = getConnection();
		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = connection.prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return preparedStatement;
	}
	
	public ResultSet getResultSet(String sql){
		PreparedStatement preparedStatement = getPreparedStatement(sql);
		ResultSet resultSet  = null;
		try {
			resultSet =  preparedStatement.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("查询出错!");
		}
		return resultSet;
	}
}

?

?

首先定义一个JavaBean ,ResourceVO类,用来映射数据库的字段.

之后,就是通过,调用BaseConnection中的一个getResultSet方法返回ResultSet拼成成一个List<ResourceVO>集合;

这个过程就不说了,篇幅太长了.

?

然后,在Action中就调用方法,得到一个List<ResourceVO>集合;并且提供getter,setter方法.

package pack.java.file.upload.action;

import java.util.List;
import pack.java.service.ResourceService;
import pack.java.service.ResourceServiceImpl;
import pack.java.vo.ResourceVO;
import com.opensymphony.xwork2.ActionSupport;
/**
 * Resource Action 
 * @author Administrator
 *
 */
public class ResourceAction extends ActionSupport{

	private static final long serialVersionUID = -4190531063573107186L;
	private ResourceService resourceService = new ResourceServiceImpl();
	
	private List<ResourceVO> resourceVOList;
	public List<ResourceVO> getResourceVOList() {
		return resourceVOList;
	}
	public void setResourceVOList(List<ResourceVO> resourceVOList) {
		this.resourceVOList = resourceVOList;
	}
	
	/**
	 * 显示所有的资源文件集合;
	 * @return
	 */
	public String displayResourceVO(){
		this.resourceVOList  = resourceService.getResourceVOAll();
		System.out.println("resourceVOList 集合大小:"+resourceVOList.size()); 
		return SUCCESS;
	}
	
}

?

?

?

定义一个displayResource.jsp页面用于显示.资源国际化.