问题描述
web.xml文件:<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" 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_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class><init-param> <param-name>actionPackages</param-name> <param-value>com.wrk.bi.action</param-value> </init-param> </filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>*.action</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.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><constant name="struts.devMode" value="true" /><constant name="struts.mapper.composite" value="struts" /><constant name="struts.convention.default.parent.package" value="default-package" /><constant name="struts.i18n.encoding" value="UTF-8" /><constant name="struts.locale" value="zh_CN" /><constant name="struts.multipart.maxSize" value="100000000" /><package name="default-package" extends="struts-default"><global-results><result name="failure">/common/exception.jsp</result></global-results><global-exception-mappings><exception-mapping name="runtimeexception" exception="java.lang.RuntimeException" result="failure" /></global-exception-mappings></package></struts> Action类代码:package com.wrk.bi.action;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Namespace;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")@Namespace("ref/WorkDeploy")public class WorkDeployAction extends ActionSupport{private String SUCCESS;private String ERROR;@Action(value = "queryWork")public String queryWork(){System.out.println("2222222222");return SUCCESS;}public String getSUCCESS() {return SUCCESS;}public void setSUCCESS(String success) {SUCCESS = success;}public String getERROR() {return ERROR;}public void setERROR(String error) {ERROR = error;}} 报错:Struts Problem ReportStruts has detected an unhandled exception:Messages: File:org/apache/struts2/impl/StrutsActionProxy.javaLine number:69
解决方案
struts2的 bug在用注释的时候我们想当然的以为在action上面注释Namespace,在方法上面注释action的动作,在实际使用中发现根本不会拼接。@Namespace("ref/WorkDeploy") 要改的话这样action的注释改为@Namespace("/") 方法的注释改为@Namespace("/ref/WorkDeploy/add") 如果是add方法可以在tomcat启动的时候看注释,如果是在action上面注释Namespace的话,action 的namespace读出来的都是空 [].
解决方案二:
private String SUCCESS; private String ERROR; 不需要了吧 Struts2 内部定义了
解决方案三:
换个mvc框架吧spring3mvc就比较好上手
解决方案四:
唉 现在改版了 没有以前那么爽哦 不适应啊
解决方案五:
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter>控制器设置的不对吧,还有就是,你用的struts2是哪个版本?
解决方案六:
引用<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> lz class不对
解决方案七:
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter>
解决方案八:
@Namespace("ref/WorkDeploy")改为@Namespace("/ref/WorkDeploy") 试试!命名空间以"/"开头
解决方案九:
使用strtus2的注解配置的action必须有一个name = "success"的Result例如:...@Results( { @Result(name = "success", location = "/index.jsp") })...public class XXXAction extends ActionSupport {.....}
解决方案十:
private String SUCCESS; 告诉你既然你继承了ActionSupport,那么这个父类中就有这个属性了,而你在子类中又直接有定义了一遍,那么这个属性的值是什么呢?Action需要返回,你这个属性是空的,不能转向,所以报空指针错误!private String SUCCESS="success";或者直接返回SUCCESS!
解决方案十一:
还有,你把http请求过的路径发出来,看看对不对,。直接贴出来这个不好判断吧
解决方案十二:
你这个用注解外加这个<init-param> <param-name>actionPackages</param-name> <param-value>com.wrk.bi.action</param-value> </init-param> 貌似不用struts.xml了吧。我这里里面有个例子,就是s2sh全注解的例子,就是没用struts2的配置文件struts.xml。我那个例子是页面通过http发过来的请求能找到execute方法,但是找不到我自定义的方法。