(三)Struts之Action类基础(一)
一、Action的类型
A、使用普通的类
- 必须有public的execute且返回值为String的方法。
ActionType.java
package com; public class ActionType_1 {
public String execute()
{
System.out.println("我是Action类型1");
return null;
}
}
B、实现Actin接口
- 实现com.opensymphony.xwork2.Action接口,并实现该接口中的execute()方法。
- 在实际开发中,action类很少直接实现Action接口,通常都是从com.opensymphony.xwork2.ActionSupport类继承,ActionSupport实现了Action接口和其他一些可选的接口,提供了输入验证,错误信息存取,以及国际化的支持,选择从ActionSupport继承,可以简化action的定义。
- Action接口中定义了一些常量和一个方法。
C、继承ActionSupport类
- 这个类里面封装了大量的方法,且实现了B中Action接口,
package com; import com.opensymphony.xwork2.ActionSupport; public class ActionType_3 extends ActionSupport { @Override
public String execute() throws Exception {
System.out.println("这是继承ActionSupport的类");
return super.execute();
} }
二、调用Action中的方法
A、 使用method属性
示例:
ActionMethod_1.java:
package method; import com.opensymphony.xwork2.ActionSupport; public class ActionMethod_1 extends ActionSupport { @Override
public String execute() throws Exception {
System.out.println("执行默认execute方法");
return super.execute();
} public String add()
{
System.out.println("执行add方法");
return NONE;
}
public String del()
{
System.out.println("执行del方法");
return NONE;
} public String list()
{
System.out.println("执行list方法");
return NONE;
} public String uptdate()
{
System.out.println("执行uptdate方法");
return NONE;
}
}
假设Actionmethod_1这个Action中有4个方法(除execute外),如果我们要用method属性调用这些方法,则需要:
struts.xml
<package name="default" namespace="/" extends="struts-default"> <action name="method_1" class="method.ActionMethod_1" method="add">
</action>
<action name="method_2" class="method.ActionMethod_1" method="del">
</action>
<action name="method_3" class="method.ActionMethod_1" method="uptdate">
</action>
<action name="method_4" class="method.ActionMethod_1" method="list">
</action> </package>
如配置,当地址栏URL=“工程名/method_1”时,执行method.ActionMethod_1类中的add方法; 当地址栏URL=“工程名/method_2”时,执行method.ActionMethod_1类中的adel方法,以此类推。
- 缺点:如果某个类有很多方法的时候,那么配置起来很麻烦,代码非常多,一般适用于类方法特别少的情况下。
B、 使用动态方法调用
- 对以上示例优化:
ActionMethod_1.java:
package method; import com.opensymphony.xwork2.ActionSupport; public class ActionMethod_1 extends ActionSupport { @Override
public String execute() throws Exception {
System.out.println("执行默认execute方法");
return super.execute();
} public String add()
{
System.out.println("执行add方法2");
return NONE;
}
public String del()
{
System.out.println("执行del方法2");
return NONE;
} public String list()
{
System.out.println("执行list方法2");
return NONE;
} public String uptdate()
{
System.out.println("执行uptdate方法2");
return NONE;
}
}
struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default">
<action name="method" class="method.ActionMethod_1">
</action>
</package>
</struts>
- 注意: 使用动态方法调用,struts.enable.DynamicMethodInvocation 这个常量必须是true ;
index.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>
<% String path=request.getContextPath(); %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="<%=path%>/method!add">执行add方法</a>
<a href="<%=path%>/method!del">执行del方法</a>
<a href="<%=path%>/method!uptdate">执行uptdate方法</a>
<a href="<%=path%>/method!list">执行list方法</a>
</body>
</html>
解析: 当点击a标签时候,请求url中的!后跟的是方法名,<a href="<%=path%>/method!add">执行add方法</a> 即当点击这个标签时,执行action名为“method”中的add方法。其他的依次类推。 这种方法就避免了在struts.xml文件中配置过多,导致代码臃肿。
注意的是:使用动态方法调用,struts.enable.DynamicMethodInvocation 这个常量必须是true ;
C、 使用通配符方法(推荐使用)
ActionMethod_1.java
package method; import com.opensymphony.xwork2.ActionSupport; public class ActionMethod_1 extends ActionSupport { @Override
public String execute() throws Exception {
System.out.println("执行默认execute方法");
return super.execute();
} public String add()
{
System.out.println("执行add方法3");
return NONE;
}
public String del()
{
System.out.println("执行del方法3");
return NONE;
} public String list()
{
System.out.println("执行list方法3");
return NONE;
} public String uptdate()
{
System.out.println("执行uptdate方法3");
return NONE;
}
}
idnex.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>
<% String path=request.getContextPath(); %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="<%=path%>/method_add">执行add方法</a>
<a href="<%=path%>/method_del">执行del方法</a>
<a href="<%=path%>/method_uptdate">执行uptdate方法</a>
<a href="<%=path%>/method_list">执行list方法</a>
</body>
</html>
struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default">
<action name="method_*" class="method.ActionMethod_1" method="{1}">
</action>
</package>
</struts>
- 解析:当点击<a href="<%=path%>/method_add">执行add方法</a> 这个标签的时候,请求url为"工程名/method_add",在命名空间为“/”的包下找到一个叫
“method_*”的Action,* 号表示任意字符(任意长度),即调用method.ActionMethod_1这个类,并调用method=“{1}” 这个方法,{1} 中的表示*表示的字符,在这个标签中*代表add,所以就调用了ActionMethod_1类中的add方法。
注意: 如果有多个通配符,
<action name="method_*_*" class="method.ActionMethod_1" method="{1}"> 而标签为<a href="<%=path%>/method_exam_add">执行add方法</a>,则调用的是第一个通配符匹配的字符即exam方法。
三、接收表单(用户请求)的值
3.1 在struts中获取servlet的原生对象
- 获取servlet原生对象的方式如下:
A、 实现相应的接口,注入servlet对象
示例:
index.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>
<% String path=request.getContextPath(); %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="<%=path%>/servlet/obj">获取并测试servlet原生对象</a> </body>
</html>
struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/servlet" extends="struts-default">
<action name="obj" class="com.ServletObject"></action>
</package>
</struts>
ServletObject.java
package com; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionSupport; public class ServletObject extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{
//定义servlet原生对象为成员变量,否则如果是局部变量生命周期太短。
private ServletContext servletContext;
private HttpServletResponse response;
private HttpServletRequest request;
private PrintWriter out;
private HttpSession session; /**
* 设置ServletContext
*/
public void setServletContext(ServletContext servletContext) {
this.servletContext=servletContext; } /**
* 设置ServletResponse,得到response之后就可由response得到PrintWriter对象
*/
public void setServletResponse(HttpServletResponse response) {
this.response=response;
if(response!=null)
{
try {
this.out=response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
* 设置ServletRequest,得到request之后就可由request得到HttpSession
*/
public void setServletRequest(HttpServletRequest request) {
this.request=request;
if(request!=null)
{
this.session=request.getSession();
} } public String execute()
{
/**
* 测试servlet原生对线是否真正获取可以使用
*/
System.out.println(this.request.getContextPath()); //获取工程路径 System.out.println(this.session.getId()); //获取session的id System.out.println(this.servletContext.getRealPath("/index.jsp")); //获取/index.jsp的真实路径 this.out.print("我是PrintWriter类的对象out");
return NONE;
}
}
- 本例只获取了5中servlet原生对象,但只实现了ServletRequestAware,ServletResponseAware,ServletContextAware三个接口,至于printWriter、httpsession对象,则分别由ServletResponseAware对象、ServletRequestAware对象来获取即可。
- ServletContext.getRealPath()获取的是真实路径,即在硬盘上的路径(如C:\Users\Administrator\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\struts_project_2\index.jsp),而request.getContextPath()获得是工程路径(如/struts_project_2)。
B、 使用struts提供的静态类来获取
package com; import java.io.PrintWriter; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class ServletObject extends ActionSupport { public String execute()
{
try{
HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
ServletContext servletContext=ServletActionContext.getServletContext();
PrintWriter out=response.getWriter();
HttpSession session=request.getSession(); /**
* 测试servlet原生对线是否真正获取可以使用
*/
System.out.println(request.getContextPath()); //获取工程路径
System.out.println(session.getId()); //获取session的id
System.out.println(servletContext.getRealPath("/index.jsp")); //获取/index.jsp的真实路径
out.print("我是PrintWriter类的对象out");
}
catch(Exception e)
{
e.printStackTrace();
} return NONE;
}
}
ServletActionContext 这个静态类提供了Servlet原生对象的创建等。
在Action中获取servlet原生对象的总结:
一般来说用第一种方法来获取,但是这种方法每个Action都要写一大堆代码很麻烦,所以这里可以把这些servlet原生对象封装为一个抽象基类,其他Action要使用servlet原生对象的话就继承这个抽象类并实现抽象execute方法即可。
示例:
BaseAction.java
package com; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware; public abstract class BaseAction implements ServletRequestAware,ServletResponseAware,ServletContextAware { HttpServletRequest request;
HttpServletResponse response;
ServletContext servletContext;
HttpSession session;
PrintWriter out; public void setServletContext(ServletContext servletContext) {
this.servletContext=servletContext; } public void setServletResponse(HttpServletResponse response) {
this.response=response;
if(response!=null)
{
try {
this.out=response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
} } public void setServletRequest(HttpServletRequest request) {
this.request=request;
if(request!=null)
{
this.session=request.getSession();
} }
abstract public String execute(); }
3.2 获取请求参数中的值
A、 使用servlet原生对象request来获取
A1. 从GET请求中获取参数 :
get把数据放在网址中,例如:http://www.abc.com/index.php?a=1&b=2 其中?a=1&b=2就是get数据,并且连http://www.abc.com/index.php长度限制在1024个字。
示例:
index.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>
<% String path=request.getContextPath(); %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="<%=path%>/request/getpost!get?userId=001&userName=用户名">用request原生对象来获取GET请求中的参数</a> </body>
</html>
- 解析:“<%=path%>/request/getpost!get?userId=001&userName=用户名 ” 表示在“/request”这个命名空间中找到getpost这个对象,执行这个对象中的get方法,并
把参数userId=001和userName=用户名作为参数传过去。
struts.xml
<?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>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" /> <package name="request" namespace="/request" extends="struts-default" >
<action name="getpost" class="com.ActionRequest"></action>
</package>
</struts>
ActionRequest.java
package com; import java.io.UnsupportedEncodingException; public class ActionRequest extends BaseAction{ @Override
public String execute() { return null;
} public String get() throws UnsupportedEncodingException
{
/**
* 模拟GET请求
*/
String userId=request.getParameter("userId");
String userName=request.getParameter("userName");
userName=new String(userName.getBytes("ISO-8859-1"),"UTF-8");
System.out.println("userId="+userId); System.out.println("userName="+userName); return null;
} public String post()
{
/**
* 模拟POST请求
*/
//TODO
return null;
} }
- 解析: userName=new String(userName.getBytes("ISO-8859-1"),"UTF-8"); 如果没有这行代码,结果的userName值是乱码,尽管我们在struts.xml里设置了<constant name="struts.i18n.encoding" value="UTF-8"></constant> 但是还是乱码,就算再加个 request.setCharacterEncoding("utf-8");也是没用的依旧是乱码,可知
- struts.i18n.encoding=UTF-8和request.setCharacterEncoding("utf-8"); 只针对POST请求有作用,对GET是无效的。
- 对于GET请求乱码问题,可以用 userName=new String(userName.getBytes("ISO-8859-1"),"UTF-8"); 这种方法来做,但是如果每个页面都这样做的话或者说需要转的参数比较多的话就很麻烦,可以用过滤器来解决。
- 过滤器解决GETPOST参数乱码示例见(四)Decorator设计模式解决GET/POST请求的乱码问题
A2.POST请求:
post则是把数据放到http请求中,例如还是传输a=1&b=2,可是网址还是http://www.abc.com/index.php,比如表单的post提交,在网址上我们是看不到用户输入的数据的。
- 示例请见(四)Decorator设计模式解决GET/POST请求的乱码问题
B、 使用属性驱动来获取
- 原则:控件的名称等于Action中成员变量名,并且属性都要有getset方法。
- 示例一:
index.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>
以属性驱动来获取表单值
<form action="request/shuxing!post_2" method="post" >
用户名:<input type="text" name="userName" /> <br/>
密 码:<input type="password" name="passWd"/><br/> 爱 好:<input type="checkbox" name="like" value="篮球"/>篮球
<input type="checkbox" name="like" value="足球"/>足球
<input type="submit" />
</form> </body>
</html>
界面:
struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/request" extends="struts-default">
<action name="shuxing" class="action.AttrrbuteDriver"></action>
</package>
</struts>
- AttrrbuteDriver.java
package action; import java.util.List; public class AttrrbuteDriver extends BaseAction{
/**
* 以属性驱动的方式来获取表单中的值
* 前提是: 1. 控件名(即表单元素名)与Action中的成员变量名要一致
* 2. Action中的成员变量要有getset方法
*/ private String userName; //获取表单元素名为userName的值,
private String passWd; //获取表单元素名为passWd的值
private List<String> like; //也可以用String[]来获取表单元素名为checkbox的值 public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassWd() {
return passWd;
} public void setPassWd(String passWd) {
this.passWd = passWd;
} public List<String> getLike() {
return like;
} public void setLike(List<String> like) {
this.like = like;
} @Override
public String execute() {
System.out.println("execute方法");
return null;
} public String post_2(){
System.out.println(userName);
System.out.println(passWd);
for(String likes:like){
System.out.println(likes);
}
return null;
}
}
如果一个表单元素有很多的时候,那么Action中定义的成员变量就会很多,代码会冗长,所以我们可以把其中一些成员变量封装到JavaBean里,具体如下:
- 属性驱动优化示例:
index.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>
<h3>以属性驱动来获取表单值</h3>
<form action="request/shuxing!post_2" method="post" >
用户名:<input type="text" name="userName" /> <br/>
密 码:<input type="password" name="passWd"/><br/> 爱 好:<input type="checkbox" name="like" value="篮球"/>篮球
<input type="checkbox" name="like" value="足球"/>足球 <h3>注册狗的信息</h3>
dogId:<input type="text" name="dog.dogId" /> <br/>
dogName:<input type="text" name="dog.dogName" /> <br/>
dogColor:<input type="text" name="dog.dogColor" /> <br/>
<input type="submit" />
</form> </body>
</html>
- 注意这里的 name="dog.dogId" 中的“dog.dogId”的写法,dog为Action中成员变量的引用,dogId为javaBean中的成员变量名。
struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/request" extends="struts-default">
<action name="shuxing" class="action.AttrrbuteDriver"></action>
</package>
</struts>
AttrrbuteDriver.java
package action; import java.util.List; import bean.DogBean; public class AttrrbuteDriver extends BaseAction{
/**
* 以属性驱动的方式来获取表单中的值
* 前提是: 1. 控件名(即表单元素名)与Action中的成员变量名要一致
* 2. Action中的成员变量要有getset方法
*/ private String userName; //获取表单元素名为userName的值,
private String passWd; //获取表单元素名为passWd的值
private List<String> like; //也可以用String[]来获取表单元素名为checkbox的值 private DogBean dog; //DogBean是JavaBean public DogBean getDog() {
return dog;
} public void setDog(DogBean dog) {
this.dog = dog;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassWd() {
return passWd;
} public void setPassWd(String passWd) {
this.passWd = passWd;
} public List<String> getLike() {
return like;
} public void setLike(List<String> like) {
this.like = like;
} @Override
public String execute() {
System.out.println("execute方法");
return null;
} public String post_2(){ System.out.println(userName);
System.out.println(passWd);
for(String likes:like){
System.out.println(likes);
}
System.out.println(dog.getDogName());
System.out.println(dog.getDogId());
System.out.println(dog.getDogColor()); return null;
}
}
- DogBean.java
package bean; public class DogBean {
private String dogId;
private String dogName;
private String dogColor;
public String getDogId() {
return dogId;
}
public void setDogId(String dogId) {
this.dogId = dogId;
}
public String getDogName() {
return dogName;
}
public void setDogName(String dogName) {
this.dogName = dogName;
}
public String getDogColor() {
return dogColor;
}
public void setDogColor(String dogColor) {
this.dogColor = dogColor;
}
}
结果:
解析: dogId:<input type="text" name="dog.dogId" /> 中的“dog.dogId”中的dog为Action类(本例中为AttrrbuteDriver类)的成员变量名即private DogBean dog; 而且dog成员变量也要有getset方法。“dog.dogId”中的dogId为JavaBean(本例中为DogBean)的成员变量dogId.
C、 使用模型驱动来获取
- 定义一个模型来接受表单中的数据,前提是Action类实现ModelDriven<类型>,并重写其中的getModel()方法,比如class Action_One ModelDriven<Dog>() 意思是这个Action接受Dog这个模型的数据。
- 每个Action类只能实现一个ModelDriven<类型> 模型,也就是只能接受一个模型的数据。
- 示例:
index.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>
<h3>以模型驱动来获取表单值</h3>
<form action="request/shuxing!post_2" method="post" >
用户名:<input type="text" name="userName" /> <br/>
密 码:<input type="password" name="passWd"/><br/> 爱 好:<input type="checkbox" name="like" value="篮球"/>篮球
<input type="checkbox" name="like" value="足球"/>足球 <h3>注册狗的信息</h3>
dogId:<input type="text" name="dogId" /> <br/>
dogName:<input type="text" name="dogName" /> <br/>
dogColor:<input type="text" name="dogColor" /> <br/>
<input type="submit" />
</form> </body>
</html>
界面:
struts.xml和DogBean.java与前一个例子一样,
package action; import java.util.List; import com.opensymphony.xwork2.ModelDriven; import bean.DogBean; public class AttrrbuteDriver extends BaseAction implements ModelDriven<DogBean>{
/**
* 模型驱动方法来获取表单元素值
* 1. 实现ModelDriven<类型>,并重写其中的getModel()方法
* 2. 本例中AttrrbuteDriver接受Dog这个模型的数据,且实现一个接口,即这个类只能接受一个数据模型,
* 如果再加implements ModelDriven<DogBean>,ModelDriven<UserBean>
* 这是错的,虽然平常来说语法是正确的。
*/ private String userName; //获取表单元素名为userName的值,
private String passWd; //获取表单元素名为passWd的值
private List<String> like; //也可以用String[]来获取表单元素名为checkbooks的值 private DogBean dog; public DogBean getModel() {
this.dog=new DogBean();
return dog;
} public DogBean getDog() {
return dog;
} public void setDog(DogBean dog) {
this.dog = dog;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassWd() {
return passWd;
} public void setPassWd(String passWd) {
this.passWd = passWd;
} public List<String> getLike() {
return like;
} public void setLike(List<String> like) {
this.like = like;
} @Override
public String execute() {
System.out.println("execute方法");
return null;
} public String post_2(){ System.out.println(userName);
System.out.println(passWd);
for(String likes:like){
System.out.println(likes);
}
System.out.println(dog.getDogName());
System.out.println(dog.getDogId());
System.out.println(dog.getDogColor()); return null;
}
}
结果:
(三)Struts之Action类基础(一)的更多相关文章
- (五)Struts之Action类基础(二)
上一章节末((三)Struts之Action类基础(一))介绍了如何获取用户输入数据的获取.接着就是在Struts中怎么把数据响应给用户端,这就必须要求我们把数据放到作用域中,然后才能显示到用户浏览器 ...
- 初次了解struts的action类
Action类真正实现应用程序的事务逻辑,它们负责处理请求.在收到请求后,ActionServlet会为这个请求选择适当的Action 如果需要,创建Action的一个实例 调用Action的perf ...
- struts(三)——struts框架实现登录示例
前两篇讲解了struts框架的基本实现,自己感觉讲的也有些枯燥,今天拿登录的例子来做一个实现. 1.新建一个javaweb项目,并将struts的jar包拷贝到WebRoot/WEB-INF/lib下 ...
- Struts2 之 Action 类访问 WEB 资源
接着上次博客的内容我继续分享我所学到的知识,和自己在学习过程中所遇到问题以及解决方案.当然,如果读者发现任何问题均可以在下方评论告知我,先谢! 在 Action 中访问 WEB 资源 web 资源 所 ...
- Struts2第三篇【Action开发方式、通配符、Struts常量、跳转全局视图、action节点默认配置】
前言 上篇Struts博文已经讲解了Struts的开发步骤以及执行流程了-..对Struts的配置文件有了了解-..本博文继续讲解Struts在配置的时候一些值得要学习的细节- Action开发的三种 ...
- JavaWeb_(Struts2框架)Struts创建Action的三种方式
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- stuts1:(Struts)Action类及其相关类
org.apache.struts.action.Action类是Struts的心脏,也是客户请求和业务操作间的桥梁.每个Action类通常设计为代替客户完成某种操作.一旦正确的Action实例确定, ...
- C#_02.13_基础三_.NET类基础
C#_02.13_基础三_.NET类基础 一.类概述: 类是一个能存储数据和功能并执行代码的数据结构,包含数据成员和函数成员.(有什么和能够干什么) 运行中的程序是一组相互作用的对象的集合. 二.为类 ...
- salesforce lightning零基础学习(十) Aura Js 浅谈三: $A、Action、Util篇
前两篇分别介绍了Component类以及Event类,此篇将会说一下 $A , Action以及 Util. 一. Action Action类通常用于和apex后台交互,设置参数,调用后台以及对结 ...
随机推荐
- 腾讯基于 Flink 的实时流计算平台演进之路
https://mp.weixin.qq.com/s/MGnG_Mpf6CUQWLJHvmWqLA
- Discretized Streams: A Fault-Tolerant Model for Scalable Stream Processing
https://www2.eecs.berkeley.edu/Pubs/TechRpts/2012/EECS-2012-259.pdf Discretized Streams: A Fault-Tol ...
- ISO/IEC 9899:2011 条款6.2.3——标识符的名字空间
6.2.3 标识符的名字空间 1.如果一个特定标识符的多个声明在一个翻译单元的任意一点可见,那么语法上下文会区分对不同实体的引用.从而,对于标识符各种不同的类别具有独立的名字空间: ——标签名(通过标 ...
- 使用C语言 判断当前网络是否联通
方式一: int GetNetStat( ) { char buffer[BUFSIZ]; FILE *read_fp; int chars_read; int ret; try { memset( ...
- VCTravel
#pragma once #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> #inclu ...
- 一个小时学会Git(转载)
---恢复内容开始--- 一个小时学会Git 最近要与部门同事一起做技术分享,我选择了Git,因为Git 是一种在全球范围都广受欢迎的版本控制系统.在开发过程中,为了跟踪代码,文档,项目等信息 ...
- springboot下jar包方式运行Caused by: java.lang.ExceptionInInitializerError: null
idea调试过程中不会出现此问题,异常如下 org.springframework.beans.factory.BeanCreationException: Error creating bean w ...
- MySQL必知必会:组合查询(Union)
MySQL必知必会:组合查询(Union) php mysqlsql 阅读约 8 分钟 本篇文章主要介绍使用Union操作符将多个SELECT查询组合成一个结果集.本文参考<Mysql ...
- spark osx:WARN NativeCodeLoader:62 - Unable to load native-hadoop library for your platform
spark-env.sh文件中增加,确保${HADOOP_HOME}/lib/native目录下有libhadoop.so文件 export JAVA_LIBRARY_PATH=${HADOOP_HO ...
- weblogic12.1.3安装
weblogic weblogic12.1.3安装 环境: centos7.5 ip: 192.168.0.94 1.安装jdk 2.安装 weblogic 下载.解压安装包 wls1213_dev. ...