struts常用标签
2009-10-12 17:42
483 查看
转自http://hi.baidu.com/javainlife/blog/item/b4e9e0199ff4364343a9ad90.html
html标签
昨天在弄那个 mapping.findForward("local") ; 然后吧提交的数据 在原来的页面上再显示出来
结果成功了。可是 那个页面的css却不加载了 。找了半天原因就是因为少个标签: <html:base/>
<html:base/>
假如一个jsp文件的路径为%YourWeb%/sales/salePane.jsp
如果一个action,forward到这个jsp上
那么在地址栏里输入:http://yourdoman/YourWeb/MyAction.do就可以访问到这个jsp文件
如果你这个jsp文件里有一些相对路径比如<link href="sale.css" rel="stylesheet" type="text/css" />
如果有<html:base/>
那么这个相对路径的绝对路径为:http://yourdoman/YourWeb/sales/sale.css
如果没有<html:base/>的话,那么这个相对路径的绝对路径为:
http://yourdoman/YourWeb/sale.css
所以除非一些特殊情况,否则还是加上的好。
html标签 对 上传提供了 很好的支持 。
上传的时候 仍然使用 html 标签 不过在ActionForm 中 用 FormFile 类型 接收 。
特别注意的是:
在 <html:form action="upLoad" method="post" enctype="multipart/form-data">
</html>加上红色的部分 。
E.g:
Jsp : 页面
<html:form action="upLoad" method="post" enctype="multipart/form-data">
name:<html:text property="name"></html:text>
<br />
file:<html:file property="file"></html:file>
<br />
<html:submit value="提交"></html:submit>
</html:form>
Form 中 对应的 类型是 :FormFile 。
接收的时候 ,要写 io 操作 。
Action :
public class UpLoadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UpLoadForm upLoadForm = (UpLoadForm) form;
// String filepath = this.getServlet().getServletConfig()
// .getServletContext().getRealPath("/")
// + "upload//" + upLoadForm.getFile().getFileName();
String filepath = this.getServlet().getServletConfig()
.getServletContext().getRealPath("/")
+ "upload//"
+ upLoadForm.getName()
+ "."
+ upLoadForm.getFile().getFileName().split("//.")[1];
System.out.println(filepath);
try {
OutputStream out = new FileOutputStream(new File(filepath));
FormFile ff = upLoadForm.getFile();
byte b[] = ff.getFileData();
out.write(b);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
logic 标签
1,<logic:present name="" scope=""></logic:present>
判断是否有指定属性是否存在范围中,如果未指定范围则表示全面查找。
2,逻辑判断
= ------- <logic:equal>
!= ------- <logic:notEqual>
>= ------- <logic:greaterEqual>
<= ------- <logic:lessEqual>
> ------- <logic:greaterThan>
< ------- <logic:lessThan>
3, 迭代标签
<logic:iterate id="每个元素的实例化对象" name="属性" scope="查找范围
"></logic:iterate>
输出的内容可以是:
对象数组 Collection集合(单值) Map集合
4, logic:empty
该标签是用来判断是否为空的。如果为空,该标签体中嵌入的内容就会被处理。
该标签用于以下情况:
1)当Java对象为null时;
2)当String对象为""时;
3)当java.util.Collection对象中的isEmpty()返回true时;
4)当java.util.Map对象中的isEmpty()返回true时。
该句等同于:
if (list.isEmpty()) {
...
}
logic:notEmpty
该标签的应用正好和logic:empty标签相反。
5,logic:forward
该标签用于实现页面导向,查找配置文件的全局forward。struts-config.xml文件中全局转向
eg. <logic:forward name="redirect"/>
nested 标签:
<nested:nest> 定义一个新的嵌套级别
<nested:writeNesting> 输出当前嵌套级别信息
<nested:root>指定顶级别的javabean
其他的还有<nested:text>,<nested:define>,<nested:equal>
他们的功能分别与:<html:text>,<html:define>,<logic:equal>相似,
区别在于Nested标签是相对于当前的嵌套级别。
e.g:
输入页面:showPerson.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested"%>
<html:html lang="true">
<head>
<title>nested.jsp</title>
</head>
<body>
<html:form action="showPerson.do">
<nested:nest property="person">
<nested:writeNesting></nested:writeNesting>
姓名::<nested:text property="name"></nested:text>
<br>
年龄:<nested:text property="age"></nested:text>
<br>
<nested:nest property="addr">
<nested:writeNesting></nested:writeNesting>
省:<nested:text property="province"></nested:text>
<br>
市:<nested:text property="city"></nested:text>
<br>
</nested:nest>
</nested:nest>
<html:submit value="提交"></html:submit>
</html:form>
</body>
</html:html>
对应几个Form:
package com.struts.form;
public class Person {
private String name;
private int age;
private Address addr;
public Address getAddr() {
return addr;
}
public void setAddr(Address addr) {
this.addr = addr;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.struts.form;
public class Address {
private String province;
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}
package com.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class PersonForm extends ActionForm {
Person person;
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
person = new Person();
person.setAddr(new Address());
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
struts-config.xml配置:
<form-beans >
<form-bean name="personForm" type="com.struts.form.PersonForm" />
</form-beans>
<action-mappings >
<action
attribute="personForm"
input="/showPerson.jsp"
name="personForm"
path="/showPerson"
scope="request"
type="com.struts.action.PersonAction">
<forward name="shows" path="/shows.jsp"></forward>
</action>
</action-mappings>
对应Action:
package com.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.struts.form.PersonForm;
public class PersonAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
PersonForm personForm = (PersonForm) form;
return mapping.findForward("shows");
}
}
shows.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested"%>
<html:html lang="true">
<head>
<title>shows.jsp</title>
</head>
<body>
<jsp:useBean id="personForm" type="com.struts.form.PersonForm"
scope="request"></jsp:useBean>
<nested:root name="personForm">
<nested:nest property="person">
<nested:writeNesting></nested:writeNesting>
姓名::<nested:write property="name" />
<br>
年龄:<nested:write property="age" />
<br>
<nested:nest property="addr">
<nested:writeNesting></nested:writeNesting>
省:<nested:write property="province" />
<br>
市:<nested:write property="city" />
<br>
</nested:nest>
</nested:nest>
</nested:root>
</body>
</html:html>
bean标签:
操作javabean:
<jsp:useBean id="" scope="" class="">
<jsp:setProperty name="id" property="属性">
<jsp:getProperty name="id" property="属性">
</jsp:useBean>
id 是定义存放在四种属性范围的对象,name是使用存放在四种属性范围的对象
1,<bean:define> 定义或复制一个对象
定义对象一般为String类型 复制对象迭代标签
e.g:
<bean:define id="str" value="show" />
str:${str}<br>
str:<bean:write name="str"/>
2,<bean:size>
求出长度 : 数组,Collection,Map
<%
Map m = new HashMap();
m.put("one","1");
m.put("two","2");
m.put("three","3");
request.setAttribute("mo",m);
%>
<br>
<bean:size id ="leng" name="mo" scope="requests"/>
${leng }
求长度都是一样的 先设置 几个 在放在 4中属性范围 然后再求长度 。
3<bean:write>
输出对象中的属性。
e.g:
Person类:
package com.struts.form;
public class Person {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<jsp:useBean id="person" class="com.struts.form.Person">
<jsp:setProperty name="person" property="name" value="zzc"/>
<jsp:setProperty name="person" property="password" value="front"/>
</jsp:useBean>
el:<br>
姓名:${person.name }<br>
密码:${person.password }<br>
bean:<br>
姓名:<bean:write name="person" property="name"/><br>
密码:<bean:write name="person" property="password"/><br>
4<bean:message>
struts 国际化;
配置 ApplicationResources.properties
welcome = /u6b22/u8fce {0} /u7684/u5230/u6765
<bean:message key="welcome" arg0="zzc"/>
html标签
昨天在弄那个 mapping.findForward("local") ; 然后吧提交的数据 在原来的页面上再显示出来
结果成功了。可是 那个页面的css却不加载了 。找了半天原因就是因为少个标签: <html:base/>
<html:base/>
假如一个jsp文件的路径为%YourWeb%/sales/salePane.jsp
如果一个action,forward到这个jsp上
那么在地址栏里输入:http://yourdoman/YourWeb/MyAction.do就可以访问到这个jsp文件
如果你这个jsp文件里有一些相对路径比如<link href="sale.css" rel="stylesheet" type="text/css" />
如果有<html:base/>
那么这个相对路径的绝对路径为:http://yourdoman/YourWeb/sales/sale.css
如果没有<html:base/>的话,那么这个相对路径的绝对路径为:
http://yourdoman/YourWeb/sale.css
所以除非一些特殊情况,否则还是加上的好。
html标签 对 上传提供了 很好的支持 。
上传的时候 仍然使用 html 标签 不过在ActionForm 中 用 FormFile 类型 接收 。
特别注意的是:
在 <html:form action="upLoad" method="post" enctype="multipart/form-data">
</html>加上红色的部分 。
E.g:
Jsp : 页面
<html:form action="upLoad" method="post" enctype="multipart/form-data">
name:<html:text property="name"></html:text>
<br />
file:<html:file property="file"></html:file>
<br />
<html:submit value="提交"></html:submit>
</html:form>
Form 中 对应的 类型是 :FormFile 。
接收的时候 ,要写 io 操作 。
Action :
public class UpLoadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UpLoadForm upLoadForm = (UpLoadForm) form;
// String filepath = this.getServlet().getServletConfig()
// .getServletContext().getRealPath("/")
// + "upload//" + upLoadForm.getFile().getFileName();
String filepath = this.getServlet().getServletConfig()
.getServletContext().getRealPath("/")
+ "upload//"
+ upLoadForm.getName()
+ "."
+ upLoadForm.getFile().getFileName().split("//.")[1];
System.out.println(filepath);
try {
OutputStream out = new FileOutputStream(new File(filepath));
FormFile ff = upLoadForm.getFile();
byte b[] = ff.getFileData();
out.write(b);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
logic 标签
1,<logic:present name="" scope=""></logic:present>
判断是否有指定属性是否存在范围中,如果未指定范围则表示全面查找。
2,逻辑判断
= ------- <logic:equal>
!= ------- <logic:notEqual>
>= ------- <logic:greaterEqual>
<= ------- <logic:lessEqual>
> ------- <logic:greaterThan>
< ------- <logic:lessThan>
3, 迭代标签
<logic:iterate id="每个元素的实例化对象" name="属性" scope="查找范围
"></logic:iterate>
输出的内容可以是:
对象数组 Collection集合(单值) Map集合
4, logic:empty
该标签是用来判断是否为空的。如果为空,该标签体中嵌入的内容就会被处理。
该标签用于以下情况:
1)当Java对象为null时;
2)当String对象为""时;
3)当java.util.Collection对象中的isEmpty()返回true时;
4)当java.util.Map对象中的isEmpty()返回true时。
该句等同于:
if (list.isEmpty()) {
...
}
logic:notEmpty
该标签的应用正好和logic:empty标签相反。
5,logic:forward
该标签用于实现页面导向,查找配置文件的全局forward。struts-config.xml文件中全局转向
eg. <logic:forward name="redirect"/>
nested 标签:
<nested:nest> 定义一个新的嵌套级别
<nested:writeNesting> 输出当前嵌套级别信息
<nested:root>指定顶级别的javabean
其他的还有<nested:text>,<nested:define>,<nested:equal>
他们的功能分别与:<html:text>,<html:define>,<logic:equal>相似,
区别在于Nested标签是相对于当前的嵌套级别。
e.g:
输入页面:showPerson.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested"%>
<html:html lang="true">
<head>
<title>nested.jsp</title>
</head>
<body>
<html:form action="showPerson.do">
<nested:nest property="person">
<nested:writeNesting></nested:writeNesting>
姓名::<nested:text property="name"></nested:text>
<br>
年龄:<nested:text property="age"></nested:text>
<br>
<nested:nest property="addr">
<nested:writeNesting></nested:writeNesting>
省:<nested:text property="province"></nested:text>
<br>
市:<nested:text property="city"></nested:text>
<br>
</nested:nest>
</nested:nest>
<html:submit value="提交"></html:submit>
</html:form>
</body>
</html:html>
对应几个Form:
package com.struts.form;
public class Person {
private String name;
private int age;
private Address addr;
public Address getAddr() {
return addr;
}
public void setAddr(Address addr) {
this.addr = addr;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.struts.form;
public class Address {
private String province;
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}
package com.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class PersonForm extends ActionForm {
Person person;
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
person = new Person();
person.setAddr(new Address());
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
struts-config.xml配置:
<form-beans >
<form-bean name="personForm" type="com.struts.form.PersonForm" />
</form-beans>
<action-mappings >
<action
attribute="personForm"
input="/showPerson.jsp"
name="personForm"
path="/showPerson"
scope="request"
type="com.struts.action.PersonAction">
<forward name="shows" path="/shows.jsp"></forward>
</action>
</action-mappings>
对应Action:
package com.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.struts.form.PersonForm;
public class PersonAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
PersonForm personForm = (PersonForm) form;
return mapping.findForward("shows");
}
}
shows.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested"%>
<html:html lang="true">
<head>
<title>shows.jsp</title>
</head>
<body>
<jsp:useBean id="personForm" type="com.struts.form.PersonForm"
scope="request"></jsp:useBean>
<nested:root name="personForm">
<nested:nest property="person">
<nested:writeNesting></nested:writeNesting>
姓名::<nested:write property="name" />
<br>
年龄:<nested:write property="age" />
<br>
<nested:nest property="addr">
<nested:writeNesting></nested:writeNesting>
省:<nested:write property="province" />
<br>
市:<nested:write property="city" />
<br>
</nested:nest>
</nested:nest>
</nested:root>
</body>
</html:html>
bean标签:
操作javabean:
<jsp:useBean id="" scope="" class="">
<jsp:setProperty name="id" property="属性">
<jsp:getProperty name="id" property="属性">
</jsp:useBean>
id 是定义存放在四种属性范围的对象,name是使用存放在四种属性范围的对象
1,<bean:define> 定义或复制一个对象
定义对象一般为String类型 复制对象迭代标签
e.g:
<bean:define id="str" value="show" />
str:${str}<br>
str:<bean:write name="str"/>
2,<bean:size>
求出长度 : 数组,Collection,Map
<%
Map m = new HashMap();
m.put("one","1");
m.put("two","2");
m.put("three","3");
request.setAttribute("mo",m);
%>
<br>
<bean:size id ="leng" name="mo" scope="requests"/>
${leng }
求长度都是一样的 先设置 几个 在放在 4中属性范围 然后再求长度 。
3<bean:write>
输出对象中的属性。
e.g:
Person类:
package com.struts.form;
public class Person {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<jsp:useBean id="person" class="com.struts.form.Person">
<jsp:setProperty name="person" property="name" value="zzc"/>
<jsp:setProperty name="person" property="password" value="front"/>
</jsp:useBean>
el:<br>
姓名:${person.name }<br>
密码:${person.password }<br>
bean:<br>
姓名:<bean:write name="person" property="name"/><br>
密码:<bean:write name="person" property="password"/><br>
4<bean:message>
struts 国际化;
配置 ApplicationResources.properties
welcome = /u6b22/u8fce {0} /u7684/u5230/u6765
<bean:message key="welcome" arg0="zzc"/>
相关文章推荐
- 常用struts标签使用举例--bean篇(转载)
- struts常用标签总结
- Struts 有哪些常用标签库
- struts常用标签举例
- Struts 2 常用标签(Tag)详解
- struts.xml中常用到的标签
- 常用struts标签使用举例--logic篇 (转载)
- validation.xml验证文件的配置 常用struts标签使用举例--bean篇
- struts中的标签三个最常用的属性
- STRUTS2 常用标签的使用
- 常用struts标签使用举例--bean篇(转载)
- 常用struts标签使用举例--logic篇 (转载)
- JAVAWEB开发之Struts2详解(四)——ognl与valueStack(重点)、Struts常用标签、防止表单重复提交、Struts2中内置json插件
- Struts 有哪些常用标签库
- SSH(struts+spring+hibernate)迅速开发--附录2 Struts 常用标签&amp;国际化(1)
- 传智播客 struts1.x 常用标签的使用
- Struts2笔记——struts常用标签
- struts常用标签总结
- STRUTS2.0 常用标签 struts2标签 s
- SSH(struts+spring+hibernate)迅速开发--附录2 Struts 常用标签&国际化(2)