$ {}在我的JSP页面上不起作用.我如何才能使$ {} html标记重新工作? [英] ${} is not working on my JSP page. How can i get my ${} html tag to work again?

查看:74
本文介绍了$ {}在我的JSP页面上不起作用.我如何才能使$ {} html标记重新工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此$ {}确实有效.但是我正在使用的JSTL jar文件在$ {}不再起作用的地方产生了.这些是我的JSTL jar文件. jstl-1.2(1).jar,jstl-impl-1.2.jar,jstl-standard.jar.我正在关注Servlet&上的Navin教程. JSP教程| YouTube上的完整课程.他跳过了JSTL jar文件.我是一名初级开发人员,试图了解为什么我的$ {}不再起作用.

So ${} did work. But the JSTL jar files that I'm using made to where the ${} doesn't work anymore. These are my JSTL jar files. jstl-1.2 (1).jar, jstl-impl-1.2.jar, jstl-standard.jar. I am following Navin tutorial on Servlet & JSP Tutorial | Full Course on youtube. He skipped JSTL jar files. I'm a junior developer trying to understand why my ${} isn't working anymore.

问题:为什么我的$ {}标签不起作用了?

Question: Why did my ${} tag not work anymore?

请保持温柔. :D

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html >
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>


    <c:forEach items="${students}" var="s" >
         ${s} <br>

    </c:forEach>



</body>
</html>

package com.Demo;

public class Student {
    int rollno;
    String name;


    @Override
    public String toString() {
        return "Student [rollno=" + rollno + ", name=" + name + "]";
    }


    public int getRollno() {
        return rollno;
    }


    public void setRollno(int rollno) {
        this.rollno = rollno;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public Student(int rollno, String name) {
        super();
        this.rollno = rollno;
        this.name = name;
    }


}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
  <display-name>JSTLexample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

package com.Demo;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/DemoServlet")
public class DemoServlet extends HttpServlet{

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {

        //String name = "Navin";

        List <Student> studs = Arrays.asList(new Student(1, "brandon"), new Student(2, "Micheal"), new Student (3, "Charles"));

        request.setAttribute("students", studs);

        RequestDispatcher rd = request.getRequestDispatcher("display.jsp");
        rd.forward(request, response);

    }
}


推荐答案

"$ {}"是EL表达式语言语法.您需要将"Web应用程序" web.xml或变体配置为使用server.xml(显然不是)或/WEB-INF/中的servers/commons/lib文件夹中的JSTL(Java标准标记库).jar文件.您的应用程序的lib. 然后在JSP页面顶部声明要使用的每个标记前缀的名称. Tomcat有几种实现它的方法.

That "${}" is EL expression language syntax. You require to configure your "web application" web.xml or variant to use the JSTL (Java Standard Tag Library) .jar file in either the servers /commons/lib folder with server.xml (obviously not) or /WEB-INF/lib of your application. Then call in the names of each tag prefix you wish to use declared at the top of your JSP page. Tomcat has a few ways of achieving it.

您的文档类型也应该是

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

Yout .tld标记库描述符应位于WEB-INF文件夹中.

Yout .tld tag library descriptor should be in the WEB-INF folder.

在您的web.xml文件中定义taglib位置

define the taglib location in your web.xml file

