从头学起: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" 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的数据。
?