访问action的方法

    xiaoxiao2021-03-25  130

    访问action的方法 一共有三种方法 分别是:

    使用action标签的method属性 在这个属性里面写执行的action的方法使用通配符方式实现动态访问实现(不用掌握)

    注意

    如果action方法有返回值 在配置文件中没有配置 会出现404错误在action里面的方法有返回值 如果有返回值时候类型必须是Stringaction里面的方法可以没有返回值 没有返回值时候 在result标签不需要配置 把方法写出void让返回值 返回“none”

    使用action标签method属性

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <constant name="struts.configuration.xml.reload" value="true"></constant> <constant name="struts.devMode" value="true"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="jAction" class="cn.zhoushicanyin.ss.jjAction" method="get">//这里的method默认的是execute <result name="none">/hello.jsp</result> <result name="ok">/world.jsp</result> </action> </package> </struts>

    注意:

    action每个方法都需要配置 如果action里面有大量方法 会配置很多的action

    使用通配符方式实现

    在action标签里面name属性 name属性值里面写符号 * 星号

    * 理解:表示匹配任意内容

    比如访问hello,*可以匹配到比如访问add,*同样可以匹配到

    举例:

    struts.xml中的配置

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="px" extends="struts-default" namespace="/"> <action name="*Action" class="cn.zhoushicanyin.ss.{1}Action" method="add"> <result>/{1}add.jsp</result>//这里的{1}匹配第一个出现的* 如果出现{2}就是匹配第二个出现的* </action> </package> </struts>

    action中的配置

    UserAction

    public class UserAction extends ActionSupport{ public String add() { return SUCCESS; } public String delete() { return SUCCESS; } }

    TeacherAction

    public class TeacherAction extends ActionSupport{ public String add() { return SUCCESS; } public String delete() { return SUCCESS; } }

    两个jsp页面

    Teacheradd.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>我是Teacher 的add方法</h1> </body> </html>

    Useradd

    <%@ 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>我是User add方法</h1> </body> </html>

    比如此时访问的是TeacherAction.action *匹配的就是Teacher 所以{1}就代表的是Teacher 所以class的值就为 cn.zhoushicanyin.ss.TeacherAction 访问的就是Teacheradd.jsp

    END!!!!!!

    转载请注明原文地址: https://ju.6miu.com/read-10112.html

    最新回复(0)