<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                這是學習Struts的一個簡單的例子文件結構如下: ![](https://box.kancloud.cn/2016-02-22_56caddf97a5f6.jpg) ## 1、[配置](http://blog.csdn.net/gwblue/article/details/18367901)[Struts環境](http://blog.csdn.net/gwblue/article/details/18367901) ##2、新建input.jsp,success.jsp,error.jsp input.jsp代碼如下: ~~~ <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> <h1>簡易計算器</h1> <hr> <form action="unit/cal.action" 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> </body> </html> ~~~ ## 3、新建com.bjpowernode.struts這個包。 關鍵的是新建CalAactionForm.java和CalAction.java這兩個類的時候需要繼承Struts的ActionForm和Action這兩個類如下: CalAactionForm.java image/a2623c082d17424e9c15e201d14dff26.jpg) 代碼如下:> ? ~~~ package com.bjpowernode.struts; import org.apache.struts.action.ActionForm; @SuppressWarnings("serial") public class CalAactionForm extends ActionForm { private int value1; private String flag; private int value2; public int getValue1() { return value1; } public void setValue1(int value1) { this.value1 = value1; } public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } public int getValue2() { return value2; } public void setValue2(int value2) { this.value2 = value2; } } ~~~ > > 注意:這個類里面的屬性要和input.jsp里面的表單數據一致 ActionForm并非單單是獲取頁面表單的數據,它也可以獲取其他的數據,比如說通過超鏈接傳過來的數據。也就是說凡是從頁面傳遞過來的參數都可以注入到ActionForm。就像C#里面我們寫的實體類一樣。 CalAction.java ![](https://box.kancloud.cn/2016-02-22_56caddf98cdd1.jpg) 代碼如下: ~~~ package com.bjpowernode.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class CalAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { CalAactionForm calForm=(CalAactionForm)form; int value1=calForm.getValue1(); String flag=calForm.getFlag(); int value2=calForm.getValue2(); 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(); mapping.findForward("error"); } return mapping.findForward("error"); } } ~~~ > ## 4、配置struts-config.xml 代碼如下: ~~~ <?xml version="1.0" encoding="ISO-8859-1" ?> <!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> <form-bean name="calForm" type="com.bjpowernode.struts.CalAactionForm"/> </form-beans> <action-mappings> <action path="/unit/cal" type="com.bjpowernode.struts.CalAction" name="calForm" scope="request" > <forward name="success" path="/success.jsp"/> <forward name="error" path="/error.jsp"/> </action> </action-mappings> </struts-config> ~~~ 注意:struts-config.xml只要更改了就要重新啟動一次Tomcat ## 5、配置web.xml。 代碼如下: ~~~ <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>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>*.action</url-pattern> </servlet-mapping> </web-app> ~~~ ## 6、填寫success.jsp,error.jsp代碼。 Success.jsp代碼如下: ~~~ <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> ${calForm.value1} ${calForm.flag } ${calForm.value2} = ${result } </body> </html> ~~~ Error.jsp代碼如下: ~~~ <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> ${calForm.value1 } ${calForm.flag } ${calForm.value2 } 計算失敗! </body> </html> ~~~ ## 7、index.jsp代碼。 ~~~ <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <a href="input.jsp">簡易計算器</a> </body> </html> ~~~ ? ## 8、效果圖: ![](https://box.kancloud.cn/2016-02-22_56caddf9aa828.jpg) 點擊計算,成功則顯示: ![](https://box.kancloud.cn/2016-02-22_56caddf9c10b1.jpg) 如果失敗則: ![](https://box.kancloud.cn/2016-02-22_56caddf9d3532.jpg)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看