浅谈Struts1.x和Struts2.0的区别
好久没有写csdn的博客了,同时也就是意味着好久没有学习了。刚刚从学校搞完毕业论文回来,心有点散。今天下午重新开始寻找以前的学习状态,现在就关于struts1.x和Struts2.0的一些区别。
在讲述其相关不同的时候我们需要先明白几个概念,ActionServlet、Action、ActionForm。其中ActionServlet是Struts1.x中的概念,其是关于Servlet和JSP的综合。Action的话则两者都会拥有。ActionForm是Struts1.x中所特有的用户对jsp中的Form表单数据进行的封装,在此,其与struts2.0有很大不同,Struts2.0中则是通过在extends ActionSupport的时候直接定义属性或者对象来封装数据的,但是Struts1.x中则需要封装一个ActionForm对象用于和JSP页面中的Form表单数据进行封装,否则无法直接映射。这也是其两者的最大区别。
Struts1.x的工作原理如下:
JSP页面发送一个action=“XXX.do”的请求------>WEB-INF/web.xml配置文件中查找对应的配置------>找到struts1.x中的org.apache.struts.action.ActionServlet过滤器进行处理----->根据请求的JSP页面的XXX.do进行相应的action--mapping处理,跳到相应的XXAction进行处理(其处理的时候需要更具XXForm进行数据封装)----->返回相应的处理结果,mapping.findForward(String name)----->进行相应的结果页面显示。
一下为代码示例:
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<!-- 一经启动tomcat服务器就加载web-inf下面的struts-config.xml配置文件 -->
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<!-- 一启动服务器就加载配置文件 -->
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<!-- 一经启动tomcat服务器就加载web-inf下面的struts-config.xml配置文件 -->
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<!-- 一启动服务器就加载配置文件 -->
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
struts-config.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.clark.form.LoginForm"></form-bean>
</form-beans>
<!-- 配置action处理的Form -->
<action-mappings>
<!-- path指定其页面跳转的form中action名称 因为web.xml中配置的是*.do,故为login.do -->
<action path="/login" type="com.clark.action.LoginAction" name="loginForm">
<!-- 登陆成功的跳转页面 -->
<forward name="success" path="/loginSuccess.jsp"></forward>
<!-- 失败页面 -->
<forward name="failure" path="/loginFailure.jsp"></forward>
</action>
</action-mappings>
</struts-config>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.clark.form.LoginForm"></form-bean>
</form-beans>
<!-- 配置action处理的Form -->
<action-mappings>
<!-- path指定其页面跳转的form中action名称 因为web.xml中配置的是*.do,故为login.do -->
<action path="/login" type="com.clark.action.LoginAction" name="loginForm">
<!-- 登陆成功的跳转页面 -->
<forward name="success" path="/loginSuccess.jsp"></forward>
<!-- 失败页面 -->
<forward name="failure" path="/loginFailure.jsp"></forward>
</action>
</action-mappings>
</struts-config>
相关的Form类
package com.clark.form;
import org.apache.struts.action.ActionForm;
/**
* 封装表单数据的一个form,用于和页面中的数据想对应
* 其不同于struts2.0,其不需要定义一个ActionForm
* @author Administrator
*
*/
public class LoginForm extends ActionForm {
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;
}
}
import org.apache.struts.action.ActionForm;
/**
* 封装表单数据的一个form,用于和页面中的数据想对应
* 其不同于struts2.0,其不需要定义一个ActionForm
* @author Administrator
*
*/
public class LoginForm extends ActionForm {
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;
}
}
相关的Action对象
package com.clark.form;
import org.apache.struts.action.ActionForm;
/**
* 封装表单数据的一个form,用于和页面中的数据想对应
* 其不同于struts2.0,其不需要定义一个ActionForm
* @author Administrator
*
*/
public class LoginForm extends ActionForm {
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;
}
}
import org.apache.struts.action.ActionForm;
/**
* 封装表单数据的一个form,用于和页面中的数据想对应
* 其不同于struts2.0,其不需要定义一个ActionForm
* @author Administrator
*
*/
public class LoginForm extends ActionForm {
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;
}
}
登录页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'index.jsp' starting page</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">
</head>
<body>
<form action="login.do" method="post">
username:<input type="text" name="username" /><br/>
password:<input type="password" name="password" /><br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
<%
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>My JSP 'index.jsp' starting page</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">
</head>
<body>
<form action="login.do" method="post">
username:<input type="text" name="username" /><br/>
password:<input type="password" name="password" /><br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
登录成功页面:和登录失败页面仅仅是简单的页面显示。。。。。此处忽略