工程基于strut java
package addressbook.actions;/**
* <strong>SearchAction</strong> will take the search parameters
* specified by the user and create the Sql statement to be used
* by the appropriate forward.
*/
public final class SearchAction extends AbstActionBase { private Log log =
LogFactory.getLog(this.getClass().getName()); public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception { Locale locale = getLocale(request);
MessageResources messages = getResources(request); ActionMessages errors = new ActionMessages();
String name = ((SearchForm) form).getName();
String phone = ((SearchForm) form).getPhone();
String address=((SearchForm)form).getAddress(); if (!errors.isEmpty()) {
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
} String strSql = new String("SELECT * FROM " + Constants.TABLENAME + " WHERE "); if (!name.equals(""))
strSql = strSql + "name LIKE '"+ name +"%' AND";
if (!phone.equals(""))
strSql = strSql + " phone LIKE '"+ phone +"%' AND";
if (!address.equals(""))
strSql = strSql + " address LIKE '"+ address +"%'";
else
strSql = strSql.substring(0,strSql.length()-3); strSql = strSql + "ORDER by ID";
HttpSession session = request.getSession();
if (log.isDebugEnabled()) {
log.debug("SearchAction session = " + session);
log.debug("SearchAction strSql = " + strSql); }
session.setAttribute(Constants.SQLSTMT_KEY, strSql); return (mapping.findForward(Constants.FORWARD_SUCCESS)); }
}
其中最后一句 return (mapping.findForward(Constants.FORWARD_SUCCESS));
死活找不到这个action跳转到那个页面或者程序里去了
package addressbook.actions;/**
* <strong>SearchAction</strong> will take the search parameters
* specified by the user and create the Sql statement to be used
* by the appropriate forward.
*/
public final class SearchAction extends AbstActionBase { private Log log =
LogFactory.getLog(this.getClass().getName()); public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception { Locale locale = getLocale(request);
MessageResources messages = getResources(request); ActionMessages errors = new ActionMessages();
String name = ((SearchForm) form).getName();
String phone = ((SearchForm) form).getPhone();
String address=((SearchForm)form).getAddress(); if (!errors.isEmpty()) {
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
} String strSql = new String("SELECT * FROM " + Constants.TABLENAME + " WHERE "); if (!name.equals(""))
strSql = strSql + "name LIKE '"+ name +"%' AND";
if (!phone.equals(""))
strSql = strSql + " phone LIKE '"+ phone +"%' AND";
if (!address.equals(""))
strSql = strSql + " address LIKE '"+ address +"%'";
else
strSql = strSql.substring(0,strSql.length()-3); strSql = strSql + "ORDER by ID";
HttpSession session = request.getSession();
if (log.isDebugEnabled()) {
log.debug("SearchAction session = " + session);
log.debug("SearchAction strSql = " + strSql); }
session.setAttribute(Constants.SQLSTMT_KEY, strSql); return (mapping.findForward(Constants.FORWARD_SUCCESS)); }
}
其中最后一句 return (mapping.findForward(Constants.FORWARD_SUCCESS));
死活找不到这个action跳转到那个页面或者程序里去了
<action path="/search"
type="addressbook.actions.SearchAction"
name="searchForm"
attribute="myForm"
scope="request"
input="/search.jsp">
<forward name="success" path="/display.jsp"/>
</action>
中的"success",如果是的话,应该没有问题。
public final class InsertAction extends AbstActionBase { private Log log =
LogFactory.getLog(this.getClass().getName()); public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
Locale locale = getLocale(request);
MessageResources messages = getResources(request);
String name=null;
String phone=null;
String address=null; ActionMessages errors = new ActionMessages(); name = ((InsertForm) form).getName();
phone=((InsertForm)form).getPhone();
address=((InsertForm)form).getAddress(); try
{
AddressBookBean bean=new AddressBookBean(name,phone,address);
bean.insert();
}
catch(Exception ex)
{
ex.printStackTrace(System.out);
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("error.insert.failed"));
} if (!errors.isEmpty()) {
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
// If we had no errors, then add a confirmation message
ActionMessages actionMessages = new ActionMessages();
actionMessages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("record.inserted"));
saveMessages(request,actionMessages); return (mapping.findForward(Constants.FORWARD_CONFIRMATION)); }
}
----
配置文件在
<action path="/insert1"
parameter="/insert_next.jsp"
type="org.apache.struts.actions.ForwardAction"
name="insertForm"
scope="session"
input="/insert.jsp"
validate="true">
</action> <action path="/insert2"
type="addressbook.actions.InsertAction"
name="insertForm"
scope="session"
input="/insert_next.jsp"
validate="true">
</action>
这个好难找到对应的jsp文件, public static final String FORWARD_CONFIRMATION = "confirmation";, 是否对应的是 confirmation.jsp
小结:(寻求确认)?如果配置文件制定跳转的jsp文件,那么就是那个制定的jsp;
?如果配置文件没有制定跳转的jsp文件, 那么就是mapping.findForward(Constants.FORWARD_CONFIRMATION)里面的哦对应名字的.jsp文件
<global-forwards>
<forward name="logoff" path="/logoff.do"/>
<forward name="logon" path="/logon.jsp"/>
<forward name="success" path="/mainMenu.jsp"/>
<forward name="search" path="/search.jsp"/>
<forward name="displayall" path="/displayall.do"/>
<forward name="insert" path="/insert.jsp"/>
<forward name="mainMenu" path="/mainMenu.jsp"/>
<forward name="confirmation" path="/confirmation.jsp"/>
</global-forwards>
<global-forwards>
定义了
<forward name="success" path="/mainMenu.jsp"/> 后面的 Action Mapping Definitions可以不再定义。
如果重名,必须重新定义。 <forward name="success" path="/display.jsp"/>
action默认先找当前action-mapping中配置的forward,找不到后再找global-forwards