似乎有两种使用JSP进行模板化的方法.包含其中一个语句的文件
<%@ include file="foo.html" %>
<jsp:include page="foo.html" />
Run Code Online (Sandbox Code Playgroud)
或使用JSP标记文件
// Save this as mytag.tag
<%@ tag description="Description" pageEncoding="UTF-8"%>
<html>
<head>
</head>
<body>
<jsp:doBody/>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在另一个JSP页面中调用它
<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:mytag>
<h1>Hello World</h1>
</t:mytag>
Run Code Online (Sandbox Code Playgroud)
那么我应该使用哪种方法?现在一个被认为已被弃用,或者它们是否有效且涵盖不同的用例?
编辑
是否使用此标记文件与使用包含相同?
// Save this as product.tag
<%@ tag description="Product templage" pageEncoding="UTF-8"%>
<%@ tag import="com.myapp.Product" %>
<%@ attribute name="product" required="true" type="com.myapp.Product"%>
Product name: ${product.name} <br/>
Quantity: ${product.quantity} <br/>
Run Code Online (Sandbox Code Playgroud)
并在另一个JSP上调用它
<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:product>
<c:forEach items="${cart.products}" var="product">
<t:product product="${product}"/>
</c:forEach>
</t:product>
Run Code Online (Sandbox Code Playgroud)
在我看来,这与使用include和传递参数非常相似.标签文件与包含相同吗?
我在JSP页面上有一个来自对象的变量:
<%= ansokanInfo.getPSystem() %>
变量的值是NAT,这是正确的,我想为这个值应用某些页面元素.如何使用标签来了解案例?我试过类似的东西
<c:if test = "${ansokanInfo.getPSystem() == 'NAT'}">
process
</c:if>
Run Code Online (Sandbox Code Playgroud)
但上面没有显示任何内容.我该怎么办?或者我也可以使用scriptlet即
<% if (ansokanInfo.getPSystem().equals("NAT"){ %>
process
<% } %>
Run Code Online (Sandbox Code Playgroud)
感谢您的任何回答或评论.
为自定义JSP标记定义属性时,是否可以指定默认值?该attribute
指令没有默认值属性.目前我正在做的事情:
<%@ attribute name="myAttr" required="false" type="java.lang.String" %>
<c:if test="${empty myAttr}" >
<c:set var="myAttr" value="defaultValue" />
</c:if>
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
是否有可能在JSTL中做这样的事情:
<div class="firstclass<c:if test='true'> someclass</c:if>">
<p>some other stuff...</p>
</div>
Run Code Online (Sandbox Code Playgroud)
有没有办法让它工作,或者是否有更好的方法通过查看JSTL-if语句来添加类?
我有一些扩展超类的类,在JSP中我想展示这些类的一些属性.我只想制作一个JSP,但我事先并不知道该对象是否具有属性.所以我需要一个JSTL表达式或一个标签来检查我传递的对象是否具有此属性(类似于javascript中的运算符,但在服务器中).
<c:if test="${an expression which checks if myAttribute exists in myObject}">
<!-- Display this only when myObject has the atttribute "myAttribute" -->
<!-- Now I can access safely to "myAttribute" -->
${myObject.myAttribute}
</C:if>
Run Code Online (Sandbox Code Playgroud)
我怎么能得到这个?
谢谢.
我目前正在开发基于Spring 3.1.0.M1的基于注释的Web应用程序,并且我在应用程序的一个特定位置解析属性占位符时遇到问题.
这是故事.
1)在我的Web应用程序上下文(由DispatcherServlet加载)中,我有
MVC-config.xml文件:
<!-- Handles HTTP GET requests for /resources/version/** -->
<resources mapping="/${app.resources.path}/**" location="/static/" cache-period="31556926"/>
...
<!-- Web properties -->
<context:property-placeholder location="
classpath:app.properties
"/>
Run Code Online (Sandbox Code Playgroud)
2)在app.properties内部,有2个属性,其中包括:
app.properties:
# Properties provided (filtered) by Maven itself
app.version: 0.1-SNAPSHOT
...
# Static resources mapping
app.resources.path: resources/${app.version}
Run Code Online (Sandbox Code Playgroud)
3)我在JSP 2.1模板中有一个JSP自定义标记.此标记负责完整的资源路径构建,具体取决于环境设置,应用程序版本,弹簧主题选择等.自定义标记类扩展spring:url实现类,因此它可能被视为通常的url标记,但具有关于正确路径的一些额外知识.
我的问题是我无法在JSP自定义标记实现中正确解析$ {app.resources.path}.JSP自定义标记由servlet容器管理,而不是Spring,因此不参与DI.所以我不能只使用通常的@Value("$ {app.resources.path}")并由Spring自动解决它.
我所有的都是Web应用程序上下文实例,所以我必须以编程方式解析我的属性.
到目前为止我试过:
ResourceTag.java:
// returns null
PropertyResolver resolver = getRequestContext().getWebApplicationContext().getBean(PropertyResolver.class);
resolver.getProperty("app.resources.path");
// returns null, its the same web context instance (as expected)
PropertyResolver resolver2 = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext()).getBean(PropertyResolver.class);
resolver2.getProperty("app.resources.path");
// throws NPE, resolver3 …
Run Code Online (Sandbox Code Playgroud) 我想在同一个jsp上使用javascript变量作为另一个标记的'src'属性.
<script>
var link = mylink // the link is generated based on some code
</script>
Run Code Online (Sandbox Code Playgroud)
我想创建这个新元素,如下所示.
<script src="mylink">
</script>
Run Code Online (Sandbox Code Playgroud)
在搜索各种论坛时,我尝试使用以下选项,但它们似乎不起作用.我希望这个东西适用于所有主流浏览器.
将此代码放在第一个元素中.
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "path/to/somelink";
document.body.appendChild(script);
Run Code Online (Sandbox Code Playgroud)在第一个元素中使用document write方法.
document.write("<script type='text/javascript' src="+ google.com + "><\/script>");
Run Code Online (Sandbox Code Playgroud)试图在第一个元素中设置JSTL变量并使用它.
<c:set var="URL" value="mylink"/>
Run Code Online (Sandbox Code Playgroud)这些方法都没有成功.什么是错误的任何建议?
我曾经写过这个方法:
private <T> SortedSet<T> createSortedSet() {
return new TreeSet<T>();
}
Run Code Online (Sandbox Code Playgroud)
它应该被称为这样:
Set<String> set = createSortedSet();
Run Code Online (Sandbox Code Playgroud)
这工作正常(虽然我在研究当前的问题时看到了答案,这是容易出错的).
无论如何,现在我正在编写以下代码(在扩展javax.servlet.jsp.tagext.TagSupport的类中):
private <T> T evaluate(String expression) {
ExpressionEvaluator evaluator = pageContext.getExpressionEvaluator();
return evaluator.evaluate(expression, T, null, pageContext.getVariableResolver());
}
Run Code Online (Sandbox Code Playgroud)
目的是能够将其称为:
Integer width = evaluate(widthStr);
Run Code Online (Sandbox Code Playgroud)
我的evaluate方法中的代码显然不起作用.第二个参数evaluator.evaluate()
应该是一个Class对象.这导致我:
如何获得泛型(返回)类型的类?作为评估的第二个参数,我应该用什么来代替T?
Nicolas似乎是对的,它无法完成,我需要将类作为参数传递给我的方法.好处是,因为他的解决方案使参数化参数化在泛型类型上,我得到了对该参数的编译检查.
我试图遵循spring JPetStore的例子,但是在引用lib标签spring的行中的JSP页面中出现错误:
找不到"http://www.springframework.org/tags"的标记库描述符
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
Run Code Online (Sandbox Code Playgroud)
这个库的URL是什么?
有没有办法避免直接依赖这个URL?
提前致谢
我创建了一个jsp页面如下:
<%@ page contentType="text/css" %>
<html>
<head>
<title>Login page</title>
<link href="/css/loginstyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1> India welfare</h1>
<p> welcome </p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
并将其命名为login.jsp
我还创建了一个名为loginstyle.css的css文件,.css文件的代码如下:
body
{
background-color:#d0e4fe;
}
h1
{
color:orange;
text-align:center;
}
p
{
font-family:"Times New Roman";
font-size:20px;
}
Run Code Online (Sandbox Code Playgroud)
css和jsp的目录结构如下: .css文件的webcontent/welfare_web/css和 jsp文件的webcontent/welfare_web/login
编程编辑器是eclipse,我使用的服务器是tomcat 7.0.当我尝试使用tomcat服务器运行login.jsp文件时.css文件没有显示任何效果.我的意思是输出是普通文本,而不是CSS文件.
请帮我如何使.css文件生效jsp文件.
jsp-tags ×10
jsp ×8
java ×6
jstl ×3
spring ×2
css ×1
custom-tag ×1
generics ×1
include ×1
javascript ×1
spring-mvc ×1
tagfile ×1
websphere ×1