簡體   English   中英

使用struts將值從java文件傳遞到jsp文件

[英]Passing value from java file to jsp file using struts

我正在嘗試實現一個基本的 struts 應用程序,我試圖將字符串值從 java 文件傳遞​​到 jsp。 但是我在 jsp 頁面中得到了一個空值。 我已經為此工作了 2-3 天,但我還沒有弄清楚這個問題。 請幫幫我

web.xml

<!DOCTYPE web-app    PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

    <display-name>Hello World Struts Application</display-name>

    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>3</param-value>
        </init-param>
        <init-param>
             <param-name>detail</param-name>
             <param-value>3</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>


    <welcome-file-list>
        <welcome-file>view.jsp</welcome-file>
    </welcome-file-list>

</web-app>

struts-config.xml

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
    <action-mappings>
        <action path="/view" type="myAction" validate="false">
                <forward name="success" path="/first" />
        </action>
        <action path="/view"
                forward="/view.jsp"/>
        <action path="/first" type="myAction" validate="false">
            <forward name="success" path="/first.jsp" />
        </action>
    </action-mappings>
</struts-config>

視圖.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>

    <body>
        <form action="first.do">
            Enter name :
            <input type="text" name="name"/>
            <input type="submit" value="Enter"/>
        </form>

    </body>
</html>

第一個.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>

    <body>
        Welcome!!!!!!!!

        <%
            String s=(String)request.getAttribute("s");
            out.println("s="+s);
        %>
    </body>
</html>

我的動作.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class myAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {

        String s="Karthikeyan";

        request.setAttribute("s",s);

        RequestDispatcher reqDispatcher = getRequestDispatcher("view.jsp");
        reqDispatcher.forward(request,response);




        return (mapping.findForward("success"));
    }
}

如何檢查值是否從java文件傳遞到jsp? 預先感謝您的幫助。

在執行方法之外聲明字符串並保留其getter setter。 然后在執行方法中將其初始化為您的值。 現在在jsp中使用屬性標簽來獲取它的值

public class Test extends ActionSupport
{
    String s;

    public String getS() {
        return s;
    }

    public void setS(String s) {
        this.s = s;
    }

    public String execute()
    {
        s="MyName";
        return SUCCESS;
    }

}

我的jsp頁面包含<s:property value="s"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM