一般开发struts可以用myeclipse开发,傻瓜高效。但高手都是手工自己配置的 ,我不是高手,也可以手工配置。
首先,上官网下载需要的包.网址
里面包括说明文档跟例子。可参考例子。webapps下面有个struts-blank.war,可以解压,这是一个空白的示范文件,可以把/WEB-INF下面有所需的文件,包括lib下面所需的包,还有/WEB-INF下面的其他文件(除了web.xml)
首先修改web.xml(可参考空白例子下面/WEB-INF/web.xml的配置)
< 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 >
< init-param >
< param-name > debug </ param-name >
< param-value > 2 </ param-value >
</ init-param >
< init-param >
< param-name > detail </ param-name >
< param-value > 2 </ param-value >
</ init-param >
< load-on-startup > 2 </ load-on-startup >
</ servlet >
<!-- Standard Action Servlet Mapping -->
< servlet-mapping >
< servlet-name > action </ servlet-name >
< url-pattern > *.do </ url-pattern >
</ servlet-mapping >
其中 <init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
指明struts-config.xml的位置,可以把struts-config.xml放在别的地方,然后在这里修改
其中struts-config.xml就是struts的核心配置,可以参考例子。最简单的一个配置如下
<! 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 >
< form - beans >
<!-- sample form bean descriptor for an ActionForm
< form - bean
name = " inputForm "
type = " app.InputForm " />
</ form - beans >
< action - mappings >
< action
path = " /InputSubmit "
type = " app.InputAction "
name = " inputForm "
scope = " request "
validate = " true "
input = " /pages/Input.jsp " />
</ action - mappings >
</ struts - config >
一个FormBean要继承ActionForm,例子如下;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/1327ab569c1ae82736693a50b8e33378.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
而action则一般继承Action,例子如下
public class ExampleAction extends Action {
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
//得到对应的form
ExampleActionForm eaf = (ExampleActionForm)actionForm;
//取得输入的test
String test = eaf.getTest();
// 判断并将值放入request
if("".equals(test)){
request.setAttribute("test","empty");
}else{
request.setAttribute("test",test);
}
// 通过mapping寻找相应的url,返回ActionFoward
return actionMapping.findForward("foward");
}
}