昨天看到同学在做struts,本来以为挺简单的东西,没想到自己都忘得差不多了……写配置文件时,name、type等等各表示什么,一下子还搞不清楚了……所以还得多做多练才行!
早上试着用struts1做了一个简单的登录功能,遇到的问题:
1、LoginAction继承Action,在覆写execute方法时,稍不注意,传错了参数,这是个神不知鬼不觉的错误……
(1)public ActionForward execute(ActionMapping mapping, ActionForm form,
ServletRequest request, ServletResponse response) throws Exception
(2)public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception
看到区别了吗?request和response应当使用带有http协议的!
2、试着让LoginAction用继承DispatchAction的方法,我简单的总结为三个步骤:
第一步:将jsp页面中的url后加上“method=方法名x”,form的method属性值必须为post,不能为get;
第二步:在struts-config.xml的配置文件的action标签中加上parameter="method";
第三步:在action中将需要执行的方法的方法名改为第一步中的“方法名x”(我的做法是,先覆盖execute方法,然后更改自己想要的方法名)。
(当然,上面的method的只是个标识,可以换成其他的,如:action、do等。)
(对于为什么用DispatchAction就只能用post,这点还没弄清楚原因,只是用了get就会出现“does not contain handler parameter named 'method'”的错误)
3、还简单的测试了一下struts1的html标签,在使用时,课本上提到,应该将标签的dtd文件的uri先到web.xml中用<taglib></taglib>标签注册,而我使用的时候,省去了这个步骤,直接在jsp页面中添加<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>,也能使用,还没遇到问题……
总的来说,算是简单的复习了一遍struts1的简单流程,使用过了struts2及struts2的标签库,感觉struts2要比struts1有了更大的优越性,使用起来更顺手!
简单的附上struts-config.xml的配置:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- Action -->
<form-beans>
<form-bean name="userForm" type="form.UserForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/login" name="userForm" type="action.LoginAction">
<forward name="success" path="/info.jsp"></forward>
</action>
<!-- DispatchAction -->
<action path="/login2" name="userForm" type="action.Login2Action" parameter="do">
<forward name="success" path="/info.jsp"></forward>
</action>
</action-mappings>
</struts-config>