在webroot下有个index.jsp文件:
<html:form action="" method="post" focus="login">
<table border="0">
<tr>
<td>用户名 : </td>
<td><html:text property="ep_name" /></td>
</tr>
<tr>
<td>密 码 : </td>
<td><html:password property="ep_pwd" /></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit>登录</html:submit></td>
</tr>
</table>
</html:form>
这个是默认模块的,默认模块的配置文件:
<form-beans>
<form-bean name="loginForm" type="com.ep.login.LoginForm"/>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action
name="loginForm"
path="/toModule"
type="org.apache.struts.actions.SwitchAction"
scope="request">
</action>
</action-mappings>
在webroot/login/下有个sucess.jsp文件这个文件只是显示些成功信息
在login模块中,struts-config-login.xml中:
<form-beans>
<form-bean name="loginForm" type="com.ep.login.LoginForm"/>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action
path="/toLogin"
name="loginForm"
type="com.ep.login.LoginAction"
scope="request">
<forward name="suc" path="/login/sucess.jsp" contextRelative="true"/>
</action>
</action-mappings>
在com.ep.login.LoginAction中:
public class LoginAction extends DispatchAction {
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
LoginForm loginForm = (LoginForm)form;
String ep_name = loginForm.getEp_name();
String ep_pwd = loginForm.getEp_pwd();
System.out.println(ep_name+" "+ep_pwd);
return mapping.findForward("suc");
}
}
在com.ep.login.LoginForm中:
public class LoginForm extends ActionForm {
private static final long serialVersionUID = 1L;
private String ep_name;
private String ep_pwd;
public String getEp_name() {
return ep_name;
}
public void setEp_name(String ep_name) {
this.ep_name = ep_name;
}
public String getEp_pwd() {
return ep_pwd;
}
public void setEp_pwd(String ep_pwd) {
this.ep_pwd = ep_pwd;
}
}
在web.xml中:
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>config/login</param-name>
<param-value>/WEB-INF/struts-config-login.xml</param-value>
</init-param>
好,在默认模块中包含index.jsp和struts-config.xml两个文件,在login模块中包含
sucess.jsp,LoginAction,LoginForm和struts-config-login.xml四个文件,因为是要
用SwitchAction练习,所以不准备用别的。
问题是在index.jsp文件中不知道写什么好,后来经过测试,是用
/toModule.do?prefix=/login&page=/toLogin.do?method=login
就可以了
但是,有个问题:就是两个struts-config中都用了一个form-bean(LoginForm),
在struts-config.xml中因为是html:form表单(包含form属性)提交,所以要将表单封装到form
中,在struts-config-login.xml中因为要处理表单里的内容,所以也要加这个form,所以
问题就来了,只是一个表单提交就用到两个form,很不爽,想只写一个但不知道怎么用?
而且上面的那个url看起来很复杂,想搞个简单的,不知道怎么弄?
下面的地址是我发的求助帖,
http://community.csdn.net/Expert/topic/5474/5474422.xml?temp=.8182642
当然我自己也会想办法的