struts2的关于method=“{1}"意思详解
  <action   name= "Login_* "   method= "{1} "   class= "mailreader2.Login ">
  中Login_*带*是什么意思?method= "{1} "带{}这个是什么意思?
  ====================================================
  name= "Login_* "
  代表这个action处理所有以Login_开头的请求
  method= "{1} "
  根据前面请求Login_methodname,调用action中的以methodname命名的方法
  class= "mailreader2.Login "
  action的类名称
  如jsp文件中请求Login_validateUser的action名称,根据上面配置,调用action类mailreader2.Login类中方法validateUser()
  又如:
  对于Login_update请求,将会调用mailreader2.Login的update()方法。
  它的用法同webwork中的!符号的作用,相当于是一个通配符。
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++
  struts2中的路径问题
  注意:在jsp中”/”表示tomcat 服务器的根目录,在struts.xml配置文件中”/”表示webapp的根路径,即MyEclipse web项目中的WebRoot路径。
  总结:
  struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径 。
  虽然可以用redirect方式解决,但redirect方式并非必要。
  解决办法非常简单,统一使用绝对路径。 (在jsp中用request.getContextRoot方式来拿到webapp的路径)
  或者使用myeclipse经常用的,指定basePath。
  Action Method


  配置:
  <package name="user" extends="struts-default" namespace="/user">
  <action name="userAdd" class="com.bjsxt.struts2.user.action.UserAction" method="add">
  <result>/user_add_success.jsp</result>
  </action>
  <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
  <result>/user_add_success.jsp</result>
  </action>
  </package>
  总结:
  Action执行的时候并不一定要执行execute方法
  1、可以在配置文件中配置Action的时候用method=来指定执行哪个方法(前者方法)
  2、也可以在url地址中动态指定(动态方法调用DMI )(推荐)(后者方法)
  <a href="<%=context %>/user/userAdd">添加用户
  <br />
  <a href="<%=context %>/user/user!add">添加用户
  <br />
  前者会产生太多的action,所以不推荐使用。(注:<% String context = request.getContextPath(); %>)
  再给个案例,大概介绍!使用动态调用DMI的方法,即通过!+方法名的指定方法:
  UserAction.java
  import com.opensymphony.xwork2.ActionContext;
  import java.util.Map;
  public class UserAction {
  private String userName;
  private String password;
  public String getUserName() {
  return userName;
  }
  public void setUserName(String userName) {
  this.userName = userName;
  }
  public String getPassword() {
  return password;
  }
  public void setPassword(String password) {
  this.password = password;
  }
  public String execute(){
  if(!userName.equals("aa")||!password.equals("aa")){
  return "error";
  }else{
  Map session=(Map)ActionContext.getContext().getSession();
  session.put("userName", userName);
  return "success";
  }
  }
  public String loginOther(){
  if(!userName.equals("bb")||!password.equals("bb")){
  return "error";
  }else{
  Map session=(Map)ActionContext.getContext().getSession();


  session.put("userName", userName);
  return "success";
  }
  }
  }
  struts.xml
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
  <struts>
  <package name="default" extends="struts-default">
  <action name="struts" class="org.action.StrutsAction">
  <result name="success">/welcome.jsp</result>
  <result name="error">/hello.jsp</result>
  <result name="input">/hello.jsp</result>
  </action>
  <action name="user" class="org.action.UserAction">
  <result name="success">/login_welcome.jsp</result>
  <result name="error">/login_error.jsp</result>
  </action>
  <!--<action name="loginOther" class="org.action.UserAction" method="loginOther">
  <result name="success">/login_welcome.jsp</result>
  <result name="error">/login_error.jsp</result>
  </action>
  -->
  </package>
  </struts>
  login_welcome.jsp
  <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


  <%@ taglib uri="/struts-tags" prefix="s" %>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
  <head>
  <title>欢迎</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>
  <s:set value="#session.userName" name="userName" />
  你好!<s:property value="#userName"/>
  </body>
  </html>
  login_error.jsp
  <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
  <head>
  <title>登陆失败</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="login.jsp">登陆
  </body>
  </html>
  login.jsp
  <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  <%@ taglib uri="/struts-tags" prefix="s" %>
  <%
  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>struts 2应用</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>
  <s:form  action="user!loginOther" method="post"> 红色部分,你如果想调用userAction中的loginOther方法而不想调用execute方法, 直接通过 !+方法名即可,那你就不用再设置struts.xml中注释掉的部分了,这样可以不产生太多的action
  <s:textfield name="userName" label="请输入姓名" ></s:textfield> <s:textfield name="password" label="请输入密码"></s:textfield>
  <s:submit value="提交"></s:submit>
  </s:form>
  </body>
  </html>
  Action Wildcard(Action 通配符)


  配置:
  <package name="actions" extends="struts-default" namespace="/actions">
  <action name="Student*" class="com.bjsxt.struts2.action.StudentAction" method="{1}">
  <result>/Student{1}_success.jsp</result>
  </action>
  <action name="*_*" class="com.bjsxt.struts2.action.{1}Action" method="{2}">
  <result>/{1}_{2}_success.jsp</result>
  <!-- {0}_success.jsp -->
  </action>
  </package>
  {1}、{2}表示第一第二个占位符
  *为通配符
  通过action name的通配匹配,获得占位符,可以使用占位符放在result和method、class中替代匹配的字符。
  总结:
  使用通配符,将配置量降到最低。
  <a href="<%=context %>/actions/Studentadd">添加学生
  <a href="<%=context %>/actions/Studentdelete">删除学生
  不过,一定要遵守"约定优于配置"的原则。
  <a href="<%=context %>/actions/Teacher_add">添加老师
  <a href="<%=context %>/actions/Teacher_delete">删除老师
  <a href="<%=context %>/actions/Course_add">添加课程
  <a href="<%=context %>/actions/Course_delete">删除课程


  接收参数值
  1、使用action属性接收参数
  只需在action加入getter/setter方法,如参数name=a,接受到参数必须有getName/setName方法。
  链接:<a href="user/user!add?name=a&age=8">
  public class UserAction extends ActionSupport {
  private String name;
  private int age;
  public String add() {
  System.out.println("name=" + name);
  System.out.println("age=" + age);
  return SUCCESS;
  }
  public String getName() {
  return name;
  }
  public void setName(String name) {
  this.name = name;
  }
  public int getAge() {
  return age;
  }
  public void setAge(int age) {
  this.age = age;
  }
  }


  2、使用Domain Model接收参数
  将之前的属性放入到POJO ,并设置属性的setter/getter方法
  链接:使用Domain Model接收参数<a href="user/user!add?user.name=a&user.age=8">添加用户
  public class UserAction extends ActionSupport {
  private User user;
  //private UserDTO userDTO;
  public String add() {
  System.out.println("name=" + user.getName());
  System.out.println("age=" + user.getAge());
  return SUCCESS;
  }
  public User getUser() {
  return user;
  }
  public void setUser(User user) {
  this.user = user;
  }
  }
  public class User {
  private String name;
  private int age;
  public String getName() {
  return name;
  }
  public void setName(String name) {
  this.name = name;
  }
  public int getAge() {
  return age;
  }
  public void setAge(int age) {
  this.age = age;
  }
  }


  3、使用ModelDriven接收参数
  Action实现ModelDriven接口,实现getModel()方法。
  这样user需要自己new出来,getModel返回user。
  链接:使用ModelDriven接收参数<a href="user/user!add?name=a&age=8">添加用户
  public class UserAction extends ActionSupport implements ModelDriven<User> {
  private User user = new User();
  public String add() {
  System.out.println("name=" + user.getName());
  System.out.println("age=" + user.getAge());
  return SUCCESS;
  }
  @Override
  public User getModel() {
  return user;
  }
  }
  字符编码
  配置:
  <constant name="struts.i18n.encoding" value="GBK" /> <!-- internationalization -->
  在struts2.1.6中不起作用,属于bug,在struts2.1.7中修改。


  解决方案:
  修改web.xml 中:
  <filter>
  <filter-name>struts2</filter-name>
  <!-- struts2.1中使用filter -->
  <!--<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>-->
  <!-- struts2.0中使用的filter -->
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  =============================================================
  Struts2一个Action内包含多个请求处理方法的处理(三种方式)
  Struts1提供了DispatchAction,从而允许一个Action内包含多个请求处理方法。Struts2也提供了类似的功能。处理方式主要有以下三种方式:
  1.1. 动态方法调用:
  DMI:Dynamic Method Invocation 动态方法调用。
  动态方法调用是指:表单元素的action不直接等于某个Action的名字,而是以如下形式来指定对应的动作名:
  <form method="post" action="userOpt!login.action">
  则用户的请求将提交到名为”userOpt”的Action实例,Action实例将调用名为”login”方法来处理请求。同时login方法的签名也是跟execute()一样,即为public String login() throws Exception。
  注意:要使用动态方法调用,必须设置Struts2允许动态方法调用,通过设置struts.enable.DynamicMethodInvocation常量来完成,该常量属性的默认值是true。
  1.1.1.    示例:
  修改用户登录验证示例,多增加一个注册用户功能。
  1.    修改Action类:
  package org.qiujy.web.struts2.action;
  import com.opensymphony.xwork2.ActionContext;
  import com.opensymphony.xwork2.ActionSupport;
  /**
  *@authorqiujy
  *@version1.0
  */
  publicclass LoginAction extends ActionSupport{
  private String userName;
  private String password;
  private String msg; //结果信息属性


  /**
  *@returnthemsg
  */
  public String getMsg() {
  returnmsg;
  }
  /**
  *@parammsgthemsgtoset
  */
  publicvoid setMsg(String msg) {
  this.msg = msg;
  }
  /**
  *@returntheuserName
  */
  public String getUserName() {
  returnuserName;
  }
  /**
  *@paramuserNametheuserNametoset
  */
  publicvoid setUserName(String userName) {
  this.userName = userName;
  }
  /**
  *@returnthepassword
  */
  public String getPassword() {
  returnpassword;
  }
  /**
  *@parampasswordthepasswordtoset
  */
  publicvoid setPassword(String password) {
  this.password = password;
  }
  /**


  *处理用户请求的login()方法
  *@return结果导航字符串
  *@throw***ception
  */
  public String login() throws Exception{
  if("test".equals(this.userName) && "test".equals(this.password)){
  msg = "登录成功,欢迎" + this.userName;
  //获取ActionContext实例,通过它来访问Servlet API
  ActionContext context = ActionContext.getContext();
  //看session中是否已经存放了用户名,如果存放了:说明已经登录了;
  //否则说明是第一次登录成功
  if(null != context.getSession().get("uName")){
  msg = this.userName + ":你已经登录过了!!!";
  }else{
  context.getSession().put("uName", this.userName);
  }
  returnthis.SUCCESS;
  }else{
  msg = "登录失败,用户名或密码错";
  returnthis.ERROR;
  }
  }
  public String regist() throws Exception{
  //将用户名,密码添加到数据库中
  //...
  msg = "注册成功。";
  returnthis.SUCCESS;
  }
  }
  2.    struts.xml文件:没有什么变化,跟以前一样配置


  <!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
  <struts>
  <package name="my" extends="struts-default" namespace="/manage">
  <!-- 定义处理请求URL为login.action的Action -->
  <action name="userOpt" class="org.qiujy.web.struts2.action.LoginAction">
  <!-- 定义处理结果字符串和资源之间的映射关系 -->
  <result name="success">/success.jsp</result>
  <result name="error">/error.jsp</result>
  </action>
  </package>
  </struts>
  3.    页面:
  index.jsp
  <%@ page language="java" pageEncoding="UTF-8"%>
  <html>
  <head>
  <title>用户登录页面</title>
  </head>
  <body>
  <h2>用户入口</h2>
  <hr>
  <form action="manage/userOpt!login.action" method="post">
  <table border="1">
  <tr>
  <td>用户名:</td>
  <td><input type="text" name="userName"/></td>
  </tr>
  <tr>
  <td>密码:</td>
  <td><input type="password" name="password"/></td>
  </tr>
  <tr>
  <td colspan="2">
  <input type="submit" value=" 确定 "/>
  </td>
  </tr>
  </table>
  </form>
  </body>
  </html>
  regist.jsp
  <%@ page language="java" pageEncoding="UTF-8"%>


  <html>
  <head>
  <title>用户注册页面</title>
  </head>
  <body>
  <h2>用户注册</h2>
  <hr>
  <form action="manage/userOpt!regist.action" method="post">
  <table border="1">
  <tr>
  <td>用户名:</td>
  <td><input type="text" name="userName"/></td>
  </tr>
  <tr>
  <td>密码:</td>
  <td><input type="password" name="password"/></td>
  </tr>
  <tr>
  <td colspan="2">
  <input type="submit" value=" 注册 "/>
  </td>
  </tr>
  </table>
  </form>
  </body>
  </html>
  1.2. 为Action配置method属性:
  将Action类中的每一个处理方法都定义成一个逻辑Action方法。
  <!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
  <struts>
  <package name="my" extends="struts-default" namespace="/manage">
  <action name="userLogin" class="org.qiujy.web.struts2.action.LoginAction" method="login">
  <result name="success">/success.jsp</result>
  <result name="error">/error.jsp</result>


  </action>
  <action name="userRegist" class="org.qiujy.web.struts2.action.LoginAction" method="regist">
  <result name="success">/success.jsp</result>
  <result name="error">/error.jsp</result>
  </action>
  </package>
  </struts>
  如上,把LoginAction中的login和regist方法都配置成逻辑Action。要调用login方法,则相应的把index.jsp中表单元素的action设置为"manage/userLogin.action";要调用regist方法,把regist.jsp中表单元素的action 设置为"manage/userRegist.action"。
  1.3. 使用通配符映射(wildcard mappings)方式:
  在struts.xml文件中配置<action…>元素时,它的name、class、method属性都可支持通配符,这种通配符的方式是另一种形式的动态方法调用。
  当我们使用通配符定义Action的name属性时,相当于用一个元素action定义了多个逻辑Action:
  <action name="user_*"
  class="org.qiujy.web.struts2.action.UserAction" method="{1}">
  <result name="success">/success.jsp</result>
  <result name="error">/error.jsp</result>
  </action>
  如上,<action name=”user_*”>定义一系列请求URL是user_*.action模式的逻辑Action。同时method属性值为一个表达式 {1},表示它的值是name属性值中第一个*的值。例如:用户请求URL为user_login.action时,将调用到UserAction类的 login方法;用户请求URL为user_regist.action时,将调用到UserAction类的regist方法。

