利用spring mvc,hibernate重构系统(1)
最近在云南做一个新项目,之前在湖南做项目时一直想使用JDK 5的注解特性来简化繁琐的XML配置;这次做项目终于用上spring mvc 及 jpa啦。 1.web.xml中加入如下代码片段
<!-- SPRING annomvc -->
<servlet>
<servlet-name>annomvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatchServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/context/spring-annomvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>annomvc</servlet-name>
<url-pattern>*.mvc</url-pattern>
</servlet-mapping>2.spring-annomvc.xml中的内容:
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
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">
<!--扫描ANNOTATION注入的ACTION-->
<context:component-scanbase-package="com.linkage"/>
<!--异常捕获-->
<beanid="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<propertyname="exceptionMappings">
<props>
<propkey="java.lang.Exception">common/error</prop>
</props>
</property>
</bean><!-- URL 映射处理 -->
<beanclass="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<propertyname="interceptors">
</property>
<propertyname="caseSensitive"value="true"/>
</bean><!-- 视图解析-->
<beanid="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="viewClass"
value="org.springframework.web.servlet.view.JstlView"/>
<propertyname="prefix"value="/"/><!--/WEB-INF/jsp/-->
<propertyname="suffix"value=".jsp"/>
</bean></beans>
3.所有ActionController 文件的基类
public class BaseActionController extends MultiActionController { protected final Log log = LogFactory.getLog(getClass());
private static String RESULT_PAGE = "/common/resultPage"; // .jsp private static String JSON_PAGE = "/common/ajaxJson"; // .jsp
protected static String MOD = "MOD"; //修改
protectedstaticStringDEL="DEL";//删除
protectedstaticStringADD="ADD";//新增
protectedstaticStringJSONRESULT="jsonResult";//JSON返回值
private String jspPkg;public String getJspPkg() {
returnjspPkg;
}public void setJspPkg(String jspPkg) {
this.jspPkg=jspPkg;
}public BaseActionController() {
super();
if(this.getClass().isAnnotationPresent(JspPkg.class)){
JspPkgjspPkgAnnotation=this.getClass().getAnnotation(JspPkg.class);
jspPkg=jspPkgAnnotation.value();
}
}public BaseActionController(Object delegate) {
super(delegate);
}/**
*转向默认JSP页面
*@paramrequest
*@return
*@throwsNoSuchRequestHandlingMethodException
*/
protectedModelAndViewgetDefaultView(HttpServletRequestrequest)throwsNoSuchRequestHandlingMethodException{
StringviewName=this.getMethodNameResolver().getHandlerMethodName(request);
Stringdir=this.getJspPkg();
ModelAndViewview=CmsDispatchServlet.getModelAndView();
view.setViewName(dir+viewName);
log.debug("JSPName:"+view.getViewName()+".jsp");
returnview;
}
/**
*如果没有指定viewName,则转向默认JSP
*@paramrequest
*@return
*@throwsNoSuchRequestHandlingMethodException
*/
protectedModelAndViewgetReqView(HttpServletRequestrequest)throwsNoSuchRequestHandlingMethodException{
StringviewName=request.getParameter("viewName");
StringhandlerMethodName=this.getMethodNameResolver().getHandlerMethodName(request);
viewName=(viewName==null)?handlerMethodName:viewName;
Stringdir=this.getJspPkg();
ModelAndViewview=CmsDispatchServlet.getModelAndView();
view.setViewName(dir+viewName);
log.debug("JSPName:"+view.getViewName()+".jsp");
returnview;
}/**
*程序员可直接指定JSP的绝对路径
*@paramrequest
*@return
*@throwsNoSuchRequestHandlingMethodException
*/
protectedModelAndViewgetCustomView(StringviewName){
ModelAndViewview=CmsDispatchServlet.getModelAndView();
view.setViewName(viewName);
log.debug("JSPName:"+view.getViewName()+".jsp");
returnview;
}/**
*AJAX调用时转向的JSP
*@return
*/
protectedModelAndViewgetJSONPage(){
returngetCustomView(JSON_PAGE);
}/**
*指向IFRAME回调页面
*@return
*/
protectedModelAndViewgetResultPage(){
returngetCustomView(RESULT_PAGE);
}
}另外注解标注 ActionController 对应的JSP目录
import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public@interfaceJspPkg{
public String value();}
4.BaseActionController 的子类 //访问该类的方法 http://localhost:7001/user2/view.mvc
package com.linkage.demo.web.action;
import java.util.HashMap;import java.util.List;
import javax.annotation.Resource;
importjavax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;import org.springframework.web.servlet.ModelAndView;
import com.linkage.util.cache.resource.RptResourceCache;
importcom.linkage.util.cache.resource.model.ServiceConfig;
importcom.linkage.util.cache.resource.service.ServiceConfigManager;
importcom.linkage.util.jdbc.QryCenter;
importcom.linkage.util.jdbc.ServiceUtil;
importcom.linkage.util.web.WebUtil;
importcom.linkage.util.web.action.BaseActionController;
import com.linkage.util.web.annotation.JspPkg;@Controller
@JspPkg(value="/demo/")
public class User2Controller extends BaseActionController {public ModelAndView view(HttpServletRequest request,
HttpServletResponseresponse)throwsException{
JSONObjectjo=newJSONObject();
request.setAttribute("jsonResult",jo.toString());
request.setAttribute("jsonResult", inputValue);return this.getJSONPage(); }
}