java - struts中如何消除空指针异常?

标签 java jsp configuration struts2 action

我在 struts 中遇到空指针异常。我只做了 hello world 的简单例子。当我点击时显示此错误。 这是我的代码

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/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">
 <display-name>web</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

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

  <package name="basicstruts2" extends="struts-default">

  <action name="index">
    <result>/index.jsp</result>
  </action>

  <action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
    <result name="success">/HelloWorld.jsp</result>
  </action>

Action 类别:

package org.apache.struts.helloworld;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

    private MessageStore messageStore;

    public String execute() throws Exception {

        messageStore = new MessageStore() ;
        return SUCCESS;
    }

    public MessageStore getMessageStore() {
        return messageStore;
    }

    public void setMessageStore(MessageStore messageStore) {
        this.messageStore = messageStore;
    }

}

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=ISO-8859-1">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'/>">Hello World</a></p>
</body>

当我单击 hello 时,它显示 index.jsp 竞争,它显示错误。

enter image description here

HelloWorld.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=ISO-8859-1">
    <title>Hello World!</title>
    </head>
    <body>
        <h2><s:property value="messageStore.message" /></h2>
    </body>
    </html>

最佳答案

操作配置中的拼写错误

<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">

应替换为

<action name="hello" class="org.apache.struts.helloworld.HelloWorldAction" method="execute">

因为 struts 无法从不存在的包中构建操作。

关于java - struts中如何消除空指针异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21329847/

相关文章:

java - tomcat 8 jsp 如何包含/导入 jsp 页面

jsp - 登录时获取用户IP

java - 在 log4j2 中按类名过滤

amazon-web-services - 为 cloudwatchLogs 中的每个日志文件创建一个 logStream

emacs - 检测 Emacs 窗口已拆分

Java webapp添加版本到应用程序静态内容以进行缓存

java - 事务标记为仅回滚 : How do I find the cause

Java整数运算

java - 如何在不将文件保存到磁盘的情况下打开文件

java - 如何从主 jsp 页面获取图 block jsp 页面的 Map 值