在创建好WebProject后,就可以开始进行Struts2的环境配置,可以到Struts2官网下载,本环境使用struts-2.3.24.1版本。

  首先导入必要的jar包到WebProject的/WebRoot/WEB-INF/lib下,具体jar包如下图所示:

  

  接着修改web.xml文件,加入struts2的配置信息,文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>myStruts2</display-name>
<!-- struts2 configuration -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>cn.net.bysoft</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

web.xml的配置

  现在struts2的环境配置已经建立完毕,可以编写一个HelloWorld应用进行测试了,首先创建两个jsp页面,一个是index.jsp,里面写一个<a href="helloWorld">测试HelloWorld应用</a>连接到Action。接着创建一个hello.jsp页面用来显示<h1>Hello World</h1>,具体代码如下:

<%@ 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>
<a href="helloWorld">test helloworld</a>
</body>
</html>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'hello.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>
HelloWorld <br>
</body>
</html>

hello.jsp

  页面创建完毕后,开始编写Action类,创建一个普通的类,继承ActionSupport类。编写execute()方法进行页面控制,返回一个成功的标识SUCCESS,具体代码如下:

package cn.net.bysoft;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {

    /**
*
*/
private static final long serialVersionUID = 6649419922238488318L; @Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
} }

HelloWorldAction的代码

  最后在/src目录下创建一个struts.xml文件,对我们编写的Action进行配置,文件内容如下:

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="struts2_3_24_1" extends="struts-default">
<action name="helloWorld" class="cn.net.bysoft.HelloWorldAction">
<result>/hello.jsp</result>
</action>
</package>
</struts>

struts.xml的内容

  以下是项目结构:

最新文章

  1. Nginx安装、配置文档
  2. yourphp的sql语句
  3. iframe 内显示的网页 只显示改网页的某一部分!
  4. Android使用的设计模式2——策略模式
  5. 实现CodeFirst自动数据迁移无需命令
  6. oraclesql日志
  7. Floodlight之 FloodlightContextStore 数据结构
  8. vim的ex模式用法
  9. Yii2 独立操作
  10. 配置ubuntu的超管账号密码
  11. aspx 页面中 js 引用与页面后台的数据交互 --【 js 调后台】
  12. PAT-A1004. Counting Leaves (30)
  13. 剑指offer(27)字符串的排列
  14. Java中的访问权限细谈
  15. 线性回归,多项式回归(P2)
  16. 光流法(optical flow)
  17. 在Unity3d中调用外部程序及批处理文件
  18. 倒计时 总结 Timer Handler CountDownTimer RxJava MD
  19. Linux(Ubuntu16.04)下添加新用户
  20. fir.im 测试包下载工具--FIRReader 的图文介绍

热门文章

  1. [Locked] Group Shifted Strings
  2. [Locked] Verify Preorder Sequence in Binary Search Tree
  3. 简单的FIRST+集演示程序
  4. poj 3903 最长上升子序列 Stock Exchange
  5. javascript随机将第一个dom中的图片添加到第二个div中去
  6. php同时循环两个数组
  7. cgdb调试postgresql
  8. Thrift初用小结
  9. android图片缓存框架Android-Universal-Image-Loader(二)
  10. tomcat URL简写案例:模拟站点www.baidu.com的訪问