struts開發<struts中的action詳細配置. 二>

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   java   檔案   

在eclipse中建立項目StrutsDemo1【struts的配置見】struts開發<在eclipse中配置struts. 一>

詳細檔案夾結構例如以下



第一種配置方法

建立UserAction

package fzl.user.struts.demo;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {public String list(){System.out.println("list");return "success";}public String input(){System.out.println("input");return "success";}public String add(){System.out.println("add");return "success";}}

在struts重配置

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="default" namespace="/" extends="struts-default"><!-- 第一種,配置多個action --><action name="add" class="fzl.user.struts.demo.UserAction" method="add"><result name="success">/WEB-INF/User/add.jsp</result></action><action name="input" class="fzl.user.struts.demo.UserAction" method="input"><result name="success">/WEB-INF/User/input.jsp</result></action><action name="list" class="fzl.user.struts.demo.UserAction" method="list"><result name="success">/WEB-INF/User/list.jsp</result></action></package></struts>

建立input.jsp 、list.jsp、add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!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=UTF-8"><title>Insert title here</title></head><body><h1>------------------input-----------------</h1></body></html>

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!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=UTF-8"><title>Insert title here</title></head><body><h1>------------------add-----------------</h1></body></html>

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!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=UTF-8"><title>Insert title here</title></head><body><h1>------------------list-----------------</h1></body></html>


執行tomcat,在瀏覽器輸入http://localhost:9000/strustDemo1/input


同理輸入響應的檔案夾就會得到對應的頁面輸出

另外一種配置方法

其它的不用修改,直接修改UserAction和struts.xml就可以


package fzl.user.struts.demo;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {public String list(){System.out.println("list");return "list";}public String input(){System.out.println("input");return "input";}public String add(){System.out.println("add");return "add";}}


<action name="User" class="fzl.struts.demo.UserAction">     <result name="add">/WEB-INF/User/list.jsp</result>    <result name="input">/WEB-INF/User/input.jsp</result>    <result name="list">/WEB-INF/User/add.jsp</result>     </action>

執行時輸入http://localhost:9000/StrutsDemo2/User!add 就可以得到對應的頁面



第三種方法


package fzl.user.struts.demo;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {public String list(){System.out.println("list");return "success";}public String input(){System.out.println("input");return "success";}public String add(){System.out.println("add");return "success";}}

通過萬用字元配置,萬用字元不來瞭解的自行百度

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="default" namespace="/" extends="struts-default"> <action name="*_*" class="fzl.user.struts.demo.{1}Action"method="{2}"><result>/WEB-INF/{1}/{2}.jsp</result></action> </package></struts>


action的配置方法大概就這三種,後兩種在開發中用的比較多


struts開發&lt;struts中的action詳細配置. 二&gt;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.