SSH系列教程之二:创建Struts2应用

蒲公英 提交于 周四, 08/17/2017 - 09:22

前提: 继续本教程之前假定你已经配置好以下环境。

  • JDK7 + Tomcat7 + Eclipse IDE for Java EE Developers(Juno 版)
  • Mysql 数据库(你可以下载个 XAMPP,简单方便)

准备工作:

  1. 下载最新版 Struts2(当前:Struts 2.3.12),解压备用。

 

第一步:创建动态 Web 工程 sampleApp  

第二步:添加 Struts2 相关 Jar 包

  1. asm-x.x.jar
  2. asm-commons-x.x.jar
  3. asm-tree-x.x.jar
  4. commons-fileupload-X.X.X.jar
  5. commons-io-X.X.X.jar
  6. commons-lang3-X.X.jar
  7. freemarker-X.X.X.jar
  8. javassist-X.X.X.jar
  9. ognl-X.X.X.jar
  10. struts2-core-X.X.X.X.jar
  11. xwork-core-X.X.X.jar

第三步:在 web.xml 内添加 Struts2 过滤器
以便 Struts2 能监测到所有请求。

<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>

第四步:在 src 下创建 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>
    <constant name="struts.devMode" value="true" />
    <package name="basicstruts2" extends="struts-default">
        <action name="index" class="com.sample.action.IndexAction">
            <result name="success">/index.jsp</result>
        </action>
    </package>
</struts>

以上配置告诉 Struts2 框架:当 URL 以 index.action 结尾时会执行 IndexAction 类,执行成功后重定向到 index.jsp。

五步:创建 index.jsp
输出所有雇员信息。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Insert title here</title>
</head>
<body>
  <table id="employees" border="1">
    <s:iterator value="indexModel.employeeList">
      <tr>
        <td><s:property value="name" /></td>
        <td><s:property value="age" /></td>
        <td><s:property value="email" /></td>
      </tr>
    </s:iterator>
  </table>
</body>
</html>

第六步:创建雇员对应的实体类 Employee

package com.sample.domain;

public class Employee {

    private String name;

    private int age;

    private String email;

    public Employee(String name, int age, String email) {
        this.name  = name;
        this.age   = age;
        this.email = email;
    }

    // get 与 set 方法......
}

第七步:创建用于装载表示层数据的 IndexModel

package com.sample.model;

import java.util.List;
import com.sample.domain.Employee;

public class IndexModel {
    // 雇员信息
    private List<Employee> employeeList;

    public List<Employee> getEmployeeList() {
        return employeeList;
    }

    public void setEmployeeList(List<Employee> employeeList) {
        this.employeeList= employeeList;
    }
}

第八步:创建 IndexAction

package com.sample.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.sample.domain.Employee;
import com.sample.model.IndexModel;

public class IndexAction extends ActionSupport {
    private static final long serialVersionUID = 1L;

    private IndexModel indexModel = new IndexModel();

    public String execute() throws Exception {
        // 模拟数据
        List<Employee> employeeList= new ArrayList<Employee>();
        employeeList.add(new Employee("XiaoLi", 12, "xiaoli@test.com"));
        employeeList.add(new Employee("XiaoZhang", 12, "xiaozhang@test.com"));
        employeeList.add(new Employee("XiaoLiu", 12, "xiaoliu@test.com"));

        indexModel.setEmployeeList(employeeList);

        return SUCCESS;
    }

    public IndexModel getIndexModel() {
        return indexModel;
    }
}

第九步:运行
http://localhost:8080/sampleApp/index.action  

第十步:改善
感觉出上面的 URL 不太方便吧,我们增加个 index.html 页面。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=index.action">
</head>

<body>
<p>Loading ...</p>
</body>
</html>

附件:  sampleApp-struts2.7z