在myeclipse中新建web工程.选择struts,自动配置web.xml和struts-config.xml文件
1.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="
http://java.sun.com/xml/ns/j2ee" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
sevlet映射文件名.do </servlet-mapping>
</web-app>
2. struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!-- ==========Form Bean Definitions配置bean文件=================================== -->
<form-beans>
<form-bean name="GetParameterForm" type="beans.GetParameterForm"/>
</form-beans>
<!-- ========== global forwards Definitions配置.do映射 =================================== -->
<global-forwards>
<forward name="success" path="/success.do"/>
<forward name="fail" path="/fail.do"/>
<forward name="good" path="/good.do"/>
</global-forwards>
<!-- ========== Action Mapping Definitions 配置每个action处理完后显示页面============================== -->
<action-mappings>
<action path="/showinput" type="beans.ShowAction" name="GetParameterForm" scope="request" input="/index.jsp" />
<action path="/success" forward="/success.jsp"/>
<action path="/fail" forward="/error.jsp" />
<action path="/good" forward="/good.jsp" />
</action-mappings>
</struts-config>
3. GetParameterForm.java //bean
package beans;
import org.apache.struts.action.ActionForm;
public class GetParameterForm extends ActionForm
{
private String valu="null";
public GetParameterForm() {
}
public void setValu(String s) {
valu = s;
}
public String getValu() {
return valu;
}
}
4.ShowAction.java //action_servlet
package beans;
import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;
import org.apache.struts.util.*;
public final class ShowAction extends Action
{
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
Locale locale = getLocale(request);
MessageResources messages = getResources(request);
HttpSession session = request.getSession();
GetParameterForm userform = (GetParameterForm) form;
if(userform.getValu().equals("success")) {
System.out.println("success");
return(mapping.findForward("success"));
}
else {
if(userform.getValu().equals("good")) {
System.out.println("good");
return(mapping.findForward("good"));
}
else {
System.out.println("fail");
return(mapping.findForward("fail"));
}
}
}
}
5.index.jsp
6.success.jsp
7.fail.jsp
1.在web.xml里配置action的映射名为 .do
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2.在struts-config.xml里配置form-bean文件
<form-beans>
<form-bean name="GetParameterForm" type="beans.GetParameterForm"/>
</form-beans>
3.在struts-config.xml里声明所有.do
<global-forwards>
<forward name="success" path="/success.do"/>
<forward name="fail" path="/fail.do"/>
<forward name="good" path="/good.do"/>
</global-forwards>
4.配置每个action处理完后显示页面
<action-mappings>
<action path="/showinput" type="beans.ShowAction" name="GetParameterForm" scope="request" input="/index.jsp" />
<action path="/success" forward="/success.jsp"/>
<action path="/fail" forward="/error.jsp" />
<action path="/good" forward="/good.jsp" />
</action-mappings>
5.处理过程