平常我们使用struts会定义form,写Action,设置struts-config.xml文件,然后页面的数据是以form对象传给Action,然后调用Service层,完成业务,再返回struts-config.xml配置的页面。
而JPetStore-5.0的struts不同,例如:
页面是这样的
<html:form method="post" action="/shop/editAccount.shtml">
<html:hidden name="accountBean" property="validation" value="edit"/>
<html:hidden name="accountBean" property="username"/>
<h3>User Information</h3>
<table>
<tr>
<td>User ID:</td><td><bean:write name="accountBean" property="username"/></td>
</tr><tr>
<td>New password:</td><td><html:password name="accountBean" property="password"/></td>
</tr><tr>
<td>Repeat password:</td><td><html:password name="accountBean" property="repeatedPassword"/></td>
</tr>
</table>
<%@ include file="IncludeAccountFields.jsp" %>
<input type="submit" name="submit" value="Save Account Information"/>
</html:form>
struts-config.xml的配置文件是这样的
<form-beans>
<form-bean name="accountBean" type="com.ibatis.jpetstore.presentation.AccountBean"/>
</form-beans>
<action path="/shop/editAccount" type="org.apache.struts.beanaction.BeanAction"
name="accountBean" scope="session"
validate="true" input="/account/EditAccountForm.jsp">
<forward name="success" path="/shop/index.shtml"/>
</action>
AccountBean是这样的:
public String editAccount() {
try {
accountService.updateAccount(account);
account = accountService.getAccount(account.getUsername());
myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
return SUCCESS;
} catch (Exception e) {
throw new BeanActionException ("There was a problem updating your Account Information. Cause: "+e, e);
}
}
jsp把页面form提交给了/shop/editAccount.shtml,其实所有的数据都提到了一个类里org.apache.struts.beanaction.BeanAction,这个类其实就是一个我们平常写的Action,里面是这样的:
public final ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
String forward = SUCCESS_FORWARD;
try {
if (!(form instanceof BaseBean)) {
if (form != null) {
throw new BeanActionException("The form for mapping '" + mapping.getPath() + "' named '" + mapping.getName() + "' was not an instance of BaseBean. BeanAction requires an BaseBean instance.");
} else {
throw new BeanActionException("The form for mapping '" + mapping.getPath() + "' named '" + mapping.getName() + "' was null. BeanAction requires an BaseBean instance.");
}
}
BaseBean bean = (BaseBean) form;
ActionContext.initCurrentContext(request, response);
if (bean != null) {
// Explicit Method Mapping
Method method = null;
String methodName = mapping.getParameter();
if (methodName != null && !NO_METHOD_CALL.equals(methodName)) {
try {
method = bean.getClass().getMethod(methodName, null);
synchronized (bean) {
forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
}
} catch (Exception e) {
throw new BeanActionException("Error dispatching bean action via method parameter ('" + methodName + "'). Cause: " + e, e);
}
}
// Path Based Method Mapping
if (method == null && !NO_METHOD_CALL.equals(methodName)) {
methodName = mapping.getPath();
if (methodName.length() > 1) {
int slash = methodName.lastIndexOf("/") + 1;
methodName = methodName.substring(slash);
if (methodName.length() > 0) {
try {
method = bean.getClass().getMethod(methodName, null);
synchronized (bean) {
forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
}
} catch (Exception e) {
throw new BeanActionException("Error dispatching bean action via URL pattern ('" + methodName + "'). Cause: " + e, e);
}
}
}
}
}
} catch (Exception e) {
forward = "error";
request.setAttribute("BeanActionException", e);
}
return mapping.findForward(forward);
}
也就是说,在这个类里,程序会取配置文件里的 parameter 属性是否定义,如果定义了,则就用反射调用com.ibatis.jpetstore.presentation.AccountBean里的相应方法;如果没有定义,则就会去配置文件里的path="/shop/editAccount" 属性,取最后一个斜杠后的单词,再用反射调用相应方法;如个parameter 定义了且是parameter="*"则不调用任何一个方法,直接返回,配置的返回页面。
分享到:
相关推荐
ibaits 示例,供大家分享,哈哈哈哈哈哈哈哈 哈哈啊哈哈和哈哈哈哈和
学习iBatis,快速入门, 源代码包括宠物商店
学习iBatis的必须工具,另有iBatjs经典应用JPetStore源代码
采用STRUTS2架构写的PETSTORE
使用了mybatis的jpetstore-6
NULL 博文链接:https://xredman.iteye.com/blog/663840
jpetstore-3-1-1
iBATIS_JPetStore-4.0.5
jpetstore-4.0.3
jpetstore-ibatis
Jpetstore
用spring改造的jpetstore4.0,适用于jdk7+,tomcat6+。mysql数据库
MyBatis JPetStore JPetStore 6是基于MyBatis 3,Spring 4和Stripes构建的完整Web应用程序。 必需品 您可能想了解的其他版本 ...$ cd jpetstore-6 $ ./mvnw clean package 启动Tomcat服务器并部署We
jpetstore-6-spring-data-mongodb 该项目是使用Spring Data MongoDB将原始从RDBMS / MyBatis移植到NoSQL / MongoDB的练习。 使用Maven运行: git clone ...
jpetstore-6-vaadin-spring-boot该项目不再维护。此项目是将原始移植到Vaadin和Spring Boot的练习。分行大师-Java 11,Vaadin 8.9.0(正在供货) vaadin-v8-java-v8 -Java 8,Vaadin 8.3.0(旧版) vaadin-v8-with-...
jPetStore-SerenityBDD ##如何在本地运行JpetStore *要在本地运行JpetStore,请从获取github网址git clone :mybatis / jpetstore-6.git *要构建war文件./mvnw清洁软件包--Mac mvn清洁软件包-Windows(检查目标...
该程序是jpetstore宠物店程序,放在tomcat的webapps的目录下就能运行。修改jdbc.properties的数据库密码,先将两个sql文件导入mysql数据库中。
想用什么数据库直接在database.properties文件里面修改连接数据库代码,我用的是Mysql数据库,sql文件在ddl.mysql包里面,依次导入jpetstore-mysql-schema.sql,jpetstore-mysql-dataload.sql,jpetstore-mysql-...