利用spring mvc,hibernate重构系统(1) MVCSpringHibernateServletJSP
最近在云南做一个新项目,之前在湖南做项目时一直想使用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"?>
<beans xmlns="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-scan base-package="com.linkage" />
<!-- 异常捕获 -->
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">common/error</prop>
</props>
</property>
</bean>
<!-- URL 映射处理 -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="interceptors">
</property>
<property name="caseSensitive" value="true" />
</bean>
<!-- 视图解析-->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" /> <!-- /WEB-INF/jsp/ -->
<property name="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"; //修改
protected static String DEL = "DEL"; //删除
protected static String ADD = "ADD"; //新增
protected static String JSONRESULT = "jsonResult" ;// JSON返回值
private String jspPkg;
public String getJspPkg() {
return jspPkg;
}
public void setJspPkg(String jspPkg) {
this.jspPkg = jspPkg;
}
public BaseActionController() {
super();
if (this.getClass().isAnnotationPresent(JspPkg.class)) {
JspPkg jspPkgAnnotation = this.getClass().getAnnotation(JspPkg.class);
jspPkg = jspPkgAnnotation.value();
}
}
public BaseActionController(Object delegate) {
super(delegate);
}
/**
* 转向默认JSP页面
* @param request
* @return
* @throws NoSuchRequestHandlingMethodException
*/
protected ModelAndView getDefaultView(HttpServletRequest request) throws NoSuchRequestHandlingMethodException {
String viewName = this.getMethodNameResolver().getHandlerMethodName(request);
String dir = this.getJspPkg();
ModelAndView view = CmsDispatchServlet.getModelAndView();
view.setViewName(dir + viewName);
log.debug("JSP Name :" + view.getViewName() +".jsp");
return view;
}
/**
* 如果没有指定viewName,则转向默认JSP
* @param request
* @return
* @throws NoSuchRequestHandlingMethodException
*/
protected ModelAndView getReqView(HttpServletRequest request) throws NoSuchRequestHandlingMethodException {
String viewName = request.getParameter("viewName");
String handlerMethodName = this.getMethodNameResolver().getHandlerMethodName(request);
viewName = (viewName==null) ? handlerMethodName:viewName ;
String dir = this.getJspPkg();
ModelAndView view = CmsDispatchServlet.getModelAndView();
view.setViewName(dir + viewName);
log.debug("JSP Name :" + view.getViewName() +".jsp");
return view;
}
/**
* 程序员可直接指定JSP的绝对路径
* @param request
* @return
* @throws NoSuchRequestHandlingMethodException
*/
protected ModelAndView getCustomView(String viewName) {
ModelAndView view = CmsDispatchServlet.getModelAndView();
view.setViewName(viewName);
log.debug("JSP Name :" + view.getViewName() +".jsp");
return view;
}
/**
* AJAX调用时转向的JSP
* @return
*/
protected ModelAndView getJSONPage() {
return getCustomView(JSON_PAGE);
}
/**
* 指向IFRAME回调页面
* @return
*/
protected ModelAndView getResultPage() {
return getCustomView(RESULT_PAGE);
}
}
另外注解标注 ActionController 对应的JSP目录
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface JspPkg {
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;
import javax.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;
import com.linkage.util.cache.resource.model.ServiceConfig;
import com.linkage.util.cache.resource.service.ServiceConfigManager;
import com.linkage.util.jdbc.QryCenter;
import com.linkage.util.jdbc.ServiceUtil;
import com.linkage.util.web.WebUtil;
import com.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,
HttpServletResponse response) throws Exception {
JSONObject jo = new JSONObject();
request.setAttribute("jsonResult", jo.toString());
request.setAttribute("jsonResult", inputValue);
return this.getJSONPage();
}
}