自己制作的类似Struts1.x 简单web框架
1.利用servelt ,反射 实现 通过url 映射到 action类的方法中。
这个是在我参加工作那年写的,最近工作需要,写一个不使用 struts,spring,hibernate等技术的web框架,用于公司的一个非常小的小系统。
我写了一个框架,然后把之前写的struts 拿出来 简单修改了一下,看了看,自己之前写的代码 有些地方太幼稚了。
呵呵。。
--------------------
我在这列出:web.xml 修改:
<context-param>
<param-name>mjp-config</param-name>
<param-value>mjp-config-*.xml</param-value>
</context-param>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.mjp.core.struts.action.ActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.mjp.core.struts.listener.ConfigListener</listener-class>
</listener>
-------------------
我的struts 配置文件写法;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE action-mappings [
<!ELEMENT action-mappings (action+)>
<!ELEMENT action (forward+)>
<!ATTLIST action path CDATA #REQUIRED>
<!ATTLIST action type CDATA #REQUIRED>
<!ATTLIST forward name CDATA #REQUIRED>
<!ATTLIST forward path CDATA #REQUIRED>
<!ATTLIST forward type CDATA #IMPLIED default (redirect|forward) "forward">
]>
<action-mappings>
<action path="/login" type="com.mjp.system.action.LoginAction" >
<forward name="success" path="index.do?method=doWelcome" type="redirect"/>
<forward name="input" path="/pages/system/login.jsp" />
</action>
<action path="/index" type="com.mjp.system.action.IndexAction" >
<forward name="input" path="/pages/system/index.jsp" />
<forward name="login" path="login.do?method=doInput" type="redirect"/>
</action>
</action-mappings>
-----------------
比如jsp调用写法:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.mjp.core.util.StringUtil"%>
<%
String path = request.getContextPath();
String error = StringUtil.nvl(request.getAttribute("error"));
String login_account = StringUtil.nvl(request.getAttribute("login_account"));
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>后台登录</title>
<link rel="stylesheet" type="text/css" href="resources/css/base.css"/>
</head>
<body id="body" scroll="no">
<center>
<form id="form_login" action="system/login.do?method=doLogin" method="post">
<div style="height:100px;" align="center">
</div>
<table style="width:300px;" align="center" cellpadding="5px" cellspacing="2px">
<tbody>
<tr>
<td colspan="4" height="31" align="center" valign="bottom">
<img src="resources/images/system/login_title.gif" width="180" height="30" />
</td>
</tr>
<tr>
<td height="5px" colspan="4"> </td>
</tr>
<tr>
<td width="132" height="20" align="right" valign="middle" align="left" valign="middle">
<input id="login_account" name="login_account" type="text" value="<%=login_account%>" />
</td>
</tr>
<tr>
<td height="20" align="right" valign="middle" align="left" valign="middle">
<input id="login_password" name="login_password" type="password" />
</td>
</tr>
<tr>
<td height="5px" colspan="4"> </td>
</tr>
<tr>
<td colspan="4" align="center">
<input type="button" name="btn_login" id="btn_login" value="登录" onclick="javascript:loginSubmit();" />
<input type="reset" name="btn_login_reset" id="btn_login_reset" value="重置" />
</td>
</tr>
</tbody>
</table>
<div style="height:100px;" align="center" type="text/javascript"></script>
<script src="resources/js/validate/jquery.validate.js" type="text/javascript"></script>
<script src="resources/js/validate/locale/messages_cn.js" type="text/javascript"></script>
<script src="resources/js/block/jquery.blockUI.js" type="text/javascript"></script>
<script src="pages/system/login.js" type="text/javascript"></script>
-----------------------
自制struts 源码,请下载附件。