我有多个以“问题.....”形式命名的文本字段。如何从JSP页面获取以“问题”开头的所有参数到Action?
答案 0 :(得分:70)
<%@ page import = "java.util.Map" %>
Map<String, String[]> parameters = request.getParameterMap();
for(String parameter : parameters.keySet()) {
if(parameter.toLowerCase().startsWith("question")) {
String[] values = parameters.get(parameter);
//your code here
}
}
答案 1 :(得分:30)
即使这是一个老问题,我今天也必须做类似的事情,但我更喜欢JSTL:
<c:forEach var="par" items="${paramValues}">
<c:if test="${fn:startsWith(par.key, 'question')}">
${par.key} = ${par.value[0]}; //whatever
</c:if>
</c:forEach>
答案 2 :(得分:6)
这应该打印出所有以“Question”开头的参数。
<html><body>
<%@ page import = "java.util.*" %>
<b>Parameters:</b><br>
<%
Enumeration parameterList = request.getParameterNames();
while( parameterList.hasMoreElements() )
{
String sName = parameterList.nextElement().toString();
if(sName.toLowerCase.startsWith("question")){
String[] sMultiple = request.getParameterValues( sName );
if( 1 >= sMultiple.length )
// parameter has a single value. print it.
out.println( sName + " = " + request.getParameter( sName ) + "<br>" );
else
for( int i=0; i<sMultiple.length; i++ )
// if a paramater contains multiple values, print all of them
out.println( sName + "[" + i + "] = " + sMultiple[i] + "<br>" );
}
}
%>
</body></html>
答案 3 :(得分:1)
localhost:8080/esccapp/tst/submit.jsp?key=datr&key2=datr2&key3=datr3
<%@page import="java.util.Enumeration"%>
<%
Enumeration in = request.getParameterNames();
while(in.hasMoreElements()) {
String paramName = in.nextElement().toString();
out.println(paramName + " = " + request.getParameter(paramName)+"<br>");
}
%>
key = datr
key2 = datr2
key3 = datr3
答案 4 :(得分:0)
最快的方法应该是:
<%@ page import="java.util.Map" %>
Map<String, String[]> parameters = request.getParameterMap();
for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
if (entry.getKey().startsWith("question")) {
String[] values = entry.getValue();
// etc.
请注意,您无法执行:
for (Map.Entry<String, String[]> entry :
request.getParameterMap().entrySet()) { // WRONG!
原因解释here。
答案 5 :(得分:0)
form-inline