<转载>Java(41):在jsp中取出存放在Attribute中的对象及属性

2018年2月3日

1.存放在Attribute中的是某个对象的属性

request.setAttribute("userName",userName);

//在jsp中取值//第一种方式取值

<%=request.getAttribute("userName")%>

//第二种方式取值 注意此处必须进行强制类型转换

<% String a=(String)request.getAttribute("userName");


2.存放在Atrribute中的是某个对象

request.setAttribute("user",user);

//取值方式 注意强制类型转换,必须导入User所在的包 否则会报错

<% User user=(User)request.getAttribute("user");%>

//获取user对象中的属性

<%=user.getUserId()%>

<%=user.getUserName()%>


3.存放在Attribute中的是某个集合对象 集合中有n个User对象

request.setAttribute("list",list);

//获值方式 注意 1.导入List所在的包 和User所在的包 2.List和User要进行强制类型转换。

<% List list=(List)request.getAttribute("list");

Iterator it=list.iterator();

while(it.hasNext()){

User user=(User)it.next(); //记得强制类型转换

String userId=user.getId(); //获取User中的属性

}

%>


<?xml version=“1.0”encoding=“UTF-8”?><xsl:stylesheet version=“3.0”xmlns:xsl=“http:xmlns:fo=“http:xmlns:w=“http:exclude-result-prefixes=“w”><!--增强字体映射--><xsl:variable name="font-mapping"><font w:name="宋体"fo:name="SimSun"/><font w:name="黑体"fo:name="SimHei"/><font w:name="等线"fo:name="DengXian"/></xsl:variable><!--根模板--><xsl:template match="/"><fo:root><fo:layout-master-set><fo:simple-page-master master-name="A4"margin="1in"><fo:region-body margin-top="0.5in"margin-bottom="0.5in"/><fo:region-before extent="0.5in"/><fo:region-after extent="0.5in"/></fo:simple-page-master></fo:layout-master-set><fo:page-sequence master-reference="A4"><fo:flow flow-name="xsl-region-body"><xsl:apply-templates select="//w:body/*"/></fo:flow></fo:page-sequence></fo:root></xsl:template><!--增强段落处理--><xsl:template match="w:p"><fo:block xsl:use-attribute-sets="paragraph-style"><xsl:apply-templates select="w:pPr/w:jc"mode="align"/><xsl:apply-templates select="w:pPr/w:ind"/><xsl:apply-templates select=".//w:r"/></fo:block></xsl:template><!--文本格式处理增强--><xsl:template match="w:r"><fo:inline><xsl:apply-templates select="w:rPr"/><xsl:value-of select="string-join(w:t, '')"/></fo:inline></xsl:template><!--增强字体处理--><xsl:template match="w:rPr"><xsl:variable name="w-font"select="w:rFonts/@w:ascii | w:rFonts/@w:hAnsi"/><xsl:attribute name="font-family"><xsl:value-of select="($font-mapping/font[@w:name = $w-font]/@fo:name, $w-font, 'SimSun')[1]"/></xsl:attribute><xsl:if test="w:sz/@w:val"><xsl:attribute name="font-size"select="w:sz/@w:val * 0.5 || 'pt'"/></xsl:if><xsl:if test="w:color/@w:val != 'auto'"><xsl:attribute name="color"select="'#' || w:color/@w:val"/></xsl:if><xsl:apply-templates select="w:b | w:i | w:u | w:strike"/></xsl:template><!--下划线和删除线处理--><xsl:template match="w:u"><xsl:attribute name="text-decoration">underline</xsl:attribute></xsl:template><xsl:template match="w:strike"><xsl:attribute name="text-decoration">line-through</xsl:attribute></xsl:template><!--表格处理增强--><xsl:template match="w:tbl"><fo:table table-layout="fixed"width="100%"><xsl:for-each select="w:tblGrid/w:gridCol"><fo:table-column column-width="{@w:w div 20}pt"/></xsl:for-each><fo:table-body><xsl:apply-templates select="w:tr"/></fo:table-body></fo:table></xsl:template><xsl:template match="w:tr"><fo:table-row><xsl:apply-templates select="w:tc"/></fo:table-row></xsl:template><xsl:template match="w:tc"><fo:table-cell border="1pt solid #000"padding="4pt"><fo:block><xsl:apply-templates select=".//w:p"/></fo:block></fo:table-cell></xsl:template><!--段落对齐处理--><xsl:template match="w:jc"mode="align"><xsl:attribute name="text-align"><xsl:choose><xsl:when test="@w:val = 'center'">center</xsl:when><xsl:when test="@w:val = 'right'">end</xsl:when><xsl:when test="@w:val = 'both'">justify</xsl:when><xsl:otherwise>start</xsl:otherwise></xsl:choose></xsl:attribute></xsl:template><!--段落缩进处理--><xsl:template match="w:ind"><xsl:attribute name="text-indent"><xsl:value-of select="@w:firstLine div 20 || 'pt'"/></xsl:attribute></xsl:template><!--增强段落样式--><xsl:attribute-set name="paragraph-style"><xsl:attribute name="space-after">12pt</xsl:attribute><xsl:attribute name="line-height">1.5</xsl:attribute><xsl:attribute name="text-align">left</xsl:attribute></xsl:attribute-set></xsl:stylesheet>这个不对,xsl:attribute没有select属性
最新发布
03-11
<think>好的,我现在需要帮助用户解决他们提供的XSLT样式表中的问题。用户指出“xsl:attribute没有select属性”,这确实是一个常见的错误。首先,我需要仔细检查用户提供的代码,找出具体哪里用到了`xsl:attribute`的`select`属性。 首先,我看到在字体大小处理的模板中,有这样一行: ```xml <xsl:attribute name="font-size" select="w:sz/@w:val * 0.5 || 'pt'"/> ``` 这里使用了`select`属性,但实际上`xsl:attribute`元素的内容应该通过子元素或文本来设置属性,而不是使用`select`属性。正确的做法是使用`<xsl:value-of>`或者直接计算表达式,然后将其作为元素的内容。 接着在颜色处理部分: ```xml <xsl:attribute name="color" select="'#' || w:color/@w:val"/> ``` 同样的问题,这里错误地使用了`select`属性。应该移除`select`,并将表达式放入`<xsl:value-of>`中,或者直接拼接字符串作为属性的内容。 另外,在段落缩进处理的模板中: ```xml <xsl:attribute name="text-indent" select="@w:firstLine div 20 || 'pt'"/> ``` 这里也错误地使用了`select`属性。正确的做法是计算后,将其作为元素的内容,而不是使用`select`。 接下来,我需要回忆XSLT的正确语法。`xsl:attribute`元素的正确用法是通过其内容来生成属性,而不是通过`select`属性。`select`属性通常用于如`xsl:value-of`或`xsl:apply-templates`等元素中,用于指定数据的来源。因此,用户代码中的这些`select`属性都是无效的,必须被移除,并将表达式转换为使用`<xsl:value-of>`或直接拼接字符串。 例如,字体大小的处理应该改为: ```xml <xsl:attribute name="font-size"> <xsl:value-of select="w:sz/@w:val * 0.5"/> <xsl:text>pt</xsl:text> </xsl:attribute> ``` 这里通过`xsl:value-of`计算数,并添加单位“pt”。同样,颜色处理应该拼接`#`和颜色,使用: ```xml <xsl:attribute name="color"> <xsl:text>#</xsl:text> <xsl:value-of select="w:color/@w:val"/> </xsl:attribute> ``` 对于段落缩进,正确的代码应该是: ```xml <xsl:attribute name="text-indent"> <xsl:value-of select="@w:firstLine div 20"/> <xsl:text>pt</xsl:text> </xsl:attribute> ``` 此外,还需要检查用户代码中是否有其他潜在问题。例如,在字体映射部分,变量`font-mapping`的定义是否正确,是否使用了正确的命名空间。还有,在表格处理中,列宽的计算是否正确,是否将`w:w`除以20转换为pt单位,这可能涉及到Word的twips单位转换(1 twip = 1/20 pt)。 可能还需要检查属性拼接的语法是否正确。例如,原来的`||`操作符在XSLT中并不存在,正确的字符串拼接应使用`concat()`函数或直接通过多个`xsl:value-of`和`xsl:text`元素组合。 总结需要修改的地方: 1. 移除所有`xsl:attribute`中的`select`属性。 2. 使用`xsl:value-of`和文本节点来生成属性。 3. 确保字符串拼接正确,例如颜色的`#`前缀。 4. 检查数计算和单位添加是否正确。 最后,将这些修正点整理成用户容易理解的步骤,并给出修改后的代码示例,帮助用户正确修复他们的XSLT样式表。</think>您指出的问题正确,在XSLT中`xsl:attribute`确实没有`select`属性。以下是代码的修正步骤和说明: $\boxed{问题定位}$ 错误出现在以下三处使用了无效的`select`属性: 1. 字体大小处理:`<xsl:attribute name="font-size" select=...>` 2. 颜色处理:`<xsl:attribute name="color" select=...>` 3. 段落缩进处理:`<xsl:attribute name="text-indent" select=...>` $\boxed{修正方案}$ 需改用以下方式构造属性: 1. **字体大小处理**: ```xml <xsl:if test="w:sz/@w:val"> <xsl:attribute name="font-size"> <xsl:value-of select="w:sz/@w:val * 0.5"/>pt </xsl:attribute> </xsl:if> ``` 2. **颜色处理**: ```xml <xsl:if test="w:color/@w:val != 'auto'"> <xsl:attribute name="color"> <xsl:text>#</xsl:text> <xsl:value-of select="w:color/@w:val"/> </xsl:attribute> </xsl:if> ``` 3. **段落缩进处理**: ```xml <xsl:template match="w:ind"> <xsl:attribute name="text-indent"> <xsl:value-of select="@w:firstLine div 20"/>pt </xsl:attribute> </xsl:template> ``` $\boxed{修正原理}$ 1. **XSLT语法规则**: - `xsl:attribute`的应由其子元素内容生成,而非`select`属性 - 数计算通过`xsl:value-of`实现 - 字符串拼接使用嵌套元素完成(如`xsl:text`) 2. **单位处理**: - Word的`w:sz`以半磅为单位,乘0.5得实际磅 - `w:firstLine`缩进以twips为单位,需除以20转换为磅 $\boxed{改进建议}$ 1. **增加命名空间声明**: 原始代码缺少完整的命名空间声明,应补充: ```xml xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" ``` 2. **增强错误处理**: 建议在关键位置添加容错判断,例如: ```xml <xsl:attribute name="font-family"> <xsl:choose> <xsl:when test="$font-mapping/font[@w:name = $w-font]"> <xsl:value-of select="$font-mapping/font[@w:name = $w-font]/@fo:name"/> </xsl:when> <xsl:otherwise>SimSun</xsl:otherwise> </xsl:choose> </xsl:attribute> ``` $\boxed{修改后效果}$ 修正后的样式表将能正确: - 将12号字体转换为6pt - 将颜色`FF0000`转换为`#FF0000` - 将首行缩进240 twips转换为12pt(240/20=12) - 避免XSLT处理器报错终止转换 建议使用XSLT 3.0处理器(如Saxon-HE 10+)并添加`xsl:mode on-no-match="shallow-copy"`以增强节点处理能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值