从头学起:struts2(1)
从头学起:struts2(1)
从页面跳转说起:先看一个例子,使用通常的方式进行页面跳转。
建立如下两个页面:
first.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> first </title>
</head>
<body>
<h1>这里是:first.jsp </h1>
<s:form action="second.jsp">
<s:submit value="提交到second.jsp" />
</s:form>
</body>
</html>
second.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>second</title>
</head>
<body>
<h1>这里是:second.jsp!</h1>
<s:a href="first.jsp">点击这里转到first.jsp</s:a>
</body>
</html>
在first.jsp中使用submit提交form表单的方式跳转到secont.jsp页面,而second.jsp页面使用超链接的方式跳转到了first.jsp页面。在上面的页面中,我们使用了struts2的标签。
这样的方式造成了页面之间的紧密耦合,当页面众多时,会造成非常复杂的耦合关系;如果涉及到数据层的话还会造成页面和数据层的紧密耦合,不符合分层原则。
下面我们使用strts2的结构进行页面跳转。首先我们创建一个struts.xml文件(当然应该先导入struts2框架所需的包,导入那些包网上搜一下即可):
<struts>
<package name="default" extends="struts-default">
<action name="second" class="action.myAction">
<result name="msg">/second.jsp</result>
</action>
<action name="first" class="action.myAction">
<result name="msg">/first.jsp</result>
</action>
</package>
</struts>
文件中主要的标签是:<action>,其name属性表示页面发出的请求“信号”,class属性表示控制中心接收到请求“信号”后应该作出的“动作”,其值为包名加类名;<action>的子标签<result>表示“动作”的“结果”,其属性name表示接收到的“动作”的“结果”,标签里的文本表明应跳转的“方向”。由此可以解读文件中的第一段内容:当接收到一个名为:“second”的信号后,执行“action”包中的“myAction”动作,当返回一个“msg”字符串时跳转到second.jsp页面。文件中的第二段内容以此类推。
但是谁会把“信号”送过来呢?答案是web.xml文件,我们在其中添加如下内容:
<filter>
<filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
上面文本表示:系统中有一个名为struts2的过滤器,要对发出了“*.action”或“*.jsp”的请求,将此请求发送到struts.xml进行匹配,其中“*”为通配符。
接着我们创建一个包:action,在包中创建一个action类:myAction,代码如下:
package myaction;
public class myAction {
public String execute() throws Exception {
return "msg";
}
}
为了简单起见,不论什么情况一律返回一个“msg”字符串。运行一下项目,就会得到和原来一样的效果。但是怎么知道这样的跳转是经过了“控制”的呢?我们不妨将struts.xml中的语句:
<result name="msg">/second.jsp</result>
改为:
<result name="msg">/third.jsp</result>
于是当页面发出“second.jsp”请求时,经过“控制”后,到达了“third.jsp”。
完成了跳转的任务后,还有一个问题没有解决:数据传输,我们常常需要在页面跳转的时候进行数据的传输。下面我们看一下struts2怎么样进行数据的传输。
首先修改first.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>first</title>
</head>
<body>
<s:form action="myaction.action">
<s:textfield name="userName" label="姓名"/>
<s:password name="userPassword" label="密码" />
<s:submit value="提交"/>
</s:form>
</body>
</html>
在这里我们将action属性的值按照通常的做法,写成和action类相近的名称,扩展名一律为:.action。
在action类中做如下修改:
package myaction;
import com.opensymphony.xwork2.ActionContext;
public class myAction {
private String userName;
private String userPassword;
private String mymsg;
public String execute() throws Exception {
ActionContext.getContext().getSession().put("user", getUserName());
ActionContext.getContext().getSession().put("pass", getUserPassword());
mymsg="欢迎"+userName+"来访!";
return "msg";
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getMymsg() {
return mymsg;
}
}
action类通过其属性userName、userPassword、mymsg与页面进行数据交换,注意与页面的控件name的值一定相同。同时又可以通过action上下文的Session对象进行数据传输。
修改second.jsp以接收经过“控制”中心处理过的数据:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>second</title>
</head>
<body>
<h1>欢迎,${sessionScope.user},您已经登录!${sessionScope.pass}</h1>
<h2><s:property value="mymsg"/></h2>
<s:a href="first.jsp">点击这里转到first.jsp</s:a>
</body>
</html>
jsp文件使用了两种方式接收来自action的数据。
解析得很详细,不错