    <jsp-config>
<taglib>
    <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
        <taglib-location>/WEB-INF/c.tld</taglib-location>
        </taglib>
    </jsp-config>

此外,您的bean类也应该在web.xml中声明,并且页面声明也要在顶部的页面中调用.

Also, your bean class should be declared in the web.xml and the page declarations to call it in the page at the top too.

如果出现问题,可以再次检查以上内容.

您创建的servlet是 GET请求 servlet,它们通过

The servlet you have made is a GET request servlet , they pass parameters on the URL by

?name = **此零件的值& anid = **此处的某物& terminus =

?name=**valuOFthisPart&anid=**somethinHere&terminus=

如果您使用$ {Param.student}作为学生添加在?"之后在可能对EL可用的网址上

if you use ${Param.student} added as student past the "?" on the url that may be usable to the EL something like this

**?students = name = **此部分的值& next1 =此处某物& next2 =此处某物& terminus =

**?students=name=**valuOFthisPart&next1=somethinHere&next2=somethinHere&terminus=

POST servlet不能在URL上携带参数,这是您尝试通过代码执行的操作,因此 request.setAttribute设置要针对POST请求进行的操作(POST请求中确实包含令牌).

A POST servlet cannot carry parameters on the URL and is what you are trying to do by the code so is what the request.setAttribute is setting doing is for a POST request (POST requests do carry tokens).

请求对象上的setAttribute也可以通过其类的接口使用,该类也可以通过包装子类在类级别上进行操作

Too, the setAttribute on request object is available by the interface of its class of which it can be done at class level by a wrapper sub class too as next

javax.servlet.ServletRequestWrapper requestWrpp = new javax.servlet.ServletRequestWrapper(request);
requestWrpp.setAttribute("students", studs);

但是,虽然更现代的Web容器版本可以识别诸如List和Map之类的复杂类型(但可能不是Student类),但是您可能可以通过我模糊地认为我记得使用条款来使用代码JSP处理中复杂对象的定义是,并且可以理解为可转换为字符串. Web应用程序解析器规则无法识别Student,但是,如果您编写Student来扩展(Map int,String),那么运行时和编译器便可以将其中的设置用作(Map K,V) 实际上,这是行不通的,因为您尝试通过响应[!在JSP处理之前在servlet中执行此操作.除非Student类在classes文件夹中只是 servlet支持类. (不是 bean语法类) 参见下一段].

HOWEVER, while more modern versions of web container recognise complex types such as List and Map (but probably not Student class) you may be able to use the code there by what i vaguely think i remember the clause of use of complex objects in JSP processing is and that being it is understood to be convertible to string. Student is unrecognizable to the web app parser rules, however if you wrote Student to extend ( Map int,String ) then the runtime and compiler may be able to use that set up inside as a ( Map K,V ) Actually, this cannot work because you try to do this in the servlet before JSP processing by the response [ ! unless the Student class is only a servlet support class in the classes folder. (not bean syntax class) see next paragraph ].

您正在尝试使用类来操作Bean,并且必须在web.xml和页面中声明一个Bean,并且还必须使用来自class或/classes/beans文件夹的Student对象引用来通知servlet和servlet. !!! 如果是Bean,则应将其放置在Bean文件夹中(如果不是Bean,并且只有支持处理类应该在带有servlet打包的类中),并由JSP解析器中的响应调用,但必须正确加载,应该使用在Web应用程序用户会话中正确更新的当前实例(JSF可以更轻松地完成). 您可以通过获取Web应用程序上下文和初始实例线程来查找所需的Bean,其当前实例和当前状态(需要是会话Bean,除非值是恒定的或必须在整个应用程序配置中声明Bean类,该类与servlet相似,但在运行时和语法的声明严格规则上有所不同.

You are trying to use a class the way a bean operates , and a bean must be declared both in the web.xml and the page and the servlet notified too with a Student object reference from either classes or /classes/beans folder !!! If it is a bean it should be in a bean folder (if it is not a bean and only a support processing class should be in classes with the servlet packaged or not) and called by the response in the JSP parser , but properly loaded, correctly updated current instance of it in the web app user session should be used (something JSF does more easily). You can obtain current session and beans for servlet instance use by acquiring the web app context and initial instance thread to find the bean you want, its current instance and current state (requires to be a session bean unless the values are constant or it only outputs by set get processing instantly) to get its' current values. Bean classes must be declared throughout the app configuration not dissimilar to servlets but are different with rigid rules of declaration for runtime and syntax.

最后的记录

// the first argument should be a string NOT an int
req.setAttribute("String Object",(java.lang.Object)anObject);

//注意:该对象必须可以从可识别的Java语言核心类转换为String !!!

//NOTE: That object must be convertible to a String from a recognizable java language core class !!!

关于发布请求的简短说明,实际上它可以携带查询字符串,但是在Java框架内被认为是一种怪异的动作.

Just a quick note about post requests, actually it can carry a query string but is considered a bizzarre action inside a Java framework.

这篇关于$ {}在我的JSP页面上不起作用.我如何才能使$ {} html标记重新工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