DispatchAction:
1.DispatchAction是什么?有什么用?
2.工作流程
3.语法
4.引例
DispatchAction是什么?有什么用?
看一个问题,一个表单可能有多处用途,比如说增加,修改,如果我们根据这个需求写程序
需要写两个action类,因为Action类的方法比较单一(execute()),一个功能就对应一个
action,分的太细,如果是一个用例的,做一个表单,一系列操作这个表单的action,这种情况我们希望只写一个action,去完成同一用例,一个表单所对应的一系列功能
DispatchAction可以完成这个任务
工作流程:
请求(表单参数and参数值)--C控制器(requestProessor)--xml(得到配置信息,其中读取url对应的parameter,parameter里面指定了表单参数,找到了参数就找到了该参数的值)--action(requestProessor执行execute()方法,根据参数值,调用和参数值名字一样的方法)
语法:
1.在jsp中要在表单url后面添加参数和参数值
url?canshu='value'
2.在配置文件中的action中添加<parameter=参数>
引例:
程序目标:用户输入名字和密码,当提交表单的时候,显示action处理了哪个方法
程序逻辑:
1.jsp
2.action
3.actionform
4.struts-config.xml
login.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="html" uri="/tags/struts-html" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="/yuchen-struts/login.do?method=add" method="post">
userName: <input type="text" name="user" /><br>
password: <input type="password" name="pwd" /><br>
<input type="submit" />
</form>
</body>
</html>
success.jsp:
<%=request.getAttribute("success")%><br>
<h1>success!</h1>
LoginAction.java:
package yuchen.dispatchation;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
public class LoginAction extends DispatchAction{
public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
request.setAttribute("success", "this is add");
return mapping.findForward("success");
}
public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
request.setAttribute("success", "this is delete");
return mapping.findForward("success");
}
}
配置文件:
在<action>加上parameter="method"
LookDispatchAction:
1.什么是LookDispatchAction?有什么用?
2.工作流程
4.引例
什么是LookDispatchAction?有什么用?
通过这个Action抽象类继承DispatchAction,它的相应方法的执行由 ActionMapping中parameter属性决定。它适合在一个form中有很多按钮,按不同的按钮则执行不同的操作
工作流程:
原理:
这个类利用了资源文件
提交按钮<-->key<-->方法名:LookDispatchAction的getKeyMethodMap()为key<-->方法名做关联,利用的是map,在jsp页面中提交按钮参数与key做关联,然后在配置文件中的action里<parameter=提交按钮参数>
引例:
程序目标:作两个提交按钮,分别被action类中的相应的方法处理
jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="html" uri="/tags/struts-html" %>
<%@taglib prefix="bean" uri="/tags/struts-bean" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="/yuchen-struts/login.do" method="post">
userName: <input type="text" name="user" /><br>
password: <input type="password" name="pwd" /><br>
<html:submit property="ac">
<bean:message key="submit.add"/>
</html:submit>
<html:submit property="ac">
<bean:message key="submit.delete"/>
</html:submit>
</form>
</body>
</html>
success.jsp:
<%=request.getAttribute("submit")%><br>
<h1>success!</h1>
LoginAction.java:
package yuchen.lookaction;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.LookupDispatchAction;
public class LoginAction extends LookupDispatchAction{
protected Map getKeyMethodMap() {
Map map=new HashMap();
//key and method mapping
map.put("submit.add", "add");
map.put("submit.delete", "delete");
return map;
}
public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse
response) throws Exception {
// TODO Auto-generated method stub
request.setAttribute("submit", "this is delete");
return mapping.findForward("success");
}
public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse
response) throws Exception {
// TODO Auto-generated method stub
request.setAttribute("submit", "this is add");
return mapping.findForward("success");
}
}
配置文件:
<action-mappings>
<action
name="dyLoginForm"
scope="request"
path="/login"
parameter="ac"
type="yuchen.lookaction.LoginAction">
<forward name="success" path="/pages/success.jsp"/>
</action>
资源文件:
submit.add=add
submit.delete=delete
MappingDispatchAction:
1.什么是MappingDispatchAction?有什么用?
2.工作流程
3.引例
什么是MappingDispatchAction?有什么用?
使用这个aciton,对程序员来讲更有亲和力,一个url对应一个MappingDispatchAction类的一个方法,这样程序员读程序更加清楚,实现的功能都是一样的
工作流程:
请求--C:得到请求中的url--读配置文件:得到url对应的action,找到parameter参数中的方法名,调用vaildate方法,这个方法根据url对应的parameter的方法名,去掉action类中的同名方法
语法:
jsp:url
struts-config.xml:url对应action(paramter="action类的方法名")
action:得到了paramter中的值,调用和这个值名字相同的方法
引例:
login.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1><%=request.getAttribute("save") %></h1>
<table>
<td><a href="/yuchen-struts/add.do">add</a></td>
<td><a href="/yuchen-struts/delete.do">delete</a></td>
<td><a href="/yuchen-struts/motify.do">motify</a></td>
<td><a href="/yuchen-struts/list.do">list</a></td>
</table>
</body>
</html>
success.jsp:
<%=request.getAttribute("save") %>
<h1>Success!</h1>
action:
package yuchen.mappingDispatchAction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
public class MyMappingDispatchAction extends MappingDispatchAction{
public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse
response) throws Exception {
// TODO Auto-generated method stub
request.setAttribute("save", "this is add");
return mapping.findForward("success");
}
public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse
response) throws Exception {
// TODO Auto-generated method stub
request.setAttribute("save", "this is list");
return mapping.findForward("success");
}
public ActionForward motify(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse
response) throws Exception {
// TODO Auto-generated method stub
request.setAttribute("save", "this is motify");
return mapping.findForward("success");
}
public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse
response) throws Exception {
// TODO Auto-generated method stub
request.setAttribute("save", "this is delete");
return mapping.findForward("success");
}
}
配置文件:一个action对应一个url,url对应一个action类的一个方法,这几个action使用的是同一个action类
<action
path="/add"
parameter="add"
type="yuchen.mappingDispatchAction.MyMappingDispatchAction">
<forward name="success" path="/pages/success.jsp"/>
</action>
<action
path="/motify"
parameter="motify"
type="yuchen.mappingDispatchAction.MyMappingDispatchAction">
<forward name="success" path="/pages/success.jsp"/>
</action>
<action
path="/delete"
parameter="delete"
type="yuchen.mappingDispatchAction.MyMappingDispatchAction">
<forward name="success" path="/pages/success.jsp"/>
</action>
<action
path="/list"
parameter="list"
type="yuchen.mappingDispatchAction.MyMappingDispatchAction">
<forward name="success" path="/pages/login.jsp"/>
</action>
validator Framework:
1.什么是validator?有什么用?
2.工作原理
3.需要的资源
4.在struts中配置validator插件
5.validator-rules.xml:
6.validator.xml
7.针对formbean作验证:静态formbean and 动态formbean
8.针对formbean存在的问题
9.针对action做验证:静态,动态
10.针对formbean的客户端验证
11.针对action的客户端验证
什么是validator?有什么用?
表单验证框架,看两个问题,我们以前使用的验证是在ActionForm的子类中重写validate方法,在这里面写验证逻辑代码,这样会产生两个问题,一是当验证逻辑改变的时候,需要修改ActionForm类,二是因为一个应用中可能有多个表单,每个表单的验证逻辑都不一定一样,这样需要再写多个ActionForm,造成代码冗余,如果要修改验证逻辑,维护起来也是很麻烦的,但是使用表单验证框架可以解决这些问题
工作原理:
请求--C控制器:控制器读Struts-config.xml文件,安装validator框架,查看配置文件中url对应的action中需不需要验证,如果需要,读取validator.xml文件,找到form,调用验证类的验证方法来验证form,这些方法在validator-rules.xml中定义
需要的资源:
1.两个jar包,jakarta-oro.jar:处理文本的类 commons-valdator.jar:包含验证类
这两个包struts自带
2.配置文件:validator-rules.xml and validator.xml 在WEB-INF:目录下
在struts中配置validator插件:
struts-config.xml:
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
<plug-in>:定义插件的标签
className:插件的类
value:配置文件
自动配好的,一般情况下不需要改动
validator-rules.xml:
1.有什么用?
在这里定义validator的表单验证规则:如,不能为空验证等
2.语法:
<form-validation>根标签
<global>全局标签
//下面是定义一个表单验证规则
name:这个验证功能的名字
classname:表单验证规则类
method:使用的是验证规则类的哪个方法
methodParams:方法参数
msg:key,当验证没通过,去资源文件找这个key对应的value
<validator name="required" //required 必须
classname="org.apache.struts.validator.FieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>
validator.xml:
1.这个文件是什么?有什么用?
如果我们想给以个表单加上验证的话,需要在这里为表单添加验证规则
也就是说,要说明哪个form的哪些属性,要做什么功能的验证
2.语法
<formset>:form集合标签,这里面的都是定义表单需要验证的功能
<form>name:需要验证的formbean,但是如果是针对action的验证,这个写的是action对应的path,如:/login
<field>:表示表单中控件的验证规则
<field property>:需要验证的表单中的控件
<field depends>:验证规则名,这个验证规则是在validator-rules.xml定义的
<msg>:替换了validator-rules.xml中的msg,name属性:验证规则 key:资源文件中的key,这个验证规则如果验证
失败的话,那么显示这个key指向的value
<arg>:在资源文件中key and value可以带上参数,这个参数由程序员在配置validator.xml文件中设置
<arg name>:验证规则,意思是为哪个验证规则做字符串传递
<arg resource>:如果为false,那么传递的这个值直接为字面量
<var>:为arg的参数传递变量
<form name="logonForm">
<field
property="username"
depends="required">
<arg key="logonForm.username"/>
</field>
<field
property="password"
depends="required">
<arg0 key="logonForm.password"/>
<arg1 name="required" key="${var:mask}" resource="ture|false">
<var>
<var-name>mask</var-name>
<var-value>^[0 -9a -zA-Z]*$</var-value>
</var>
</field>
</form>
针对formbean作验证:静态formbean and 动态formbean:
1.开发步骤:
A.配置formbean:静态的继承ValidatorForm类,动态的在配置文件中配置
B.在配置文件中设置validate="true" input="url"
C.validator.xml:为需要验证的formbean设置验证规则
D.配置资源文件
2.引例:
login.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="html" uri="/tags/struts-html" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<html:errors/>
<form action="/yuchen-struts/login.do" method="post">
userName: <input type="text" name="user" /><br>
password: <input type="password" name="pwd" />
<input type="submit" />
</form>
</body>
</html>
LoginAction.java:
package yuchen.validator;
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;
public class LoginAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse
response) throws Exception {
// TODO Auto-generated method stub
return mapping.findForward("success");
}
}
LoginForm.java:
package yuchen.validator;
import org.apache.struts.validator.ValidatorForm;
public class LoginForm extends ValidatorForm{
private String user;
private String pwd;
public LoginForm() {
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
struts-config.xml:
<form-bean
name="LoginForm"
type="yuchen.validator.LoginForm">
</form-bean>
<action
input="/pages/login.jsp"
name="LoginForm"
validate="true"
scope="request"
path="/login"
type="yuchen.validator.LoginAction">
<forward name="success" path="/pages/login.jsp"/>
</action>
validation.xml:
<form name="LoginForm">
<field
property="user"
depends="required">
<arg key="login.user"/>
</field>
<field
property="pwd"
depends="required">
<arg key="login.pwd"/>
</field>
</form>
修改资源文件:
login.user=user
login.pwd=password
引例2:动态formbean的表单验证
引用上面的login.jsp等
修改struts-config.xml:
<form-bean
name="LoginForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="user" type="java.lang.String" />
<form-property name="pwd" type="java.lang.String" />
</form-bean>
针对formbean作验证所存在的问题:
一个表单对应多个操作,比如,增加,删除等,这样他们所需要的验证规则也不一样
如果我们沿用一个表单一个action一个formbean的原则,那么我们用针对formbean的表单验证方式,就无法实现这样的需求,因为
在设置表单验证规则的时候,我们对应的是一个formbean,使用针对action的方式作验证可以解决这个问题
针对action做验证:静态,动态
1.什么叫针对action做验证?有什么用?
为一个表单对应的多个action设置不同的验证规则,因为一个表单可能在不同的情况下需要不同的验证规则
2.开发步骤
A.配置formbean:静态的继承ValidatorActionForm类,动态的在配置文件中配置org.apache.struts.validator.DynaValidatorActionForm类
B.在配置文件中设置validate="true" input="url"
C.validator.xml:为需要验证的formbean设置验证规则<form name="action的path">
D.配置资源文件
3.引例:静态
引用上面的login.jsp等文件
修改LoginForm.java:
package yuchen.validator.action.statics;
import org.apache.struts.validator.ValidatorActionForm;
public class LoginForm extends ValidatorActionForm{
private String user;
private String pwd;
public LoginForm() {
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
struts-config.xml:
<action
input="/pages/login.jsp"
name="LoginForm"
validate="true"
scope="request"
path="/login"
type="yuchen.validator.action.statics.LoginAction">
<forward name="success" path="/pages/login.jsp"/>
</action>
<action
input="/pages/login.jsp"
name="LoginForm"
validate="true"
scope="request"
path="/login2"
type="yuchen.validator.action.statics.LoginAction">
<forward name="success" path="/pages/login.jsp"/>
</action>
validator.xml:
<form name="/login">
<field
property="user"
depends="required,minlength,maxlength">
<arg0 key="login.user"/>
<arg1 key="${var:minlength}" name="minlength" resource="fales"></arg1>
<arg1 key="${var:maxlength}" name="maxlength" resource="fales"></arg1>
<var><var-name>minlength</var-name><var-value>2</var-value></var>
<var><var-name>maxlength</var-name><var-value>10</var-value></var>
</field>
<field
property="pwd"
depends="required,minlength,maxlength">
<arg0 key="login.pwd"/>
<arg1 key="${var:minlength}" name="minlength" resource="fales"></arg1>
<arg1 key="${var:maxlength}" name="maxlength" resource="fales"></arg1>
<var><var-name>minlength</var-name><var-value>2</var-value></var>
<var><var-name>maxlength</var-name><var-value>10</var-value></var>
</field>
</form>
<form name="/login2">
<field
property="user"
depends="required,minlength,maxlength">
<arg0 key="login.user"/>
<arg1 key="${var:minlength}" name="minlength" resource="fales"></arg1>
<arg1 key="${var:maxlength}" name="maxlength" resource="fales"></arg1>
<var><var-name>minlength</var-name><var-value>5</var-value></var>
<var><var-name>maxlength</var-name><var-value>12</var-value></var>
</field>
<field
property="pwd"
depends="required,minlength,maxlength">
<arg0 key="login.pwd"/>
<arg1 key="${var:minlength}" name="minlength" resource="fales"></arg1>
<arg1 key="${var:maxlength}" name="maxlength" resource="fales"></arg1>
<var><var-name>minlength</var-name><var-value>5</var-value></var>
<var><var-name>maxlength</var-name><var-value>12</var-value></var>
</field>
</form>
4.引例:动态
struts-config.xml:
<form-bean
name="LoginForm"
type="org.apache.struts.validator.DynaValidatorActionForm">
<form-property name="user" type="java.lang.String"></form-property>
<form-property name="pwd" type="java.lang.String"></form-property>
</form-bean>
<action
input="/pages/login.jsp"
name="LoginForm"
validate="true"
scope="request"
path="/login"
type="yuchen.validator.form.dy.LoginAction">
<forward name="success" path="/pages/login.jsp"/>
</action>
<action
input="/pages/login.jsp"
name="LoginForm"
validate="true"
scope="request"
path="/login2"
type="yuchen.validator.form.dy.LoginAction">
<forward name="success" path="/pages/login.jsp"/>
</action>
validator.xml:
<form name="/login">
<field
property="user"
depends="required,minlength,maxlength">
<arg0 key="login.user"/>
<arg1 key="${var:minlength}" name="minlength" resource="fales"></arg1>
<arg1 key="${var:maxlength}" name="maxlength" resource="fales"></arg1>
<var><var-name>minlength</var-name><var-value>5</var-value></var>
<var><var-name>maxlength</var-name><var-value>12</var-value></var>
</field>
<field
property="pwd"
depends="required,minlength,maxlength">
<arg0 key="login.pwd"/>
<arg1 key="${var:minlength}" name="minlength" resource="fales"></arg1>
<arg1 key="${var:maxlength}" name="maxlength" resource="fales"></arg1>
<var><var-name>minlength</var-name><var-value>2</var-value></var>
<var><var-name>maxlength</var-name><var-value>10</var-value></var>
</field>
</form>
<form name="/login2">
<field
property="user"
depends="required,minlength,maxlength">
<arg0 key="login.user"/>
<arg1 key="${var:minlength}" name="minlength" resource="fales"></arg1>
<arg1 key="${var:maxlength}" name="maxlength" resource="fales"></arg1>
<var><var-name>minlength</var-name><var-value>5</var-value></var>
<var><var-name>maxlength</var-name><var-value>12</var-value></var>
</field>
<field
property="pwd"
depends="required,minlength,maxlength">
<arg0 key="login.pwd"/>
<arg1 key="${var:minlength}" name="minlength" resource="fales"></arg1>
<arg1 key="${var:maxlength}" name="maxlength" resource="fales"></arg1>
<var><var-name>minlength</var-name><var-value>5</var-value></var>
<var><var-name>maxlength</var-name><var-value>12</var-value></var>
</field>
</form>
针对formbean的客户端验证:
1.客户端和服务器端表单验证的区别
服务器端真正保证安全,客户端减少服务器端的一些复杂度
2.语法:
利用struts标签,在jsp页面上写验证程序,也可以直接使用javascript
<html:form method="post" action="url" οnsubmit="return validate(formbean的名字)(this);">
"return validate(formbean的名字)(this);"这句话将调用javascript代码
<html:javascript forname="formbean的名字">生成javascript代码
针对formbean的客户端验证:
语法:
<html:form method="post" action="url" οnsubmit="return validate(formbean的名字)(this);">
"return validate(formbean的名字)(this);"这句话将调用javascript代码
<html:javascript forname="action对应的path">生成javascript代码
Tiles framework:
1.什么是Tiles framework?有什么用?
2.内容
3.引例
什么是Tiles framework?有什么用?
jsp模版,好处就是可以降低代码的复杂度,可以重用
内容:
layout,类库,配置文件xml,正式定义的组件(在配置文件中定义),非正式定义的组件(jsp页面)
配置Tiles插件:
在struts-config.xml中配置<plogin>
layout:
什么是layout?这个文件中定义的是组件的位置,本身也是个jsp页面
<tiles:insert>:
类似于include,但是他可以使得布局与组件分离,布局可以重用
A:include B,A把B的内容加进来
A:<tiles:insert>B,C,D.... 在A这个页面,使用这个语法,将B,C,D这些页面插入到布局页面指定的位置上,最终形成成品A页面,A就是成品,布局页面就是模子,B,C,D等页面就是组件
引例:
1.创建layout布局页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="tiles" uri="/tags/struts-tiles" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><tiles:getAsString name="title"></tiles:getAsString></title>
</head>
<body>
<table>
<tr>
<td colspan="2"><tiles:insert attribute="head" /></td>
</tr>
<tr>
<td><tiles:insert attribute="menu" /></td>
<td><tiles:insert attribute="body" /></td>
</tr>
<tr>
<td colspan="2"><tiles:insert attribute="footer" /></td>
</tr>
</table>
</body>
</html>
2.创建组件页面 body.jsp,foter.jsp,head.jsp,menu.jsp
body.jsp:<h1>body.jsp</h1>
footer.jsp:<h1>footer.jsp</h1>
head.jsp:<h1>head.jsp</h1>
menu.jsp:<h1>menu.jsp</h1>
3.创建成品页面
Test.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="tiles" uri="/tags/struts-tiles" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<tiles:insert page="/pages/tiles/layout/layout.jsp" flush="true" >
<tiles:put name="title" value="tiles basic page" />
<tiles:put name="head" value="/pages/tiles/head.jsp" />
<tiles:put name="footer" value="/pages/tiles/footer.jsp" />
<tiles:put name="menu" value="/pages/tiles/menu.jsp" />
<tiles:put name="body" value="/pages/tiles/body.jsp" />
</tiles:insert>
</body>
</html>
definitions:
definitions是什么意思?有什么用?
网站有些网页可能只有body部分需要改动,如果用以前的方式做的话,代码会产生冗余,使用definitions可以加少代码的冗余有个特性,definitions在tiles-defs.xml定义,有名字,所以可以继承,并且可以重写其中的组件
开发步骤:
1.在tiles-defs.xml中定义definitions
<definition name="def" path="/pages/tiles/layout/layout.jsp">
<put name="title" value="Page Title" />
<put name="head" value="/pages/tiles/head.jsp" />
<put name="footer" value="/pages/tiles/footer.jsp" />
<put name="menu" value="/pages/tiles/menu.jsp" />
<put name="body" value="/pages/tiles/body.jsp" />
</definition>
2.写jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="tiles" uri="/tags/struts-tiles" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<tiles:insert definition="def" flush="true">
<tiles:put name="body" value="/pages/tiles/body1.jsp"/>
</tiles:insert>
</body>
</html>
body1.jsp:
<h1>body1 body1</h1>