原文链接: http://java.chinaitlab.com/Strut...

Struts2通配符映射/Struts action name=/*/* method={2} class=com.jxc.action.{1}Action result相关推荐

  1. Struts(七):action配置文件之通配符映射

    通配符映射:一个Web应用可能有成百上千个action声明,可以使用struts提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系. 通配符映射规则: 若找到多个匹配,没有通配符的那个将胜 ...

  2. 12.Struts2_通配符映射

    1.通配符映射: 一个 Web 应用可能有成百上千个 action 声明. 可以利用 struts 提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系 如: <action name ...

  3. Struts2第三篇【Action开发方式、通配符、Struts常量、跳转全局视图、action节点默认配置】...

    前言 上篇Struts博文已经讲解了Struts的开发步骤以及执行流程了-..对Struts的配置文件有了了解-..本博文继续讲解Struts在配置的时候一些值得要学习的细节- Action开发的三种 ...

  4. struts2的动态方法调用(DMI)和通配符映射

    动态方法调用 1.Struts2默认关闭DMI功能,需要使用需要手动打开,配置常量 [html] view plaincopy struts.enable.DynamicMethodInvocatio ...

  5. [原创]java WEB学习笔记58:Struts2学习之路---Result 详解 type属性,通配符映射

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. Struts2的配置 struts.xml Action详解

    在学习struts的时候,我们一定要掌握struts2的工作原理.只有当我们明确了在struts2框架的内部架构的实现过程,在配置整个struts 的框架时,可以很好的进行逻辑上的配置.接下来我就先简 ...

  7. Struts2 学习系列 (3) 跳转类型与通配符映射

    2019独角兽企业重金招聘Python工程师标准>>> 跳转类型 chain:Action链式处理的结果类型,也就是将结果转发到这个action中. chart:整合JFreeCha ...

  8. struts2之配置文件struts.xml详解

    struts配置文件 struts.xml配置参数详解 struts.xml中很大一部分配置默认配置就好了 但是有些还是需要做了解  以便于理解 和修改 <?xml version=" ...

  9. struts2——通配符

    struts2的配置文件是 struts.xml.. 在这个配置文件里面可以使用通配符..其中的好处就是,大大减少了配置文件的内容..当然,相应付出的代价是可读性.. 使用通配符的原则是 约定高于配置 ...

最新文章

  1. 2010年基于Linux的10大技术趋势
  2. 小程序跳转样式布局错乱_小程序 wxss 样式毁三观
  3. 中国金控(00875):林裕豪调任执行董事并获任首席执行官
  4. JavaScript实现TwoQueues缓存模型
  5. Android两个注意事项.深入了解Intent和IntentFilter(两)
  6. layui实现select下拉选择框组件(含代码、案例、截图)
  7. BZOJ3597 SCOI2014方伯伯运椰子(分数规划+spfa)
  8. AngularJS2.0教程(一)快速上手之基础知识
  9. 2019-07-10
  10. Win7连接蓝牙4.0鼠标
  11. 浅谈select2的使用
  12. 对话,对话,全是超级爆笑对话
  13. 【个人亲测】2018最流行的浏览器排行榜前10
  14. 怎么用python编写程序计算标准差_自学生信Python(第五天)|如何计算标准差?...
  15. 【华为OD机试真题 python】预订酒店【2022 Q4 | 100分】
  16. 2020.8.5课堂笔记(多线程)
  17. VaR(value at risk)在险价值
  18. 用DP-GEN跑个简单的例子吧——CH4
  19. nw.js 使用记录 - 加载远程URL
  20. h5 ios手机 隐藏input输入光标

热门文章

  1. hexo搭建github.io博客
  2. 08.attempt 方法
  3. 天翼云桌面Linux,天翼云Linux主机如何挂载数据盘
  4. UI设计教程分享:PS故障风海报制作教程
  5. 基于多项式拟合的结构光系统标定
  6. PyCharm使用技巧:Shift + Tab(向左缩进)
  7. HTML5中input输入框默认提示文字向左向右移动
  8. android 平板版 office,Android平板版Office评测:界面繁杂影响用户体验
  9. 模型数据处理之关键属性提取——SuperMap iDesktop
  10. 20. GD32F103C8T6入门教程-adc使用外部中断IT11触发启动adc规则通道,使用外中断IT15触发注入组