歡迎您光臨本站 註冊首頁

Struts2通配符映射

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0
  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中修改.


[火星人 ] Struts2通配符映射已經有712次圍觀

http://coctec.com/docs/java/show-post-60004.html