时 间 记 忆
<<  < 2011 - 10 >  >>
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

最 新 评 论

最 新 日 志

最 新 留 言

搜 索

用 户 登 录

友 情 连 接


 
 
 
struts入门心得
[ 2006-10-13 16:58:00 | By: 笨笨猪 ]
 
在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" ?>
<!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>
<!-- ==========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.处理过程
 
 
  • 标签:struts 
  • 发表评论:
     
    天涯博客 天涯博客
    天涯博客欢迎您!