主控制器主控制器的DispathcherServlet类
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 723689920988912828 L;
private ConfigModel ConfigModel = null;
@Override
public void init ( ) throws ServletException {
try {
String xmlPath = this . getInitParameter ( "xmlPath" ) ;
if ( xmlPath == null || "" . equals ( xmlPath) )
ConfigModel = ConfigModelFactory. build ( ) ;
else
ConfigModel = ConfigModelFactory. build ( xmlPath) ;
} catch ( Exception e) {
}
}
@Override
protected void doGet ( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost ( req, resp) ;
}
@Override
protected void doPost ( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
init ( ) ;
String url = req. getRequestURI ( ) ;
url = url. substring ( url. lastIndexOf ( "/" ) , url. lastIndexOf ( "." ) ) ;
ActionModel actionModel = ConfigModel. pop ( url) ;
if ( actionModel == null) {
throw new RuntimeException ( "你没有配置对应的子控制器Action!!!" ) ;
}
try {
Action newInstance = ( Action) Class. forName ( actionModel. getType ( ) ) . newInstance ( ) ;
if ( newInstance instanceof ModelDriven ) {
ModelDriven modelDriven = ( ModelDriven) newInstance;
Object model = modelDriven. getModel ( ) ;
BeanUtils. populate ( model, req. getParameterMap ( ) ) ;
}
String code = newInstance. execute ( req, resp) ;
ForwardModel forwardModel = actionModel. pop ( code) ;
if ( forwardModel == null) {
throw new RuntimeException ( "你没有配置对应的子控制器Action的处理方式的forward!!!" ) ;
}
String jspPath = forwardModel. getPath ( ) ;
if ( forwardModel. isRedirect ( ) ) {
resp. sendRedirect ( jspPath) ;
} else {
req. getRequestDispatcher ( jspPath) . forward ( req, resp) ;
}
}
catch ( InvocationTargetException e) {
e. printStackTrace ( ) ;
}
catch ( InstantiationException e) {
e. printStackTrace ( ) ;
} catch ( IllegalAccessException e) {
e. printStackTrace ( ) ;
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( SecurityException e) {
e. printStackTrace ( ) ;
}
}
}
集合方法封装的泛型方法CalAction
public interface Action {
String execute ( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
public class ActionSupport implements Action {
@Override
public final String execute ( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String methodName = req. getParameter ( "methodName" ) ;
String code = null;
try {
Method method = this . getClass ( ) . getDeclaredMethod ( methodName,
HttpServletRequest. class ,
HttpServletResponse. class ) ;
method. setAccessible ( true ) ;
code = ( String) method. invoke ( this , req, resp) ;
} catch ( IllegalAccessException e) {
e. printStackTrace ( ) ;
} catch ( IllegalArgumentException e) {
e. printStackTrace ( ) ;
} catch ( InvocationTargetException e) {
e. printStackTrace ( ) ;
} catch ( NoSuchMethodException e) {
e. printStackTrace ( ) ;
} catch ( SecurityException e) {
e. printStackTrace ( ) ;
}
return code;
}
}
public interface ModelDriven < T> {
T getModel ( ) ;
}
public class CalAction extends ActionSupport implements ModelDriven < Cal> {
private Cal cal = new Cal ( ) ;
public String add ( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req. setAttribute ( "res" ,
Integer. valueOf ( cal. getNum1 ( ) ) + Integer. valueOf ( cal. getNum2 ( ) ) ) ;
return "calRes" ;
}
public String del ( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req. setAttribute ( "res" ,
Integer. valueOf ( cal. getNum1 ( ) ) - Integer. valueOf ( cal. getNum2 ( ) ) ) ;
return "calRes" ;
}
public String chen ( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req. setAttribute ( "res" ,
Integer. valueOf ( cal. getNum1 ( ) ) * Integer. valueOf ( cal. getNum2 ( ) ) ) ;
return "calRes" ;
}
public String chu ( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req. setAttribute ( "res" ,
Integer. valueOf ( cal. getNum1 ( ) ) / Integer. valueOf ( cal. getNum2 ( ) ) ) ;
return "calRes" ;
}
@Override
public Cal getModel ( ) {
return cal;
}
}
xml的配置
< config>
! -- 独立的包类的来配置xml文件
使得框架的配置文件可变 同区域的名的配置文件
将Action的信息配置到xml ( 反射实例化) -- >
< action path= "/Cal" type= "com.web.CalAction" >
< forward name= "calRes" path= "calRes.jsp" redirect= "false" / >
< / action>
< / config>
主界面cal.jsp
< % @ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" % >
< ! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html>
< head>
< meta http- equiv= "Content-Type" content= "text/html; charset=UTF-8" >
< title> Insert title here< / title>
< script type= "text/javascript" >
function doSb ( val) {
if ( val == 1 ) {
calForm. methodName. value = "add" ;
}
else if ( val == 2 ) {
calForm. methodName. value = "del" ;
}
else if ( val == 3 ) {
calForm. methodName. value = "chen" ;
}
else if ( val == 4 ) {
calForm. methodName. value = "chu" ;
}
calForm. submit ( ) ;
}
< / script>
< / head>
< body>
< form id= "calForm" name= "calForm" action= "${pageContext.request.contextPath }/Cal.action" >
num1: < input type= "text" name= "num1" > < br>
num2: < input type= "text" name= "num2" > < br>
< input type= "hidden" name= "methodName" >
< button onclick= "doSb(1)" > + < / button>
< button onclick= "doSb(2)" > - < / button>
< button onclick= "doSb(3)" > * < / button>
< button onclick= "doSb(4)" > / < / button>
< / form>
< / body>
< / html>