天天看点

Struts2中debug标签页面无法显示解决办法

今天,在写代码时,使用struts2中的debug标签输出一下信息,但是在使用过程中发现页面中无法显示debug标签自动创建的链接。

先说解决办法:  配置struts默认常量中的struts.devMode 值为 true 就可以解决 

下面说一下原因:

特意写了个例子:  页面运行是访问DebugAction 进入到execute方法 输出“DebugAction” 然后返回SUCCESS,跳转到show.jsp 在show.jsp页面写了<debug></debug>标签 代码如下:

这是action类(为了演示,只输出了一句话,然后跳转。)

package com.study.action;

import com.opensymphony.xwork2.ActionSupport;

public class DeBugAction extends ActionSupport{

	@Override
	public String execute() throws Exception {
		System.out.println("DebugAction");
		return SUCCESS;
	}
	
}
           

struts.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="Debug" namespace="/" extends="struts-default">
		<action name="DebugAction" class="com.study.action.DeBugAction" method="execute">
			<result name="success">/show.jsp</result>
		</action>
	</package>
</struts>
           

web.xml配置文件

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>13OGNL表达式</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>struts</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
           

show.jsp 页面:

<%@ page  pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<title>13ONGLdebug</title>
</head>
<body>
 <s:debug></s:debug>
</body>
</html>
           

运行结果:

Struts2中debug标签页面无法显示解决办法

可见Action中的execute方法已经被执行,但是页面没有链接。  

接下来我们看一下当前页面的源码和我们写的jsp源码对比:(eclipse自带浏览器右键查看源会弹出一个记事本)

Struts2中debug标签页面无法显示解决办法

这里我们发现<debug></debug>标签压根没有被编译 于是,我们看一下这个标签的tld  在jsp源码上按住ctrl,点击我们配置的taglib中的uri=“/struts-tags”  跳转到tld页面 

Struts2中debug标签页面无法显示解决办法

在这个页面我们直接ctrl + f 搜索debug 可以看到下面这个描述:

Struts2中debug标签页面无法显示解决办法

我们看到这句描述的意思是说输出debug信息 (只有在struts.devMode 被激活)也就是说,我们必须开启struts的debug模式才可以,下面我们在struts.xml配置一下:

<constant name="struts.devMode" value="true"></constant>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<constant name="struts.devMode" value="true"></constant>
	<package name="Debug" namespace="/" extends="struts-default">
		<action name="DebugAction" class="com.study.action.DeBugAction" method="execute">
			<result name="success">/show.jsp</result>
		</action>
	</package>
</struts>
           

然后重启服务器  访问一下试试:

Struts2中debug标签页面无法显示解决办法

然后我们点击一下看看内容:

Struts2中debug标签页面无法显示解决办法

问题解决。  欢迎交流

继续阅读