【发布时间】:2014-04-14 12:21:21
【问题描述】:
我正在研究 struts,我正在尝试将字符串值从 java 文件传递到 jsp 页面。但我收到一个空值。请帮忙。
我的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("first.jsp");
reqDispatcher.forward(request,response);
return (mapping.findForward("success"));
}
}
我的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>
我的jsp输出:
Welcome!!!!!!!! s=null
我想知道为什么没有分配“s”。非常感谢您的帮助。
【问题讨论】:
-
控制台中的任何消息
-
一开始就没有意义;为什么要转发到 JSP 并 返回一个动作映射(带有多余的括号)?为什么要使用小脚本?为什么要使用 Struts 1?无论如何,您甚至向我们展示了哪个 JSP?
first.jsp或您的操作配置中的 JSP?呸。 -
@DaveNewton 实际上我正在尝试构建一个 struts 应用程序。我只是想将一个字符串值从一个 java 文件传递给 jsp。显示的代码是 first.jsp。
-
显示动作制作和您为动作处理点击的 URL。您是否已验证您的操作正在运行?
-
@DaveNewton 我的 struts-config.xml 包含-
--view.jsp 只是一个欢迎文件。网址是 localhost:8080/MyProj/first.do