今天介绍XingXingMVC的表单事件处理.
还是使用昨天的web工程 FirstDemo,web.xml 配置不变.XingXingMVC 的优点就是配置很少,呵呵.
建立一个页面,因为要提交表单,所以要有<form>,与其他mvc不同的是,XingXingMVC 不要求开发人员指定form元素的action.
页面名称为regist.jsp,完整路径:usermanage/regist.jsp,页面代码如下
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form> <!-- 这里无需指明action,mvc自己会找到后台对应的form类 -->
<p><label>用户名称</label><input type="text" name="txtName"/></p>
<p><label>邮件地址</label><input type="text" name="txtMail"/></p>
<p><input type="submit" name="btnOK" value="提 交"/></p>
</form>
<hr/>
<p>提交结果</p>
<p>用户名称:${userName}</p>
<p>邮件地址:${mail}</p>
<hr/>
<p><a href="../index.jsp">返回主页</a></p>
</body>
</html>
对应的后台form类为forms.usermange.regist.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package forms.usermanage;
import java.util.Map;
/**
*
* @author hicen
*/
public class regist
{
// //本页面不需要在加载时候做处理,所以onload事件可以不用写.
// public void onLoad(Map mapRequest,Map mapSession)
// {
//
// }
public void btnOK(Map mapRequest,Map mapSession)
{
String strName = gstr("txtName",mapRequest); //txtName为页面上input的name属性值
String strMail = gstr("txtMail",mapRequest);
mapRequest.put("userName", strName);//mapRequest相当于request的parameterMap,
//这个map原本只能读不能写,经过MVC包装后读写皆可.
mapRequest.put("mail", strMail);
}
private String gstr( String strKey,Map map)
{
String[] strArr = (String[]) map.get(strKey); //从request的parameterMap中得到的都是字符串数组.
if( null != strArr && strArr.length>0)
{
return strArr[0];
}
return null;
}
}