Struts2 action的引數傳遞方式
阿新 • • 發佈:2019-01-30
2016年6月19日,今天學習Struts2 action的引數傳遞
主要有3種傳輸方式:
1、用action屬性接收
2、用domain model或者dto(data transcation object)
3、實現modelDriven
第一種傳輸方式:直接在action裡set引數變數
index頁面:
<?xml version="1.0" encoding="GB18030" ?> <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; //在head中<base href>指定basePath %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <base href=<%= basePath %> /> <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> <title>Insert title here</title> </head> <body> 使用Domain Model接收引數 <a href="user/User_add?name=a&age=8">新增使用者</a> </body> </html>
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> <!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <include file="example.xml"/> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <action name="index"> <result type="redirectAction"> <param name="actionName">HelloWorld</param> <param name="namespace">/example</param> </result> </action> </package> --> <!--開啟dmi --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="user" extends="struts-default" namespace="/user"> <action name="*_*" class="com.struts.action.{1}Action" method="{2}" > <result>/{1}_{2}_success.jsp</result> </action> </package> </struts>
action類:
package com.struts.action; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") 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; } }
第二種傳輸方式:通過Domain Model(域模型)接收引數或者dto
index寫法:
action寫法:
model層:
可以通過vo(value object)或do或dto(data transcation object)
這種傳輸方式的流程是這樣的:這種方式目前還不太理解,等在後面學習到的時候再補充吧。他的大概意思是增加dto層:
通過dto層來接收引數,然後再將使用到的引數通過dto傳到domain model層。
第三種傳輸方式是action實現ModelDriven介面
package com.bjsxt.struts2.user.action;
import com.bjsxt.struts2.user.model.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
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;
}
}