Struts2开发实例-http status 500 - /index.jsp (line: 2, column: 42) File "/struts-tags" not found

开发环境:win 8

服务器:tomcat 7

开发工具:myeclipse 10

Struts2版本号:struts-2.5.10.1

JDK版本:JDK 1.6

1.工程建立以及web.xml配置

首先创建一个名为StrutsDemo的web工程,然后配置web.xml文件,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>StrutsDemo</display-name>
  
  	<filter>
    	<!--Filter名称-->
        <filter-name>struts2</filter-name>
        <!--Filter入口-->
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
    	<!--Filter名称-->
        <filter-name>struts2</filter-name>
        <!--拦截所有的url-->
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
注意:

在Struts2.5中filter-class的路径应该是:org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

Struts2.5之前的版本的路径为:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter


2. 倒入工程所需要的jar包:


注意:

1. myeclipse下直接导入lib下即可,eclipse中导入lib之后还需要在工程中构建路径,即右键工程名->build path->configure build path->添加外部jar->选择你导入jar包;

2. 在struts2.5版本中xwork-core-xxxx.jar这个包已经和struts2-core-xxxx.jar合在一起了


3. 编写struts.xml控制器文件,如下:

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<!--制定struts.xml文件的根元素-->
<struts>
	<!--制定web编码集,相当于调用HttpServletRequest的setCharacterEncoding方法-->
	<constant name="struts.i18n.encoding" value="UTF-8"/>
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<!--配置包,包名为default,该包继承了Struts2框架的默认包struts-default-->
    <package name="default" namespace="/" extends="struts-default">
        <!--定义名为hello的Action,该Action的处理类为com.action.TestAction,并映射到success.jsp-->
        <global-allowed-methods>add, update</global-allowed-methods>
        <action name="hello" class="com.action.TestAction">
        	<!--name默认值为"success"-->
            <result name="success">/success.jsp</result>
        </action>
    </package>
</struts>
注意:

<constant name="struts.enable.DynamicMethodInvocation" value="true" /> 这一句在Struts2.5的版本中必须加,是因为我们创建的是动态的web工程,在Struts2.5之前的版本会自动调用DynamicMethodInvocation类,但是在Struts2.5之后必须自己手动设置才可以。


4. 前段的index.jsp和success.jsp页面

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
	<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body>
    This is my JSP page. <br>
    <s:a action="hello">hello</s:a>
  </body>
</html>

注意:使用s标签时,必须导入struts2-core-2.5xx.jar包或者xwork-core-xxxx.jar+struts2-core2.5之前的版本


success.jsp:(只带body部分,其余部分与index.jsp相同故省略)

<body>
    This is my JSP page. <br>
    <s:property value="helo"/>
  </body>

5. 后台Struts程序TestAction

TestAction.java:

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	private String helo;

	public String getHelo() {
		return helo;
	}

	public void setHelo(String helo) {
		this.helo = helo;
	}
	
	public String execute() throws Exception {
		helo="hello,world";
		return SUCCESS;
	}
}

6. 允许结果图





注意:如果在程序运行中出现了如下错误:

http status 500 - /index.jsp (line: 2, column: 42) File "/struts-tags" not found

与上面这个错误相同的还会偶尔出现另一个500的错误可能如下:

http status 500-java.lang.xxxxNot found -index.jsp??(大致如此,具体的没有复制忘记了)


很有可能的是tomcat服务器的问题,我建议你重新安装服务器


如果出现如下问题:

java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addFilter

则在服务器配置文件context.xml中加上<Loader delegate="true"/>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
192.168.85.1 - - [26/Jun/2022:06:07:07 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 14 192.168.85.1 - - [26/Jun/2022:06:08:06 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 15 192.168.85.1 - - [26/Jun/2022:06:08:16 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 1227 192.168.85.1 - - [26/Jun/2022:06:10:15 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 79 192.168.85.1 - - [26/Jun/2022:06:13:25 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 404 752 192.168.85.1 - - [26/Jun/2022:06:16:42 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:16:57 -0400] "GET //struts2-showcase/hhh.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:18:55 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:19:02 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:19:09 -0400] "GET //struts2-showcase/hhh1.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:19:34 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 400 192.168.85.1 - - [26/Jun/2022:06:20:37 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 5 192.168.85.1 - - [26/Jun/2022:06:20:42 -0400] "GET //struts2-showcase/hhh1.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:20:46 -0400] "GET //struts2-showcase/hhh.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:20:51 -0400] "GET /struts2-showcase/hhh.jsp HTTP/1.1" 403 642
07-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值