Spring通过方法名加类注解一步到位实现 Mvc

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

不善于写博客,第一篇,介绍下spring强大的类名加方法名实现极度简洁的mvc。正文如下:

1:对web项目加入spring支持,不罗嗦哪些jar包依赖了,仅仅展示web.xml的spring支持配置


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

version="2.4">

Web

30

Spring配置文件位置

contextConfigLocation

/WEB-INF/applicationContext.xml

Spring配置加载器

org.springframework.web.context.ContextLoaderListener

App监听器

com.gyx.web.listener.AppListener

sessionFilter

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

singleSession

true

flushMode

AUTO

字符集设置过滤器

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encodingFilter

/*

sessionFilter

*.do

SpringMVC Action

action

org.springframework.web.servlet.DispatcherServlet

1

<!-这里是配置拦截匹配,必须不配置 默认是拦截 html->

action

*.do


index.html

index.htm

index.jsp

上面是web.xml对于springz支持的配置,注意就是配置spring的监听,上下文路径,拦截匹配。

下面是如何实现方法名加类名实现一步到位的配置,很简单,就是很少有人知道去用。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

           http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/spring-context-2.5.xsd"

default-autowire="byName">

Spring配置文件

value="oracle.jdbc.driver.OracleDriver">

value="jdbc:oracle:thin:@127.0.0.1 :1521:demo">

org.hibernate.dialect.Oracle9Dialect

true

true

com/gyx/web/model/AdUser.hbm.xml

com/gyx/web/model/AdForm.hbm.xml

com/gyx/web/model/AdPemit.hbm.xml

com/gyx/web/model/AdRole.hbm.xml

com/gyx/web/model/AdUserRole.hbm.xml

com/gyx/web/model/AdFormRole.hbm.xml

com/gyx/web/model/AdSession.hbm.xml

abstract="false" lazy-init="false" autowire="default"

dependency-check="default">


lazy-init="false" autowire="default" dependency-check="default">



lazy-init="true" />




abstract="true">

PROPAGATION_REQUIRED,readOnly

PROPAGATION_REQUIRED,-Exception

PROPAGATION_REQUIRED,-Exception

PROPAGATION_REQUIRED

PROPAGATION_REQUIRED

PROPAGATION_REQUIRED

PROPAGATION_REQUIRED

PROPAGATION_REQUIRED

PROPAGATION_REQUIRED,readOnly

PROPAGATION_REQUIRED,readOnly

上面是核心配置,这样就可以一步到位的去请求后台Java代码啦。

代码部分

实现一个简单的form  tree加载,方法不全,仅仅演示使用

@Controller("formAction")

public class FormActionController extends AbstractActionController {

public void tree(HttpServletRequest req, HttpServletResponse resp) throws Exception {

StringBuffer buff = new StringBuffer("[");

ArrayList params = new ArrayList();

StringBuffer sql = new StringBuffer( "SELECT F.NAME,F.AD_FORM_ID FROM AD_FORM F  WHERE 1=1 AND PARENTID IS NULL AND F.UPDATED >= TO_DATE('2013-04-08','YYYY-MM-DD')  ORDER BY F.AD_FORM_ID ");

String sqls = sql.toString();

SqlRowSet rs = getJdbcTemplate().queryForRowSet(sqls, params.toArray());

while (rs.next()) {

buff.append("{id:'" + rs.getInt("AD_Form_ID")).append("',text:'" + rs.getString("Name")).append("',");

int id = rs.getInt("AD_Form_ID");

buff.append(childNode(id));

}

if (rs.isAfterLast()) {

buff.deleteCharAt(buff.length() - 1);

}

buff.append("]");

sendJSON(resp, buff.toString());

}

}

上面的controller只有一个注解 @Controller("formAction")

用于与前台界面进行URL路径匹配。

js


Ext.namespace("App.Form");

App.Form.Manager = function(config) {

config = config || {};

this.moduleId = config.moduleId || 'Form_Manager';

this.action = App.getContextPath() + "formAction";

var pageSize = App.getPageSize();

// 树菜单

var root = new Ext.tree.AsyncTreeNode({

id : '0',

text : "菜单列表",

draggable : false,

leaf : false,

expanded : true

});


var treeloader = new Ext.tree.TreeLoader({

dataUrl : this.action + "/tree.do"

});


var treepanel = new Ext.tree.TreePanel({

rootVisible : true,

hideCollapseTool : true,

collapsible : false,

id : this.moduleId + "_formTree",

width : 200,

minSize : 180,

maxSize : 250,

split : true,

autoHeight : false,

autoScroll : true, // 自动滚动

// containerScroll : true,// 是否支持滚动条

// border : true, // 边框

animate : true, // 动画效果

loader : treeloader,

lines : true,

trackMouseOver : true,

root : root,

listeners : {

scope : this,

"click" : function(node, event) {

node.expand();

var grid = Ext.getCmp(this.moduleId + "_treeGrid");

if (node.isLeaf()) {

return;

}

if (!node.isLeaf() && node.parentNode.id == 0) {

grid.getStore().reload({

params : {

parentID : node.id

}

})

}

}


}

});

}

在js中约束好对应名称即可      this.action = App.getContextPath() + "formAction";

请求方法如下:

dataUrl : this.action + "/tree.do"

这样就实现了完全的spring mvc  无需任何额外的配置啦。

转载于:https://my.oschina.net/pvpCC9IFwqz4/blog/383977

你可能感兴趣的:(java,测试,数据库)