Struts2入门(一)
(一)利用Filter实现MVC模式
为了便于理解struts2的原理,我们先来看一个简单的例子,本例子不使用struts2而直接使用自己编写的Filter类实现MVC模式
环境配置
- java开发环境(JDK、Myeclipse、tomcat服务器)
web-project 工程及文件
源码及说明
下面根据开发过程写出源码冰女简要说明
- web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name></display-name>
<!-- 了解任何一个web工程,首先需要看web.xml这个基本配置文件 ,此配置文件的功能是将所有以
.action 结尾的请求过滤并转到我们创建的 com.struts2.HelloWord.FilterDispatcher 类处理-->
<filter>
<filter-name>FilterDispatcher</filter-name>
<filter-class>com.struts2.HelloWord.FilterDispatcher</filter-class>
</filter>
<filter-mapping >
<filter-name>FilterDispatcher</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<!-- 对于例子中的jsp页面 全部只需看 body 部分 其他部分为自动生成
,若页面存在乱码 尝试将更改此处即可 pageEncoding="ISO-8859-1" ,
下面的连接即为ServletPath
-->
<body>
输入PRODUCT:
<a href="product_input.action">product_input</a>
<br><br>
显示PRODUCT:
<a href="product_detail.action">product_input</a>
<br><br>
This is my JSP page. <br>
</body>
</html>
- detail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'detail.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
productId: ${ requestScope.product.productId}
<br><br>
productName: ${ requestScope.product.productName}
<br><br>
productPrice: ${requestScope.product.productPrice}
<br><br>
</body>
</html>
- input.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'input.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="product_save.action" method="post">
productName:<input type="text" name="productName"/> <br><br>
productDesc:<input type="text" name="productDesc"/> <br><br>
productPrice:<input type="text" name="productPrice"/> <br><br>
<input type="submit" value="submit"/> <br><br>
</form>
</body>
</html>
- product.java
package com.struts2.HelloWord;
public class Product {
private Integer productId;
private String productName;
private String productDesc;
private Double productPrice;
public Product(){}
public Product(Integer invalid, String productName, Double productPrice,
String productDesc) {
super();
this.productId = invalid;
this.productName = productName;
this.productPrice = productPrice;
this.productDesc = productDesc;
}
@Override
public String toString() {
return "Product [productId=" + productId + ", productName="
+ productName + ", productDesc=" + productDesc
+ ", productPrice=" + productPrice + "]";
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Double getProductPrice() {
return productPrice;
}
public void setProductPrice(Double productPrice) {
this.productPrice = productPrice;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
}
- FilterDispacher.java
- `/**
-
*/
package com.struts2.HelloWord;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
-
public class FilterDispatcher implements Filter {
/* 此处的类自己定义 继承Fileter接口之后 点击快捷键 shift+alt+s 选择添加接口方法
* 可以自动添加以下三个函数 . 每次得到请求 ,FilterDispather类都会新生成一个实例 */
public void destroy() { }
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
/* 为了调用getServletPath()方法 得到请求的 servletPath 把请求转换为HttpServletRequest*/
HttpServletRequest req=(HttpServletRequest)arg0;
String servletPath=req.getServletPath();
System.out.println(servletPath);
String path=null;
/* 根据判断 servletPath 设置跳转页面 ,如果是 product_input.action 则path设置为 input.jsp
* 如果是 product_save.action 则 首先通过getParamer()方法取到请求的参数,并保存在product实例中
* ,给请求添加 product 属性并赋值pd path设置为 detail.jsp页面 */
if("/product_input.action".equals(servletPath)){
path="/WEB-INF/pages/input.jsp";
}
if("/product_save.action".equals(servletPath)){
String productName=arg0.getParameter("productName");
String productDesc=arg0.getParameter("productDesc");
String productPrice=arg0.getParameter("productPrice");
Product pd=new Product(null,productName,Double.parseDouble(productPrice),productDesc);
System.out.println(pd.toString());
pd.setProductId(1001);
arg0.setAttribute("product", pd);
path="/WEB-INF/pages/detail.jsp";
}
if("/product_detail.action".equals(servletPath)){
path="/WEB-INF/pages/detail.jsp";
}
/*最终只要servletPath不为空,即拦截到请求,都会执行forward操作、跳转到相应页面
* */
if(servletPath!=null){
arg0.getRequestDispatcher(path).forward(arg0, arg1);
return;
}
}
public void init(FilterConfig arg0) throws ServletException {
}
}
`
NOTES
- 本例子通过使用 FilterDispacher类实现Filter接口,通过if语句实现了请求的过滤和转发,并且加入了简单的信息设置与传递。
- 而对于大家感兴趣的struts2则提供了封装好的jar包直接实现了本例 FilterDispacher的功能,并且结合struts.xml配置文件 更简单便利的实现了MVC结构。
- 下面的一个笔记会使用struts2实现一个简单的例子