在建立好WebProject后,就能够开始进行Struts2的环境配置,能够到Struts2官网下载,本环境使用struts-2.3.24.1版本。css
首先导入必要的jar包到WebProject的/WebRoot/WEB-INF/lib下,具体jar包以下图所示:html
接着修改web.xml文件,加入struts2的配置信息,文件内容以下:java
<?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>
如今struts2的环境配置已经创建完毕,能够编写一个HelloWorld应用进行测试了,首先建立两个jsp页面,一个是index.jsp,里面写一个<a href="helloWorld">测试HelloWorld应用</a>链接到Action。接着建立一个hello.jsp页面用来显示<h1>Hello World</h1>,具体代码以下:web
<%@ 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>
<%@ 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>
页面建立完毕后,开始编写Action类,建立一个普通的类,继承ActionSupport类。编写execute()方法进行页面控制,返回一个成功的标识SUCCESS,具体代码以下:apache
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; } }
最后在/src目录下建立一个struts.xml文件,对咱们编写的Action进行配置,文件内容以下:app
<!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>
如下是项目结构:jsp