一.Struts标签简介
在Struts中为了方便用户的开发依然提供了很多成型的标签库。无论哪种标签库实际上
都是与四种属性(page、request、session、application)范围有关的操作,所以
JSTL和Struts标签库是完全可以混合使用的。但是是这种情况基本不可能出现,因为Struts
标签库已经具备了完全的功能,直接使用即可,一般在不实用Web框架时就会使用JSTL。
以下主要介绍三个核心标签库:html、bean、logic。
二.Bean标签
Bean标签库的主要作用是定义和访问JavaBean。
<bean:define>定义变量:
s2.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>s2.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<bean:define id="info" scope="page">
CaMnter
</bean:define>
<bean:define id="message" value="Save you from anything 07"></bean:define>
<h2>内容1:${info }</h2>
<h2>内容2:${pageScope.message }</h2>
</body>
</html>
<bean:define>复制JavaBean对象:
s3.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>s3.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<jsp:useBean id="copyBean" class="com.zyy.struts.vo.CopyBean" scope="page"></jsp:useBean>
<jsp:setProperty property="message" name="copyBean" value="Save you from anything 07"/>
<bean:define id="info" name="copyBean" property="message"></bean:define>
<h2>复制后:${info }</h2>
</body>
</html>
<bean:define>中的id属性可以设置你给变量起的名或者你给复制过来的JavaBean起的名
name表示要复制的JavaBean的id。
CopyBean.java:
package com.zyy.struts.vo;
public class CopyBean {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
<bean:size>标签:
s4.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>s4.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%
List me = new ArrayList() ;
me.add("CaMnter") ;
me.add("Save you from anything 07") ;
pageContext.setAttribute("me", me) ;
%>
<bean:size id="length" name="me" scope="page"/>
<h2>长度:${length }</h2>
</body>
</html>
<bean:cookie>标签:
s5.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>s5.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<bean:cookie name="user" id="myCookie" value="CaMnter"/>
<%
//保存3000秒
myCookie.setMaxAge(3000) ;
response.addCookie(myCookie) ;
Cookie cookies[] = request.getCookies() ;
for(Cookie cookie : cookies){
%>
<h3><%= cookie.getName()%> --> <%=cookie.getValue() %></h3>
<%
}
%>
</body>
</html>
<bean:write>标签和<bean:include>标签:
s6.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>s6.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<bean:define id="CaMnter">
define_Save you from anything 07
</bean:define>
<bean:write name="CaMnter" ></bean:write>
<bean:include id="include" page="/jsp/content.html"/>
${include }
</body>
</html>
content.html:
<!DOCTYPE html>
<html>
<head>
<title>content.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h2>Save you from anything 07</h2>
</body>
</html>
<bean:resource>标签:
s7.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>s7.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<bean:resource name="/jsp/content.xml" id="resource"/>
<bean:write name="resource"/>
</body>
</html>
<bean:message>标签与国际化:
s8.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>s8.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<bean:message key="beanMessage.info" arg0="bean:" arg1="message"/>
</body>
</html>
资源文件Properties页面显示:
资源文件Source页面显示:
三.Logic标签
logic标签的主要作用是进行各种逻辑处理。如:执行分支语句、迭代、比较等操作。
<logic:present>和<logic:notPresent>标签:
s9.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>s9.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%
request.setAttribute("CaMnter", "Save you from anything 07") ;
%>
<logic:present name="CaMnter" scope="request">
<h2>我心中存在:${requestScope.CaMnter }</h2>
</logic:present>
<logic:notPresent name="me" scope="application">
<h2>她心中的Application范围内也不存在我</h2>
</logic:notPresent>
</body>
</html>
<logic:empty>标签和<logic:notEmpty>标签:
sa.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>sa.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%
List<String> me = new ArrayList<String>() ;
request.setAttribute("me", me) ;
%>
<logic:empty name="me" scope="request">
<h2>长度为0也被判为空</h2>
</logic:empty>
<logic:empty name="she" scope="request">
<h2>不存在也被判为空</h2>
</logic:empty>
</body>
</html>
这里,提示一下:长度为0在<logic:empty>中也被判定为空。
关系运算标签:
sb.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>sb.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%
request.setAttribute("she", 7) ;
request.setAttribute("me", "Save you from anything 07") ;
%>
<logic:equal value="Save you from anything 07" name="me" scope="request">
<h2>Save you from anything 07</h2>
</logic:equal>
<logic:notEqual value="forget her" name="me" scope="request">
<h2>impossible</h2>
</logic:notEqual>
<logic:lessThan value="70" name="she" scope="request">
<h2>小于70</h2>
</logic:lessThan>
<logic:greaterThan value="0" name="she" scope="request">
<h2>大于0</h2>
</logic:greaterThan>
<logic:lessEqual value="7" name="she" scope="request">
<h2>小于等于7</h2>
</logic:lessEqual>
<logic:greaterEqual value="7" name="she" scope="request">
<h2>大于等于7</h2>
</logic:greaterEqual>
</body>
</html>
<logic:iterate>标签(logic标签库中最重要的):
sc.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>sc.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%
String me_str[] = { "CaMnter", "Save you from anything 07" };
request.setAttribute("me_str", me_str);
%>
<h3>输出数组:</h3>
<ol>
<logic:iterate id="me_part_str" name="me_str" scope="request">
<li>${me_part_str }</li>
</logic:iterate>
</ol>
<%
List me_list = new ArrayList();
me_list.add("07");
me_list.add("Never remember me");
request.setAttribute("me_list", me_list);
%>
<h3>输出List:</h3>
<ol>
<logic:iterate id="me_part_list" name="me_list" scope="request">
<li>${me_part_list }</li>
</logic:iterate>
</ol>
<%
Map me_map = new HashMap();
me_map.put("If time could go back",
"I am willing to return to the past");
request.setAttribute("me_map", me_map);
%>
<h3>输出Map:</h3>
<ol>
<logic:iterate id="me_part_map" name="me_map" scope="request">
<li>${me_part_map.key } -- > ${me_part_map.value }</li>
</logic:iterate>
</ol>
</body>
</html>
<logic:redirect>重定向标签:
修改struts-config.xml:
<struts-config>
<form-beans>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
<forward name="content" path="/jsp/content.html" />
</global-forwards>
<action-mappings>
</action-mappings>
<message-resources parameter="resource.MessageResources" />
</struts-config>
以上在<global-forwards>节点中添加了全局forward。
sd.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>sd.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<!-- 跳转到<global-forwards>中name="content"的forward配置的path路径 -->
<logic:redirect forward="content"></logic:redirect>
</body>
</html>
content.xml:
<!DOCTYPE html>
<html>
<head>
<title>content.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h2>Save you from anything 07</h2>
</body>
</html>
这里,可以看出由/jsp/sd.jsp变为/jsp/content.html。明显是客户端跳转。
四.Html标签
这个Html自带的标签很类似。
那么我们就来一张JSP就知道了,很多Struts Html库中标签的功能,其实和Html本身的标签真
是大同小异。
se.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>se.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
%>
<html:form action="/jsp/simple.do" method="post">
姓名:<html:text property="name"></html:text>
<br>
密码:<html:password property="password"></html:password>
<br>
性别:<html:radio property="sex" value="男">男</html:radio>
<html:radio property="sex" value="女">女</html:radio>
<br>
语言:<html:checkbox property="languages" value="Java">Java</html:checkbox>
<html:checkbox property="languages" value="Android">Android</html:checkbox>
<html:checkbox property="languages" value="C++">C++</html:checkbox>
<html:checkbox property="languages" value="C#">C#</html:checkbox>
<br>
<br>
简介:<html:textarea property="note" cols="30" rows="3"></html:textarea>
<br>
<html:hidden property="id" value="7" />
<html:submit value="提交"></html:submit>
<html:reset value="重置"></html:reset>
</html:form>
</body>
</html>
SimpleForm.java:
package com.zyy.struts.form;
import org.apache.struts.action.ActionForm;
public class SimpleForm extends ActionForm {
private String name;
private String password;
private String sex;
private String languages[];
private String note;
private int id;
public String[] getLanguages() {
return languages;
}
public void setLanguages(String[] languages) {
this.languages = languages;
}
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
SimpleAction.java:
package com.zyy.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.zyy.struts.form.SimpleForm;
public class SimpleAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setCharacterEncoding("UTF-8");
SimpleForm simple = (SimpleForm) form;
System.out.println("ID:" + simple.getId());
System.out.println("name:" + simple.getName());
System.out.println("password:" + simple.getPassword());
System.out.println("sex:" + simple.getSex());
System.out.print("languages:");
for (String language : simple.getLanguages()) {
System.out.print(language + " ");
}
System.out.println("");
System.out.println("note:" + simple.getNote());
return null;
}
}
修改struts-config.xml:
<struts-config>
<form-beans>
<form-bean name="simpleForm" type="com.zyy.struts.form.SimpleForm"></form-bean>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
<forward name="content" path="/jsp/content.html" />
</global-forwards>
<action-mappings>
<action path="/jsp/simple" attribute="simpleForm" input="/jsp/se.jsp"
name="simpleForm" scope="request" type="com.zyy.struts.action.SimpleAction">
</action>
</action-mappings>
<message-resources parameter="resource.MessageResources" />
</struts-config>
但是,提交后一定有中文的乱码(sex:??)。
所以我们做一个过滤器SetCharacterEncodingFilter:
package com.zyy.struts.filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SetCharacterEncodingFilter implements Filter {
private String encoding;
private FilterConfig filterConfig = null;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(this.encoding);
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter("encoding");
}
}
配置struts-config.xml文件:
添加如下内容:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.zyy.struts.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>