是按最简单的例子在写的,但总是404.。

来源:-

慕标3341450

2018-09-15 16:44

本人小白,刚入struts2.之前用的2.5.17版,但是没有blank。war。。所以回装了2.3版本。但是实验最简单的action例子总是失败。代码如下: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>HelloWorld</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 配置核心拦截器 -->
<filter>
    <!-- Filter的名字 -->
    <filter-name>struts2</filter-name>
    <!-- Filter的实现类 struts2.5以前可能有所不同 -->
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>   
<filter-mapping>
    <filter-name>struts2</filter-name>
    <!-- 拦截所有的url -->
    <url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

struts.xml

<?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>
        <package name="default" namespace="/" extends="struts-default" strict-method-invocation="false">
        <!-- name action的名字,访问时使用helloworld.action访问,class:实现类 -->
        <action name="helloworld" class="cn.xhcoding.action.HelloWorldAction">
            <!-- 结果集,即action中SUCCESS返回的视图 -->
            <result>/result.jsp</result>
        </action>

        </package>
    </struts>


action类

package cn.xhcoding.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {
 @Override
    public String execute() throws Exception {
        System.out.println("正在执行的Action");
        // 返回视图 SUCCESS,这是框架定义
        return SUCCESS;
    }
}

result.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>Action Result</title>
</head>
<body>
<h1>恭喜成功配置好基本的struts2环境</h1>
<h2>Hello World, I am Successful</h2>
</body>
</html>

index.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>Action Result</title>
</head>
<body>
<h1>index</h1>

</body>
</html>

.可以显示

https://img.mukewang.com/5b9cc6630001fb6408360520.jpg

https://img1.mukewang.com/5b9cc6640001a13a08160453.jpg


写回答 关注

1回答

  • Lukaqi
    2019-01-06 12:21:08

    访问的网址有问题吧?

Struts2入门

本教程带你踏上Struts2学习之旅,对Struts2进行更深入讲解

95054 学习 · 462 问题

查看课程

相似问题