${}在我的JSP页面上不起作用。如何让我的${} HTML标签再次正常工作?

huangapple 未分类评论50阅读模式
英文:

${} is not working on my JSP page. How can i get my ${} html tag to work again?

问题

以下是翻译好的内容:

所以 ${} 有效。但是我使用的 JSTL jar 文件使得 ${} 不再起作用。这些是我的 JSTL jar 文件。jstl-1.2 (1).jar、jstl-impl-1.2.jar、jstl-standard.jar。我正在按照 Navin 在 YouTube 上的 Servlet 和 JSP 教程进行操作。他跳过了 JSTL jar 文件的部分。我是一名初级开发者,试图理解为什么我的 ${} 不再起作用。

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

请温柔一点。: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>插入标题在这里</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);
		
	}
}
英文:

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?

Please be gentle. ${}在我的JSP页面上不起作用。如何让我的${} HTML标签再次正常工作?

<%@ 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);
		
	}
}


</details>


# 答案1
**得分**: -1

"${}"是EL表达式语言语法。您需要在您的"web应用程序"的web.xml或变体中配置,以使用JSTL(Java标准标签库).jar文件,可以将它放在服务器的/commons/lib文件夹中,也可以放在/WEB-INF/lib文件夹中。

**然后在您的JSP页面顶部调用您希望使用的每个标签前缀的名称。**
Tomcat有几种实现方式。

此外,您的文档类型应为

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

您的.tld标签库描述符应位于WEB-INF文件夹中。

在您的web.xml文件中定义标签库位置

<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中声明,并且在页面顶部进行页面声明以调用它。

如果出现问题,重新检查上述内容不是坏主意。

您创建的Servlet是一个GET请求的Servlet,它通过URL传递参数,参数位于URL中的

**?name=这里是值&anid=**这里是某内容&terminus=

如果您在URL中使用${Param.student},将其添加为student,放在“?”之后,这可能对EL表达式来说是可用的,类似于这样

**?students=name=**这里是值&next1=这里是某内容&next2=这里是某内容&terminus=

POST Servlet无法在URL中传递参数,而您的代码似乎尝试这样做,所以request.setAttribute是为POST请求设置的(POST请求会携带令牌)

另外,request对象上的setAttribute方法可以通过其类的接口进行调用,也可以通过一个包装的子类在类级别进行调用,如下所示

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

然而,尽管更现代的Web容器版本可以识别复杂类型,如List和Map(但可能不识别Student类),您可能可以通过将对象转换为字符串来使用那段代码。

Student在Web应用程序解析规则中是无法识别的,但是如果您让Student扩展(Map int, String),那么运行时和编译器可能可以将其设置为(Map K, V)。
实际上,这不能工作,因为您在JSP处理之前在Servlet中尝试执行此操作 [ 除非Student类只是类文件夹中的servlet支持类(不是bean语法类) 参见下一段 ]。

您试图以Bean的方式使用一个类,而Bean必须在web.xml和页面中声明,并且还必须通过一个Student对象引用来通知Servlet,无论是从classes还是/classes/beans文件夹调用的!!!
如果它是一个Bean,它应该在bean文件夹中(如果它不是Bean,只是一个支持处理类,应该在classes中,Servlet可以随包或不随包),并且在JSP解析器中通过响应进行调用,但必须加载正确,正确更新当前实例并在Web应用程序用户会话中使用(JSF更容易实现此功能)。
您可以通过获取Web应用程序上下文和初始实例线程来获得当前会话和Servlet实例使用的bean,以找到您想要的bean,其当前实例和当前状态(除非值是恒定的,否则需要是会话bean,除非它只通过即时的set get处理输出),以获取其当前值。 Bean类必须在整个应用程序配置中声明,与Servlet类似,但声明规则不同,具有运行时和语法的严格规定。

最后注意:

// 第一个参数应该是一个字符串,而不是一个整数
req.setAttribute("String Object", (java.lang.Object)anObject);
//注意:该对象必须可以从可识别的Java语言核心类转换为字符串!!!

关于POST请求的一个简短说明实际上它可以携带一个查询字符串但在Java框架内被认为是一种奇怪的操作
英文:

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.

Also your doctype should be

&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;

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

define the taglib location in your web.xml file

    &lt;jsp-config&gt;
&lt;taglib&gt;
    &lt;taglib-uri&gt;http://java.sun.com/jstl/core&lt;/taglib-uri&gt;
    	&lt;taglib-location&gt;/WEB-INF/c.tld&lt;/taglib-location&gt;
    	&lt;/taglib&gt;
    &lt;/jsp-config&gt;

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.

Not a bad point to recheck the above if things go wrong.

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

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

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=**valuOFthisPart&next1=somethinHere&next2=somethinHere&terminus=

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).

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(&quot;students&quot;, studs);

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 ].

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.

Final note

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

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

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

huangapple
  • 本文由 发表于 2020年5月29日 13:19:33
  • 转载请务必保留本文链接:https://java.coder-hub.com/62079182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定