JAVA基础(form表单的双向映射)

JAVA基础(form表单的双向映射),第1张

JAVA基础(form表单的双向映射),第2张


有时候,我们会选择不用struts等的html注释。其实在这种情况下,并不是一个好的处理方式,因为struts的标签不仅仅是相对于重复代码的封装,更多的是为了匹配自身一些功能的实现;
比如我们会问,为什么要用html标签?我们直接用html标签真的更方便吗?我们还是要花时间向他们学习,其实并不是这样。考试。大提示你看完下面的例子就明白了;
一个页面有两个页签,分别采用以上两种形式。我们在actionmapping的动作中配置它们;

我们可以看到,我们将范围设置为session,我们填写此表单以提交到新页面,然后跳回。如果我们使用struts的html标签,可以看到我们的表单自动填充了我们提交的值,而如果我们使用html标签,那么这个表单就是空,默认实现。这个
首先在页面上,脚本会将前台页面的form form对应到后台的actionForm,也就是前台和后台的对应。我们来看看是如何实现的:
在RequestProcessor的process的方法中,有以下两个代码:
Actionformform = processactionform(请求、响应、映射);
processPopulate(请求、响应、表单、映射);
protected ActionForm processActionForm(http servlet request请求,
HttpServletResponse响应,ActionMapping映射){
//生成新的actionform
actionform实例=
request utils . create ActionForm(请求,映射,模块配置,
servlet);
if(instance = = null){
return(null);
}
//在适当的范围内存储新实例
if(log . is debugenabled()){
log . debug("在范围内存储ActionForm bean实例" "
+mapping . get scope()+" ' under attribute key ' "
+mapping . get attribute()+" ' ");
}
/从ActionMapping中获取其范围,并将其保存在设置的范围内;
if("请求"。equals(mapping . get scope()){
request . set attribute(mapping . get attribute(),instance);
} else {
http session session = request . getsession();
session . set attribute(mapping . get attribute(),instance);
}
return(实例);
}
在createActionForm方法中,我们可以看到:
ActionForm实例=
/在其作用域中查找ActionForm对象,如果存在则重用,
查找ActionForm (request,attribute,mapping . get scope());
if((实例!= null)& & config . can reuse(instance)){
return(instance);
}
/如果不存在,重新生成一个新的;
return createActionForm(config,servlet);
在populate方法中有:
如果不是上传的文件,那么:
If(!is multipart){
names = request . getparameternames();
}
/获取此页面提交的参数
while(names . hasmorelements()){
string name =(string)names . nextelement();
stripped = name;
if(前缀!= null) {
if(!stripped . starts with(prefix)){
继续;
}
stripped = stripped . substring(prefix . length());
}
if(后缀!= null) {
if(!stripped.endsWith(后缀)){
continue;
}
stripped =
stripped . substring(0,stripped . length()-suffix . length());
}
Object参数= null
if(is multipart){
parameter value = multipart parameters . get(name);
} else {
parameter value = request . getparameter values(name);
}
//填充参数,除了“标准”struts属性
//如' org . Apache . struts . action . cancel '
if(!(stripped . starts with(" org . Apache . struts . ")){
properties . put(stripped,parameter value);
}
}
/将参数与actionForm的属性相匹配;形成页面数据与背景的对应关系;
try {
bean utils . populate(bean,properties);
} catch(Exception e){
throw new servlet Exception(" bean utils . populate ",e);
}最后{
if (multipartHandler!= null) {
//为我们的ActionForm设置多部分请求处理程序。
//如果bean不是ActionForm,那么早就应该抛出异常
//所以可以放心地假设我们的bean实际上是ActionForm。
((ActionForm) bean)。setmultiprequesthandler(multipart handler);
}
}
}
我们可以看到,直到现在,前台页面和后台数据是相互对应的。那么,如何在后台修改actionForm form的数据,让前台和后台的数据对应起来呢?就像我们刚开始的时候,actionForm是会话级的,我们仍然可以通过使用struts标签让它自动填充表单;
然后我们要分析struts的标签;
首先在页面上,我们会解析标签。表单控件要和背景对应,那么这个对应的单位就是表单。因此空之间的所有应用都包含在一个表单中;
公共类form tag扩展tag support
form据说在tag支持中,他为整个form和后台的对应做了大量的前期工作;
public int doStartTag()抛出JSP exception {
postback action = null;
//查找表单的范围、bean的名称和类型。
this . lookup();
//生成文本;html表单控件的;
string buffer results = new string buffer();
results . append(this . renderformstartelement());
results . append(this . render token());
//Put tagutils . getinstance()。Write (pageContext,results . tostring());
//将当前表单信息保存在pageContext
//下,以便它们之间的表单控件可以找到
/自己的表单和后台的actionForm在表单标记结束之前;
pageContext.setAttribute(常量。FORM_KEY,this,
PageContext。请求_范围);
this . initformbean();
return(EVAL _ BODY _ INCLUDE);
}
Lookup()
Protected Void Lookup()抛出JSP异常{
//先找到模块;
module config = tagutils . getinstance()。getModuleConfig(pageContext);
if(module config = = null){
JSP exception e =
new JSP exception(messages . getmessage(" formtag . collections "));
page context . set attribute(Globals。EXCEPTION_KEY,e,
PageContext。请求_范围);
扔e;
}
/要获取其标签的action属性,我们需要根据它在模块配置信息中找到
//对应的配置信息;
String calcAction = this . action;
//如果未指定操作,则使用原始请求uri
If(this . action = = null){
http servlet request request =
(http servlet request)pagecontext . get request();
postback action =
(String)request . get attribute(Globals。原创_ URI _ KEY);
String prefix = moduleconfig . get prefix();
if (postbackAction!= null & & prefix . length()> 0 & & postback action . starts with(prefix))
{
postback action = postback action . substring(prefix . length());
}
calcAction = postbackAction;
} else {
//找到对应的动作配置信息;
action config action config = module config . findactionconfigid(this . action);
if (actionConfig!= null){
this . action = action config . getpath();
calcAction = this . action;
}
}
servlet =
(action servlet)pagecontext . getservletcontext()。getAttribute(全局。ACTION _ SERVLET _ KEY);
//查找我们将提交给
String mapping name =
tagutils . getinstance()的操作映射。getActionMappingName(calcAction);
//获取了actionMapping信息;
mapping =(action mapping)module config . findactionconfig(mapping name);
if(mapping = = null){
JSP exception e =
new JSP exception(messages . getmessage(" formtag . mapping ",
mapping name));
page context . set attribute(Globals。EXCEPTION_KEY,e,
PageContext。请求_范围);
扔e;
}
/获取了表单的配置信息;
formbean config formbean config =
module config . findformbean config(mapping . getname());
if(formBeanConfig = = null){
JSP exception e = null;
if(mapping . getname()= = null){
e = new JSP exception(messages . getmessage(" form tag . name ",calcAction));
} else {
e = new JSP exception(messages . getmessage(" formtag . form bean ",
mapping.getName()、calc action));
}
page context . set attribute(Globals。EXCEPTION_KEY,e,
PageContext。请求_范围);
扔e;
}
bean name = mapping . get attribute();
bean scope = mapping . get scope();
bean type = formbeanconfig . gettype();
}
initFormBean:表单对象已初始化;
protected void initFormBean()
抛出JSP exception {
int scope = PageContext。SESSION _ SCOPE
if("请求"。equalsIgnoreCase(bean scope)){
scope = PageContext。请求_范围;
}
Object bean = pagecontext . get attribute(bean name,scope);
if (bean == null) {
//新的和改进的-使用操作映射
bean =
request utils . createactionform((http servlet request)pageContext
中的值。getRequest(),mapping,moduleConfig,servlet);
if(ActionForm的bean instance){
((ActionForm)bean)。reset(mapping,
(http servlet request)pagecontext . get request());
}
if(bean = = null){
throw new JSP exception(messages . getmessage(" formtag . create ",
bean type));
}
pagecontext . set attribute(bean name,bean,scope);
}
pagecontext . set attribute(常量。BEAN_KEY,BEAN,
PageContext。请求_范围);
}
然后就是解析之类的表单控件:
public class text tag扩展basefieldtag {
/* *
*构造这个标签的新实例。
*/
公共文本标记
this . type = " text ";
doReadonly = true;
}
} public int do starttag()抛出JSP异常{
//将html写入页面;
TagUtils.getInstance()。write(this.pageContext,this . renderinputelement());
return(EVAL _ BODY _ TAG);
}
受保护的String renderInputElement()
throws JSP exception {
String buffer results = new String buffer(" prepare attribute(results," type ",this . type);
prepareAttribute(结果," name ",prepare name());
prepareAttribute(results," accesskey ",getAccesskey());
prepareAttribute(results," accept ",get accept());
prepareAttribute(results," maxlength ",get maxlength());
prepareAttribute(results," size ",get cols());
prepareAttribute(results," tabindex ",getTabindex());
//在这里,你会去后台对应的actionForm获取值;
//将后台数据推送到前台;
prepareValue(结果);
results . append(this . prepare eventhandlers());
results . append(this . prepare styles());
prepareOtherAttributes(结果);
results . append(this . getelementclose());
返回results . tostring();
}
protected void prepare value(string buffer results)
throws JSP exception {
results . append(" value = \ ");
//如果设置了值的属性;然后给他看;
if(值!= null){
results . append(this . format value(value));
//如果没有,从后台数据中获取;
} else if (redisplay ||!“密码”。equals(type)){
Object value =
tagutils . getinstance()。lookup(页面上下文,名称,属性,空);
results . append(this . format value(value));
}
results . append(“”);
}
公共对象lookup(PageContext pageContext,String name,String property,
stringscope)从页面中抛出JSP exception {
/Get bean
Object bean = lookup(PageContext,name,scope);
if(bean = = null){
JSP exception e = null;
if(scope = = null){
e = new JSP exception(messages . getmessage(" lookup . bean . any ",name));
} else {
e = new JSP exception(messages . getmessage(" lookup . bean ",name,
scope));
}
save exception(pageContext,e);
扔e;
}
if(property = = null){
return bean;
}
try {
/从表单中获取属性mark的值,返回,输出到页面
//;
return property utils . getproperty(bean,property);
} catch(IllegalAccessException e){
save exception(pageContext,e);
throw new JSP exception(messages . getmessage(" lookup . access ",
property,name));
} catch(IllegalArgumentException e){
save exception(pageContext,e);
throw new JSP exception(messages . getmessage(" lookup . argument ",
property,name));
} catch(InvocationTargetException e){
Throwable t = e . gettargetexception();
if(t = = null){
t = e;
}
save exception(pageContext,t);
throw new JSP exception(messages . getmessage(" lookup . target ",
property,name));
} catch(nosuchmethodeexception e){
save exception(pageContext,e);
String bean name = name;
//名称默认为内容。BEAN_KEY如果
//一个输入标记没有指定名称。因此,在该项下查找bean,并使用
//它的类名作为异常消息。
if(常量。BEAN _ key . equals(name)){
Object obj = pagecontext . find attribute(常量。BEAN _ KEY);
if (obj!= null){
bean name = obj . getclass()。getName();
}
}
throw new JSP exception(messages . getmessage(" lookup . method ",
property,bean name));
}
}
最后,将“密封”表单元素
public int doendtag()抛出JSP exception {
/并从页面上下文中移除当前表单的标签
pagecontext . remove attribute(constants . bean _ key,
pagecontext . request _ scope);
pagecontext . remove attribute(常量。FORM_KEY,
PageContext。请求_范围);
//关闭表单标记;
string buffer results = new string buffer(" ");
//显示当前焦点;
if (this.focus!= null){
results . append(this . renderfocus JavaScript());
}
/在页面上显示;
JSP writer writer = pagecontext . getout();
try {
writer . print(results . tostring());
} catch(io exception e){
throw new JSP exception(messages . getmessage(" common . io ",e . tostring());
}
postback action = null;
//继续执行此页面;
return(EVAL _ PAGE);
}
从上面我们可以看出,struts中不支持表单嵌套,表单的顺序定义还是可以的;这不会造成pageContext的页面标记被覆盖等现象;

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » JAVA基础(form表单的双向映射)

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情