1、环境搭建
(1)环境配置
jdk1.6.0_45
apache-tomcat-6.0.37
struts 2.1.8
(2)所需jar包
struts2-core-2.1.8.jar
xwork-core-2.1.6.jar
ognl-2.7.3.jar
freemarker-2.3.15.jar
commons-logging-1.0.4.jar
commons-fileupload-1.2.1.jar
(3)struts.xml配置文件
(4)web.xml文件
2、Hello World走起
首先编写第一个struts2程序。
(1)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
在web.xml中配置了一个struts2的过滤器,同时在url-pattern中配置了过滤模式,表示对所有的HTTP请求进行过滤。
(2)struts.xml
<?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="yy" namespace="/test" extends="struts-default">
<action name="helloWorld" class="com.struts2.study.yy.HelloWorldAction" method="execute" >
<result name="success">/pages/helloWorld.jsp</result>
</action>
</package>
</struts>
此文件在src目录下。该文件是配置struts2的跳转行为的。其中package的作用类似于java中的package,方便管理action。
name属性:唯一,表示包的名称。
namespace属性:包的命名空间,作为访问路径的一部分。
extends属性:指定继承包,其中struts-default定义了struts的核心功能。
abstract属性:是否是抽象包,如果是,则不能包含action配置。
action标签各种属性说明如下:
name属性:action的名字,和package的namespace共同组成访问路径。
class属性:指定action对应的处理类。
method属性:指定action对应的处理方法。
(3)HelloWorldAction.java
package com.struts2.study.yy;
/**
* @author yy
* @time 2013-12-23 下午3:11:32
*/
public class HelloWorldAction {
public String info ;
public void setInfo(String info) {
this.info = info;
}
public String execute(){
info = "hello, welcome to the struts2 world!";
return "success";
}
}
注意:execute方法必须返回String,对应于页面跳转的标识。
(4)helloWorld.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
the content is : ${info}
</body>
</html>
注意:
此处的${info}中的info对应于setInfo方法中的Info,与定义的info属性没有关系。
(5)结果访问:http://localhost:8080/struts2study/test/helloWorld
显示内容如下:
the content is : hello, welcome to the struts2 world!