对JSP页面和Java Servlets重用变量名称的约定是什么? [英] What's the convention for reusing variable names for both JSP pages and 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屋!