eclipse中创建struts2项目步骤

1.新建一个空白web项目

 创建时要勾选创建xml文件选项

 项目结构如下所示:

2.添加Structs2类库

Apache官方网站下载相应版本的Sruts2,网址:http://struts.apache.org/,下载好后解压。

此处使用一些老版本的包,如下图

struts-2.5.1版本所有jar包,示例,源码。

链接:https://pan.baidu.com/s/1SFArZuOAKC1fp-3xjW_C0Q
提取码:pn4i

以下包拷贝到WebContent文件下的lib文件夹内,如下图所示:

 

 3.配置web.xml文件,在web.xml中添加过滤器和默认配置文件,并设置为所有的请求(/*)都要通过这个过滤器,具体如下:

<?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_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>Structs2_app2</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>
  <filter>
     <filter-name>structs2</filter-name>
     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>structs2</filter-name>
      <url-pattern>*.action</url-pattern>
  </filter-mapping>
  
</web-app>
注意:按住ctrl键鼠标放置到filter-class节的类名上,出现下划线即可认为名称没有输错。

 4.编写Action

Action实际就是一个普通的java类,在MVC模式中分发器分发给不同的Action类来处理请求。Action类一般要继承ActionSupport类,因为其提供的大量的基本功能,如错误信息处理等。具体如下:

package com;
import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {
    private String message;
    
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String execute() {
        setMessage("我的第一个Struts2项目");
        return "success";
    }
}

5.配置struts.xml文件(原文链接:https://blog.csdn.net/lindan_40/article/details/19208991)

struts.xml要建在荐的src目录下,名为struts.xml,当项目发布以后,这个文件将会被复制到WEB-INF/classes下。新建方法:选择菜单New->File,接着在弹出对话框中的File name右侧输入框中主文件名struts.xml,点击Finish按钮关闭对话框。具体如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
 "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
        <package name="hellworld" extends="struts-default">    
        <action name="HelloWorld" class="com.HelloWorldAction">
            <result name="success">/HelloWorld.jsp</result>
        </action>
    </package>
</struts> 

6.创建HelloWorld.jsp页面

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HelloWorld</title>
</head>
<body>
<h2>
<s:property value="message"/>
</h2>
</body>
</html>

 7.将项目添加到tomcat中,在浏览器中输入测试网址运行,http://localhost:9091/struts_app/HelloWorld.jsp

 

 报错的信息(原文链接:https://blog.csdn.net/yugenning/article/details/3137339)

异常信息:The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag.
以上如果采用http://localhost:9091/struts_app/HelloWorld.action的方式访问,则正常。

  原因:如果想要在jsp文件中,采用 struts的tag,那么必须通过web.xml所配置的过滤器访问文件,否则会有异常,即 之前所出现的异常。

解决方案:

方案一:

采用 http://ip:port/SayHello.action 访问

方案二:

将web.xml 的过滤器,从 *.action 修改为: /*

方案三:

修改SayHello.jsp 文件,不使用 struts 的标签。
按方案一修改后,正常访问如下图所示:

 

posted @ 2023-03-27 09:29  YorkShare  阅读(413)  评论(0编辑  收藏  举报