2014-01-24 2 views
1

Я получаю исключение null-указателя в struts. Я делаю только простой пример hello world. Когда я нажимаю, отображается эта ошибка. вот мой кодКак удалить исключение нулевого указателя в 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/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> 

Класс действий:

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> 

это шоу 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> 
+0

Возможный дубликат: https://stackoverflow.com/questions/12948849/whats-causing-this-npe-when-i-request-an-action –

+0

Я уже сделал это –

+0

Какая версия S2? Кроме того, 'messageStore' должен быть открыт публичным получателем. –

ответ

0

опечатка в конфигурации действия

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

следует заменить на

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

потому что стойки не могут создать действие из пакета, который не выходит.

Смежные вопросы