首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

Struts1_学习札记2_struts0200_trainnig_cal

2012-09-05 
Struts1_学习笔记2_struts0200_trainnig_calAction:@Overridepublic ActionForward execute(ActionMapping

Struts1_学习笔记2_struts0200_trainnig_cal
Action:

@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {CalActionForm caf = (CalActionForm)form;int value1 = caf.getValue1();int value2 = caf.getValue2();String flag = caf.getFlag();int result = 0; try {if ("+".equals(flag)) {result = value1 + value2;}else if ("-".equals(flag)) {result = value1 - value2;}else if ("*".equals(flag)) {result = value1 * value2;}else if ("/".equals(flag)) {result = value1 / value2;}request.setAttribute("result", result);return mapping.findForward("success");}catch(Exception e) {e.printStackTrace();}return mapping.findForward("error");}


输入页面:
html<h1>简易计算器</h1><hr><form action="action/cal" method="post"><input type="text" name="value1"><select name="flag"><option value="+">+</option><option value="-">-</option><option value="*">*</option><option value="/">/</option></select><input type="text" name="value2"><input type="submit" value="="></form>


结果页面:
<%CalActionForm caf = (CalActionForm)request.getAttribute("testForm");%><%=caf.getValue1() %><%=caf.getFlag() %><%=caf.getValue2() %>=<%=request.getAttribute("result") %>


配置页面:
<form-beans><form-bean name="calForm" type="com.aowin.struts.CalActionForm"/></form-beans><!-- 默认scope为session --><action-mappings><action path="/cal"type="com.aowin.struts.CalAction"name="calForm"scope="request"attribute="testForm"><!-- path:必须以/开头 --><forward name="success" path="/success.jsp"/><forward name="error" path="/error.jsp"/></action></action-mappings>



web.xml配置说明:

 <servlet>    <servlet-name>action</servlet-name>    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>    <init-param>     <!-- Struts配置文件路径 -->      <param-name>config</param-name>      <param-value>/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>  

热点排行