模拟Struts2实现
生活随笔
收集整理的這篇文章主要介紹了
模拟Struts2实现
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文主要是一個(gè)模擬的Struts2的簡(jiǎn)單實(shí)現(xiàn)
真正的MVC架構(gòu)
實(shí)現(xiàn)主要思路
定義一個(gè)過濾器,接收傳遞過去的Action,根據(jù)處理的結(jié)果重定向或者轉(zhuǎn)發(fā)。
首先定義index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title></title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><form action="${pageContext.request.contextPath}/addCustomer.action" method="post">姓名:<input type="text" name="name"/><br/>年齡:<input type="text" name="age"/><br/><input type="submit" value="保存"/></form></body> </html>在web.xml中配置過濾器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>MyStruct2</display-name><filter><filter-name>CenterFilter</filter-name><filter-class>cn.itcast.framework.core.CenterFilter</filter-class> <!-- <init-param>--> <!-- <param-name>aciontPostFix</param-name>--> <!-- <param-value>action,do</param-value>--> <!-- </init-param>--></filter><filter-mapping><filter-name>CenterFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list> </web-app>核心配置文件
<?xml version="1.0" encoding="UTF-8"?> <!-- 核心配置文件 --> <framework><!-- action:name:必須有,且唯一。如同之前請(qǐng)求的什么operationclass:必須有,要執(zhí)行哪個(gè)JavaBean的類名method:可以沒有。沒有的話就是默認(rèn)值execute。JavaBean中對(duì)應(yīng)的操作方法。該方法的特點(diǎn)是:public String addCustomer(){}--><action name="addCustomer" class="cn.itcast.domain.Customer" method="addCustomer"><!-- result:代表著一種結(jié)果。type:可以沒有,默認(rèn)是dispatcher。轉(zhuǎn)向目標(biāo)的類型。dispatcher代表著請(qǐng)求轉(zhuǎn)發(fā)name:必須有。對(duì)應(yīng)的是JavaBean中addCustomer的返回值主體內(nèi)容:必須有。目標(biāo)資源的URI--><result type="redirect" name="success">/success.jsp</result><result type="dispatcher" name="error">/error.jsp</result></action><action name="updateCustomer" class="cn.itcast.domain.Customer" method="updateCustomer"><result type="dispatcher" name="success">/success.jsp</result></action> </framework>核心過濾器實(shí)現(xiàn)
javabean如下
package cn.itcast.domain;import java.io.Serializable;public class Customer implements Serializable {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String addCustomer(){if("wzhting".equals(name)){return "success";}else{return "error";}} }完成
總結(jié)
以上是生活随笔為你收集整理的模拟Struts2实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WordPress环境配置与安装
- 下一篇: Material Design入门(三)