[Struts2學習筆記] -- 環境配置

  在建立好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>
web.xml的配置

  如今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>
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,具體代碼以下: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;
    }
    
}
HelloWorldAction的代碼

  最後在/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>
struts.xml的內容

  如下是項目結構:jsp

相關文章
相關標籤/搜索