0

已编辑,仍然无法正常工作,但我需要在网站的每个页面上设置 page_type。该 page_type 需要由一些 java 检查并根据 page_type 执行代码。问题是,if else 语句没有正确完成。没有任何反应,因为似乎没有放置参数。正在设置 page_type 的主页:

<div id="DYK_area">
 <jsp:include page="/imgs/random_dyk.jsp" flush="true">
 <jsp:param name="Page_type" value="prenatalOverviewArea"></jsp:param>
 </jsp:include>
</div>

检查代码的位置

<% String pageType = request.getParameter("page_type")==null?"": request.getParameter("page_type"); 
String image_name="";   
    if(pageType.equals("prenatalOverviewArea")){
        int random = (int )(Math.random() * 10 + 1);
        image_name= "/images/did_you_know/dyk_p_" + random + ".png";
    }
    else if(pageType.equals("prenatalNorm")){
        int random = (int )(Math.random() * 25 + 1);
        image_name= "/images/did_you_know/dyk_p_" + random + ".png";
    }
    else if(pageType.equals("overviewArea")){
        int random = (int )(Math.random() * 20 + 1);
        image_name= "/images/did_you_know/dyk_" + random + ".png";
    }
    else if(pageType.equals("overviewNorm")){
        int random = (int )(Math.random() * 45 + 1);
        image_name= "/images/did_you_know/dyk_" + random + ".png";
    }

if(!image_name.equals("")){
%>

<img id="dyk_random" src="<%=image_name%>"/>

<%}%>
4

3 回答 3

0

您正在使用type变量从请求参数中获取值,但在if您使用的情况下Page_type

包括这样的 JSP

<div id="DYK_area">
   <jsp:include page="/imgs/random_dyk.jsp" flush="true">
     <jsp:param name="Page_type" value="prenatalOverviewArea" />
   </jsp:include>
</div> 

在包含的 JSP 上

<%
    System.out.println("Page type is : "+request.getParameter("Page_type"));
    String Page_type = request.getParameter("Page_type");
    if(Page_type.equalsIgnoreCase("prenatalOverviewArea")){
        int random = (int )(Math.random() * 10 + 1);
        image_name= "/images/did_you_know/dyk_" + random + ".png";
    }

    else if(Page_type.equalsIgnoreCase("prenatalNorm")){
        int random = (int )(Math.random() * 25 + 1);
        image_name= "/images/did_you_know/dyk_p_" + random + ".png";
    }
%><img id="dyk_random" src="<%=image_name%>"/>
于 2013-09-03T16:52:14.723 回答
0

I'm not sure that the code is correct in /imgs/random_dyk.jsp. You use the variable Page_type although it doesn't exist.

You should initialize it before, at line request.getParameter("Page_type");like this:

<%! String xxx = request.getParameter("Page_type"); %>

then ,

<% if(xxx.equals("prenatalOverviewArea")){
...
于 2013-09-03T16:53:37.713 回答
0

这是有效的方法,无论如何,谢谢你们,你们至少让我更接近了!

<div id="DYK_area">
 <jsp:include page="/imgs/random_dyk.jsp" flush="true">
 <jsp:param name="page_type" value="prenatalOverviewArea"></jsp:param>
 </jsp:include>
</div>

<%@ page contentType="text/html; charset=UTF-8" language="java"  errorPage="" %>

<% String pageType = request.getParameter("page_type")==null?"": request.getParameter("page_type"); 
String image_name="";   
    if(pageType.equals("prenatalOverviewArea")){
        int random = (int )(Math.random() * 10 + 1);
        image_name= "/images/did_you_know/dyk_p_" + random + ".png";
    }
    else if(pageType.equals("prenatalNorm")){
        int random = (int )(Math.random() * 25 + 1);
        image_name= "/images/did_you_know/dyk_p_" + random + ".png";
    }
    else if(pageType.equals("overviewArea")){
        int random = (int )(Math.random() * 20 + 1);
        image_name= "/images/did_you_know/dyk_" + random + ".png";
    }
    else if(pageType.equals("overviewNorm")){
        int random = (int )(Math.random() * 45 + 1);
        image_name= "/images/did_you_know/dyk_" + random + ".png";
    }

if(!image_name.equals("")){
%>

<img id="dyk_random" src="<%=image_name%>"/>

<%}%>
于 2013-09-03T19:23:41.440 回答