使用struts将值从java文件传递到jsp文件

时间:2014-04-15 06:56:55

标签: java jsp 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>

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

1 个答案:

答案 0 :(得分:0)

在execute方法之外声明字符串并保留其getter setter。然后在execute方法中将其初始化为您的值。现在在jsp中使用property标签来获取它的值

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"/>