
Apache版本:apache-tomcat-6.0.37
Struts2:本次使用版本2.3.16.3.GA,下载地址http://struts.apache.org/download.cgi#struts23163
如上图,选择下载struts-2.3.16.3-all.zip并解压
Eclipse和Tomcat的安装不再细说了,注意看一下Struts2项目的配置:
1、创建动态web项目
在Eclipse中选择File-New-Dynamic Web Project创建动态项目:
按下图创建名称为Struts2HelooWorld的项目,在Target runtime中选择Apache Tomcat v6.0并按提示选择Tomcat的安装根目录,单击“Finish”完成项目创建
2、添加Struts2 库到项目
在解压的struts-2.3.16.3-all目录下的lib目录中复制需要的库,并在Struts2HelloWorld项目的WebContent/WEB-INF/lib上右键粘贴:
需要导入的库如下,每个库的说明请参加struts2的文档:
3、配置过滤器
在Struts2HelloWorld项目的WebContent/WEB-INF/web.xml中添加如下配置:
如下:
[html] view plaincopy
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4、创建action
按下图创建一个包:
在该包中增加一个HelloWorld类,实现xwork2的Action接口:
按如下内容编辑该类:
[html] view plaincopy
package com.mystruts.action
import com.opensymphony.xwork2.Action
/**
* @author david
*
*/
public class HelloWorld implements Action {
private String message
/**
* @return the message
*/
public String getMessage() {
return message
}
/* (non-Javadoc)
* @see com.opensymphony.xwork2.Action#execute()
*/
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
message = "Hello World!"
return SUCCESS
}
}
5、添加jsp页面
在添加jsp文件之前,首先我们修改一下eclipse的默认项目编码,否则中文会是乱码,修改方法如下:
选择eclipse的主菜单中的window-Preferences
JSP Files的编码选择UTF-8
然后添加HellowWorld.jsp文件:
按下文编辑HelloWorld.jsp文件:
[html] view plaincopy
<%@ page language="java" contentType="text/htmlcharset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8">
<title>欢迎界面</title>
</head>
<body>
<h2><s:property value="message"/></h2>
</body>
</html>
6、添加struts.xml配置文件:
文件内容如下:
[html] view plaincopy
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="HelloWorld" extends="struts-default">
<action name="HelloWorld" class="com.mystruts.action.HelloWorld">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
7、调试
<!-- 如果是单个的数据源可以不用key属性--><data-source key="mysql">
<set-property property="autoCommit" value="false"/>
<set-property property="description" value="Data Source Configuration"/>
<!-- 数据库的JDBC驱动程序地址 -->
<set-property property="driverClass" value="com.mysql.jdbc.Driver"/>
<!-- 最大数据库连接数 -->
<set-property property="maxCount" value="200"/>
<!-- 最小数据库连接数 -->
<set-property property="minCount" value="20"/>
<!-- 设置连接数据库的用户名称-->
<set-property property="user" value="root"/>
<!-- 设置连接数据库的密码-->
<set-property property="password" value="root"/>
<!-- 设置连接数据库的名称和地址-->
<set-property property="url" value="jdbc:mysql://localhost:3306/train"/>
</data-source>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)