سؤال

Why my output is not coming?

adder.java

package action;
public class adder {
    int num1,num2,result2;
    public String execute()
    {
        result2=num1+num2;
        return "success";
    }
    public int getNum1() {
        return num1;
    }
    public void setNum1(int num1) {
        this.num1 = num1;
    }
    public int getNum2() {
        return num2;
    }
    public void setNum2(int num2) {
        this.num2 = num2;
    }

}

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

index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<s:form action="adder">
<s:textfield name="num1" label="firstnumber"/>
<s:textfield name="num2" label="secondnumber"/>
<s:submit value="add"/>
</s:form>

MyJsp.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<b>result is
<s:property value="result2"/>
</b>
<br/> 
<jsp:include page="index.jsp"/>

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>
    <package name="default" extends="struts-default">
    <action name="adder" class="action.adder" >
    <result name="success">/MyJsp.jsp</result>

    </action>
    </package>
    </struts>

The output is result is, but the value of result is not being displayed.

هل كانت مفيدة؟

المحلول

You are missing the Getter for result2.

You should also declare the variables private.

You are also using the old FilterDispatcher: if you are using Struts2 > 2.1.3, remember to change it with the new filter, StrutsPrepareAndExecuteFilter.

نصائح أخرى

It's failing to bind to whatever port you're trying to use for the webserver. My guess is you forgot to shutdown an instance or it is hanging.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top