sxt_1_struts2入门_hello

一、搭建第一个struts项目

  1.1新建一个web项目

  1.2导入所需jar包

  1.3web.xml中配置struts核心过滤器  

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>1_struts_hello</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置struts核心过滤器(过滤请求) -->
  <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>
</web-app>

  1.4写action处理类  

package edu.aeon.struts2.action;

import com.opensymphony.xwork2.ActionSupport;
/**
 * [说明]:action处理类
 * @author aeon
 *
 */
public class HelloAction extends ActionSupport {
    public String helloAc(){
        System.out.println("hello,struts2!");
        return SUCCESS;
    }
}

  1.5在struts.xml核心配置文件中配置请求映射(将一个请求映射到一个action处理类)  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <!-- struts基于包管理 -->
    <package name="hello" namespace="/" extends="struts-default">
        <action name="helloAction" class="edu.aeon.struts2.action.HelloAction">
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts>

  1.6编写显示页面hello.jsp  

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>hello,struts</title>
</head>
<body>
    hello,struts!!!
</body>
</html>

  1.7测试结果截图:

  

  

  

  

如有任何疑问可联系邮箱: 给我发邮件、或直接联系QQ:1584875179 || 点返回首页

原文地址:https://www.cnblogs.com/aeon/p/10230471.html