【问题标题】:Converting this scriptlet code to JSTL / EL?将此 scriptlet 代码转换为 JSTL / EL?
【发布时间】:2012-06-09 10:18:54
【问题描述】:

我不知道如何将此 scriptlet 代码重新编码为 JSTL / EL taglib。这些语句非常简单,但它使用会话数据并生成我不知道如何在 JSTL / EL 中执行的新对象。是否可以将其重新编码为 JSTL / EL 或者我是否需要将其放入预处理 servlet?如果我需要预处理 servlet,那么我仍然不知道如何管理,因为该视图没有特定的请求处理程序,只有对将会话对象填充到我们的 jsp 页面的网页的访问。如何将其重写为 JSTL / EL?

<%
    final Logger logger = Logger.getLogger("arendeprocess_grunduppgifter_visa.jsp");
    ArendeProcessPageController apc = new ArendeProcessPageController(request);
    GrunduppgifterPageController pc = new GrunduppgifterPageController(request);
    String arendeTyp = apc.getArendeTyp();
    boolean showSearch = false;
    int vectr = 0; // får bara användas i errormessages.jspf
    IFormData ifData = PandoraManager.getSessionData(session).getFormData();

    AnsokanInfo ansokanInfo = apc.getAnsokanInfo();
    PersonInfo editPerson = new PersonInfo();
    if(ansokanInfo != null && ansokanInfo.hasEditPersonInfo()) {
        editPerson = ansokanInfo.getEditPersonInfo();
    } else {
        editPerson.setFornamn(apc.getNyregPerson().getFornamn());
        editPerson.setEfternamn(apc.getNyregPerson().getEfternamn());
        editPerson.setForetag(apc.getNyregPerson().getForetag());
        //editPerson.setOrgnr(apc.getNyregPerson().getOrgnr());
        editPerson.setLandKod(apc.getNyregPerson().getLandKod());
    }
    if(apc.getLatestAction().equals("Namnsokning") && apc.getLatestActionCommand().equals("search")) {
        showSearch = true;    
    }
    List<PersonInfo> sokandeList = ansokanInfo.getSokandeList();
    List<PersonInfo> uppfinnareList = ansokanInfo.getUppfinnareList();
    List<PersonInfo> ombudList = ansokanInfo.getOmbudList();
    List<Prioritet> prioriteter = ansokanInfo.getPrioriteter();
    List<Deposition> depositioner = ansokanInfo.getDepositioner();
    request.setAttribute("request", request);
    request.setAttribute("ansokanInfo", ansokanInfo); 
    request.setAttribute("editPerson", editPerson);
    request.setAttribute("apc", apc);
    request.setAttribute("sokandeList", sokandeList);
    request.setAttribute("uppfinnareList", uppfinnareList);
    request.setAttribute("ombudList", ombudList);
    request.setAttribute("GrunduppgifterConstants", new GrunduppgifterConstants());
%>

【问题讨论】:

    标签: jsp servlets jstl el scriptlet


    【解决方案1】:

    是的,您可以在 scriptlet 中编写的任何内容都可以在 EL/Custom Tags 中编写。

    有两种方法:

    • 将所有 Controller 类更改为 Use Beans 并使用 EL 编写上述代码。
    • 将上述代码移至自定义标签并消除脚本

    【讨论】:

      【解决方案2】:

      JSTL 旨在控制 HTML 生成的流程。然而,这个 scriptlet 不会产生任何 HTML,所以用 JSTL 替换是完全没有问题的。

      您确实需要一个预处理 servlet。我只是不明白你到底是什么意思

      我仍然不知道如何管理,因为这个视图没有特定的请求处理程序,只有对将会话对象填充到我们的 jsp 页面的网页的访问

      但是应该将有问题的 JSP 文件移动到 /WEB-INF 文件夹中,这样就永远无法直接访问它并创建一个映射到所需 URL 模式并在 doGet() 方法中完成工作的 servlet最后转发到所需的 JSP。假设 JSP 文件是 /page.jsp 并已重定位到 /WEB-INF/page.jsp,那么 servlet 将如下所示:

      @WebServlet("/page.jsp")
      public class PageServlet extends HttpServlet {
      
          @Override
          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              // Put the original scriptlet code here.
      
              // Keep the following line untouched.
              request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
          }
      
      }
      

      【讨论】: