初步使用struts2的一个问题,纠结了半天,熟悉struts2的帮忙看看

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 Report

Struts has detected an unhandled exception:

Messages:  
File: org/apache/struts2/impl/StrutsActionProxy.java
Line number: 69

Stacktraces

java.lang.NullPointerException
    org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69)
    com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
    org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
    org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
    com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:500)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
    java.lang.Thread.run(Thread.java:662)
    



You are seeing this page because development mode is enabled. Development mode, or devMode, enables extra debugging behaviors and reports to assist developers. To disable this mode, set:

  struts.devMode=false
in your WEB-INF/classes/struts.properties file.


12个回答

struts2的 bug

在用注释的时候我们想当然的以为在action上面注释Namespace,在方法上面注释action的动作,在实际使用中发现根本不会拼接。

@Namespace("ref/WorkDeploy")

要改的话这样
action的注释改为@Namespace("/")

方法的注释改为@Namespace("/ref/WorkDeploy/add") 如果是add方法

可以在tomcat启动的时候看注释,如果是在action上面注释Namespace的话,action 的namespace读出来的都是空 [].

little_shieh
little_shieh 最近把原来的项目翻出来看,发现我之前的回复是错误的。 @Namespace("/ref/WorkDeploy") 最前面的/必须要,action上面的注释前面的/一定不能要
7 年多之前 回复

你这个用注解
外加这个


actionPackages

com.wrk.bi.action


貌似不用struts.xml了吧。

我这里里面有个例子,就是s2sh全注解的例子,就是没用struts2的配置文件struts.xml。

我那个例子是页面通过http发过来的请求能找到execute方法,但是找不到我自定义的方法。

还有,你把http请求过的路径发出来,看看对不对,。直接贴出来这个不好判断吧

private String SUCCESS;

告诉你既然你继承了ActionSupport,那么这个父类中就有这个属性了,而你在子类中又直接有定义了一遍,那么这个属性的值是什么呢?
Action需要返回,你这个属性是空的,不能转向,所以报空指针错误!
private String SUCCESS="success";
或者直接返回SUCCESS!

使用strtus2的注解配置的action必须有一个name = "success"的Result
例如:
...
@Results( { @Result(name = "success", location = "/index.jsp") })
...
public class XXXAction extends ActionSupport {
.....
}

@Namespace("ref/WorkDeploy")改为@Namespace("/ref/WorkDeploy") 试试!命名空间以"/"开头

[code="java"]
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
[/code]

[quote]
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
[/quote]
:idea: lz class不对


struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

控制器设置的不对吧,还有就是,你用的struts2是哪个版本?

唉 现在改版了 没有以前那么爽哦 不适应啊

共12条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问