首先在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页面用于显示.资源国际化.
<table width="80%" align="center" cellpadding="0" cellspacing="1" style="border:0px solid blue"> <tr> <th>编号</th> <th>资源名称</th> <th>备注</th> </tr> <s:iterator id="resource" status="stat" value="#request.resourceVOList"> <tr> <td><s:property value="#resource.id"></s:property></td> <td> <!-- 显示从数据库配置好的key --> <s:property value="%{getText(#resource.key)}"/> </td> <td><s:property value="#resource.remark"/></td> </tr> </s:iterator> </table>
配置struts.xml文件:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "//struts.apache.org/dtds/struts-2.0.dtd" > <struts> <package name="pack.java.file.upload.action" extends="struts-default"> <!-- resource Action --> <action name="resourceAction" class="pack.java.file.upload.action.ResourceAction"> <result>/displayResource.jsp</result> </action> </package> </struts>
配置web.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts2Upload</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
最后在新建一个struts.properties配置文件放入src的目录下:
加入:
struts.custom.i18n.resources=globalMessages
然后再加入一个globalMessages_en_US.properties文件.
配置:
student.name = Name
student.address = Address
student.age = Age
student.sex = Sex
student.phone = Phone
即可.
启动服务器:运行 http://localhost:8008/Struts2ResourceDemo/displayResource.jsp