对JSP页面和Java Servlets重用变量名称的约定是什么? [英] What's the convention for reusing variable names for both JSP pages and Java Servlets?

查看:82
本文介绍了对JSP页面和Java Servlets重用变量名称的约定是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常会教会使用变量来复制具有相同值的实例,以便可以用一行代码更改来更新所有实例。这是基本的编程。

目前,我正在编写一个与Java servlet交互的JSP页面,但输入表单的名称是以纯文本编写的,如name =formInput1和I必须在纯文本中使用res.getParamter(formInput1)在我的servlet代码中复制名称。



这是一般的不好的做法,有一个更好的这样做的方式,但我的谷歌搜索没有让我任何地方生产力。有人知道吗?我想在我的JSP页面和servlet java代码之间共享变量名称!什么是最好的方式来做到这一点?

解决方案

简单的方法是为常量设置一个类

  package com.my.pakage; 

public class FormConstants {
public static final String formInput1 =formInput1;

}

然后在jsp中进行导入:

 <%@ page import =com.my.pakage.FormConstants%> 

像这样使用它:

 < input type =textname =<%= FormConstants.formInput1%>> 

并且通常在您的Java文件中:

  import com.my.pakage.FormConstants 


Normally it's taught to use variables in order to duplicate intances of the same value so that all can be updated with one line of code change. This is basic programming.

Currently I'm writing a JSP page that interacts with a Java servlet but the names of the input forms are written in pure text such as name="formInput1" and I have to duplicate the name in pure text with res.getParamter("formInput1") inside my servlet code.

This is bad practice in general and there HAS to be a better way to go about doing it but my google searches aren't getting me anywhere productive. Does anyone know? I want to share variable names between my JSP pages and servlet java code! What's the best way to do this?

解决方案

An easy way would be to have a Class for constants

package com.my.pakage;

public class FormConstants {
    public static final String formInput1="formInput1";

}

and then do the imports in jsp:

<%@ page import="com.my.pakage.FormConstants" %>

to use it like this:

<input type="text" name="<%=FormConstants.formInput1%>">

and normally in your Java files:

import com.my.pakage.FormConstants

这篇关于对JSP页面和Java Servlets重用变量名称的约定是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