【问题标题】:Passing value from java file to jsp file using struts使用struts将值从java文件传递到jsp文件
【发布时间】:2014-05-29 09:16:31
【问题描述】:

我正在尝试实现一个基本的 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>

view.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>

first.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>

myAction.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?提前感谢您的帮助。

【问题讨论】:

  • 如果您希望它在请求之间持续存在,请使用会话。
  • 您不必在任何地方使用 reqDispatcher,因为您正在使用 struts。 strut 有责任在指定的 jsp 上转发您的请求
  • @user3260147 在上面的代码中,reqDispatcher.forwardmapping.findForward 会成功吗?
  • 如果你可以在这里粘贴stcaktrace

标签: java jsp struts


【解决方案1】:

在执行方法之外声明字符串并保留它的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页面包含&lt;s:property value="s"/&gt;

【讨论】:

  • 您能否提供一些源代码来帮助提问者理解您的意思?
猜你喜欢
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 2014-12-29
  • 2014-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多