现在的位置: 首页 > 综合 > 正文

MVC框架

2018年02月13日 ⁄ 综合 ⁄ 共 5869字 ⁄ 字号 评论关闭

ActionDTO.java

public class ActionDTO {

private String path;
private String className;
private boolean redirect;

public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public boolean isRedirect() {
return redirect;
}
public void setRedirect(boolean redirect) {
this.redirect = redirect;
}

DataSourceDTO.java

public class DataSourceDTO {

private String driver;
private String url;
private String username;
private String password;
private int maxActive;

public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getMaxActive() {
return maxActive;
}
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
}

DataSourceFactory.java

public class DataSourceFactory {
private static BasicDataSource bds;
public static void initDataSource(){
if(bds==null){
bds=new BasicDataSource();
DataSourceDTO dto=MVCConfig.getInstance().getDatasource();
bds.setDriverClassName(dto.getDriver());
bds.setUrl(dto.getUrl());
bds.setUsername(dto.getUsername());
bds.setPassword(dto.getPassword());
bds.setMaxActive(dto.getMaxActive());
}
}
public static BasicDataSource getDataSource(){
return bds;
}
public static void main(String[] args) {
try {
Connection con=DataSourceFactory.getDataSource().getConnection();
System.out.println(con.isClosed());
} catch (SQLException e) {
e.printStackTrace();
}
}
}

MVCConfig.java

/**
 * @author Administrator
 *解析xml配置文件,保存解析结果
 */
public class MVCConfig {

private static MVCConfig instance = new MVCConfig();

private MVCConfig(){

}
public static MVCConfig getInstance(){
return instance;
}

private DataSourceDTO datasource = new DataSourceDTO();
private Map<String,ActionDTO> actions = new HashMap<String, ActionDTO>();

public DataSourceDTO getDatasource() {
return datasource;
}

public Map<String, ActionDTO> getActions() {
return actions;
}
public void parse(String path){
//产生一个解析器
Digester digester = new Digester();
//指定回调方法所属的对象
digester.push(this);
//声明当解析器遇到某个节点时做什么事
digester.addCallMethod("mvc-config/data-sources/data-source", "addDataSource",5);
digester.addCallParam("mvc-config/data-sources/data-source/driver", 0);
digester.addCallParam("mvc-config/data-sources/data-source/url", 1);
digester.addCallParam("mvc-config/data-sources/data-source/username", 2);
digester.addCallParam("mvc-config/data-sources/data-source/password", 3);
digester.addCallParam("mvc-config/data-sources/data-source/maxActive", 4);


digester.addCallMethod("mvc-config/action-mappings/action", "addAction",3);
digester.addCallParam("mvc-config/action-mappings/action", 0, "path");
digester.addCallParam("mvc-config/action-mappings/action", 1, "className");
digester.addCallParam("mvc-config/action-mappings/action", 2, "redirect");
try {
//开始解析
digester.parse(new File(path));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void addAction(String path,String className,String redirect){
ActionDTO action = new ActionDTO();
action.setPath(path);
action.setClassName(className);
action.setRedirect(Boolean.parseBoolean(redirect));
actions.put(path, action);
}

public void addDataSource(String driver,String url,String username,String password,String maxActive){
datasource.setDriver(driver);
datasource.setUrl(url);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setMaxActive(Integer.parseInt(maxActive));
}

public static void main(String[] args) {
MVCConfig.getInstance().parse("f:\\mvc-config.xml");
String className = MVCConfig.getInstance().getActions().get("productPreAdd").getClassName();
System.out.println(className);
}
}

Action.java

public abstract class Action {
public abstract String execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException;
public Connection getConnection(){
Connection con= null;
try {
con=DataSourceFactory.getDataSource().getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
}

ActionServlet.java

public class ActionServlet extends HttpServlet {

private Map<String,ActionDTO> config = null;

@Override
public void init() throws ServletException {
//
String contextPath = this.getServletContext().getRealPath("/");
String path = this.getInitParameter("config");
MVCConfig.getInstance().parse(contextPath+path);
DataSourceFactory.initDataSource();
config = MVCConfig.getInstance().getActions();
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//解析用户请求,获得请示路径path
String uri = request.getRequestURI();
// String url = request.getRequestURL().toString();
System.out.println(uri);
// System.out.println(url);
int start = uri.lastIndexOf("/")+1;
int end = uri.lastIndexOf(".");
String path = uri.substring(start,end);
System.out.println(path);
//多态+反射 构建最灵活最具扩展性的框架
Action action = null;
ActionDTO actionDTO=config.get(path);
try {
action = (Action) Class.forName(actionDTO.getClassName()).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// if("productList".equals(path)){
// action = new ProductListAction();
// //action.execute(request, response);
// }else if("productPreAdd".equals(path)){
// action = new ProductPreAddAction();
// }
// else if("productAdd".equals(path)){
// action = new ProductAddAction();
// }
String forward=action.execute(request, response);
if(actionDTO.isRedirect()){
response.sendRedirect(forward);
}
else{
request.getRequestDispatcher(forward).forward(request, response);
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

【上篇】
【下篇】

抱歉!评论已关闭.