struts1开发学习笔记
使用一个简单的例子记录struts1开发过程。
1、 描述
主要功能描述:
index界面上只有两个超链接,分别可以打开录入产品界面和显示产品列表界面。
newProduct.jsp是录入的界面,archProduct.jsp是查询列表的界面。
ProductAction.java是处理逻辑的action
简单描述流程图:
2、 构建环境
在Myeclip中,新建一个工程StrutsTest,选择该工程,右键Myeclip->add sruts capapibality,弹出界面,选择struts1.2支持。如下图所示:
添加完struts支持后,lib目录下会增添struts的jar包、增添struts的配置文件:/l、同时在l中增添了struts的配置。
3、 代码编写
1) 创建表结构,只有三个字段
create table t_product
(
product_id varchar2(4) primary key,
product_name varchar2(50) not null,
price float
);
2) 创建工具类Util.java,从数据库连接池中获得连接。需要两个步骤
第一步:在WebRoot/META-INF/下新建一个context文件,通过jndi方式配置数据源,l文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
引号的作用有哪些
<Resource
auth="Container"
name="jdbc/StrutsTestPool"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
urname="apps"
password="apps"
maxActive="20"/>
</Context>
第二步:在util类中读取配置获得连接,util.java如下:
package pany.struts.util;
import java.sql.Connection;
import javax.naming.Context;
《心经》全文import javax.naming.InitialContext;借据模板
import javax.sql.DataSource;
public class Util {
/**
* 通过连接池得到连接
* @return
* @throws Exception
*/
public static Connection getPoolConnection() throws Exception{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:/comp/env/jdbc/StrutsTestPool");
Connection conn = ds.getConnection();
return航天日是几月几日 conn;
}
}
3) 创建工具类EncodingFilter.java,自定义过滤器,并在l中配置,参考l配置中内容。EncodingFilter.java的内容如下:
package pany.struts.util;
import java.io.IOException;
import javax.rvlet.Filter;
import javax.rvlet.FilterChain;
import javax.rvlet.FilterConfig;
import javax.rvlet.ServletException;
import javax.rvlet.ServletRequest;
import javax.rvlet.ServletRespon;
public class EncodingFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletRespon respon,
FilterChain chain) throws IOException, ServletException {
request.tCharacterEncoding("UTF-8");
chain.doFilter(request, respon);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
4) 修改l,指定首页为index.jsp。文件内容如下:最后定义了一个fileld的过滤器,是用来过滤字符编码的,后面会提到。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="/xml/ns/javaee" xmlns:xsi="/2001/XMLSchema-instance" version="2.5" xsi:schemaLoca
tion="/xml/ns/javaee /xml/ns/javaee/web-app_2_5.xsd">
<rvlet>
<rvlet-name>action</rvlet-name>
<rvlet-class>org.apache.struts.action.ActionServlet</rvlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/l</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</rvlet>
<rvlet-mapping>
<rvlet-name>action</rvlet-name>
<url-pattern>*.action</url-pattern>
</rvlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 自定义的fileld 用来过滤字符编码 -->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>pany.struts.util.EncodingFilter</filter-class>
</filter>
<filter-mapping>
作文报 <filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5) 在WebRoot下打开index.jsp,去掉无用信息,写代码如下:
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>孝道名言佳句
<title></title>
</head>
<body>
<br>
<br>
<br>
<table width = "500px" align = "center" height = "200px">
<tr><td>
<a href = "productAction.action?method=viewAddForm" target="black">input</a> <br /><br />
<a href = "productAction.action?method=query" target="black">arch</a>
</td></tr>
</table>
</body>
</html>
6) index.jsp中提到productAction.action?method=viewAddForm,需要在struts的配置文件l中配置实现的action,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="productForm" type="pany.struts.vo.ProductVo" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/productAction" type="pany.struts.action.ProductAction" 枕头可以洗吗parameter = "method" scope="request" name="productForm">
<forward name="viewNewProduct" path="/newProduct.jsp"></forward>
<forward name="viewSearchProduct" path="/archProduct.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="pany.struts.ApplicationResources" />
</struts-config>
在配置文件中配置了action的实现类,以及作为上传数据时用来保存数据的form: productForm
7) Action处理类ProductAction.java如下:
package pany.struts.action;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.rvlet.http.HttpServletRequest;
import javax.rvlet.http.HttpServletRespon;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import pany.struts.util.Util;
import pany.struts.vo.ProductVo;
public class ProductAction extends DispatchAction {
/**
* 转到新增界面
*/
public ActionForward viewAddForm(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletRespon respon)
throws Exception {
return mapping.findForward("viewNewProduct");
}
/**
小学语文课标* 转到查询界面
*/
public ActionForward viewSearchForm(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletRespon respon)
throws Exception {
return mapping.findForward("viewSearchProduct");
}