struts1和spring 1.2 的简单配置(我是初学者)

以前用过struts2,但没用过struts1,最近项目上需要用到1,所以这两天在看1的配置,自己动手配了一个测试项目,
首先把包导近来,包括commons、struts1、spring、log4j的包,
其次,配置web.xml
如下:

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


然后,在web-info下添加struts1配置文件 struts-config.xml
如下:



<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

<data-sources />
<form-beans>
<form-bean name="testForm" type="com.me.TestForm" />
</form-beans>

<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/aa" name="testForm" type="suibian tian " parameter="method" scope="request">
<forward name="success" path="/WEB-INF/jsp/aa.jsp" />
</action>
<action path="/bb" forward="/WEB-INF/jsp/bb.html" />
</action-mappings>

<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
<message-resources parameter="resources.ApplicationResources" key="org.apache.struts.action.MESSAGE" />

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validation.xml" />
</plug-in>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/classes/conf/spring/spring.xml" />
</plug-in>
</struts-config>


[color=red]注意点[/color]:我刚开始在<set-property property="contextConfigLocation" value="/WEB-INF/classes/conf/spring/spring.xml" />走了弯路,写成 value="conf/spring/spring.xml"。以为这样可以了,并且刚开始没配log4j,看不出来什么错误,浪费了很多时间,后来裴了log4j才知道是路径搞错了.


在然后,在/WEB-INF/classes/conf/spring里配一个spring.xml,
如下;


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean name="/aa" class="com.me.TestAction" singleton="false"/>
</beans>


再然后,添加action类,添加jsp,添加html文件。

action文件。

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class TestAction extends DispatchAction {



protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

System.out.println("-----------UserAction.unspecified()-----------");

// 调用业务逻辑操作
request.setAttribute("welcome", "from unspecified");
TestForm testForm = (TestForm)form;
System.out.println(testForm.getName());
return mapping.findForward("success");

}

public ActionForward go(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
if(form!=null){
TestForm testForm = (TestForm)form;
if(testForm.getName()!=null){
System.out.println(testForm.getName());}
}
request.setAttribute("welcome", "from go method");
return (mapping.findForward("success"));
}

public ActionForward come(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

request.setAttribute("welcome", "from come method");
return (mapping.findForward("success"));
}

}


jsp和html文件我就不列出来了,太长了。

然后一切就可以了,这个mini练手项目就搭起来了,大家可以根据自己需求往里面添砖加瓦,让它不断壮大。

[color=red]注意点[/color]:为了可以使用action中的其它方法,需要继承DispatchAction,这样可以通过url来指定你使用的方法,别忘了重写unspecified方法,写清楚没有指定方法时应该怎么办。
简单说明一下,DispatchAction本身时继承了Action,并重写了它的execute方法(可以看一下它的源码,应该接受parameter,并做相应处理),所以我们这里就不要重写execute了,否则很可能报错。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值