当前位置: 代码迷 >> Java Web开发 >> spring的mvc模式,该如何解决
  详细解决方案

spring的mvc模式,该如何解决

热度:69   发布时间:2016-04-17 01:11:25.0
spring的mvc模式
请教高手spring的mvc模式是什么样子的?如果有例子就更好了!谢谢

------解决方案--------------------
给你贴个spring mvc Form的封装
------解决方案--------------------
多了去了 下载官方文档看看就行了 很容易的
------解决方案--------------------

------解决方案--------------------
http://swengineer.iteye.com/blog/1103748 看看就知道了
------解决方案--------------------
Java code
package com.louis.ssh.web.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.louis.ssh.hibernate.GeneralDao;public interface Controller {    public HttpServletRequest getRequest();    public HttpServletResponse getResponse();    public String getPathVariable(String pathVarName);    public void dataPrepare();    public GeneralDao getGeneralDao();}package com.louis.ssh.web.controller;import java.lang.reflect.ParameterizedType;import java.util.Map;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.ui.ModelMap;import org.springframework.web.context.request.NativeWebRequest;import org.springframework.web.servlet.HandlerMapping;import com.louis.ssh.hibernate.GeneralDao;@SuppressWarnings("unchecked")public abstract class AbstractController<T> implements Controller {    private NativeWebRequest nativeWebRequest;    private ModelMap model;    private GeneralDao generalDao;    private String redirectPath;    private Class<T> clazz;    public static final String SUFFIX = ".html";    public AbstractController() {    }    @SuppressWarnings("unchecked")    public String getPathVariable(String pathVarName) {        HttpServletRequest request = getRequest();        Map<String, String> uriTemplateVariables = (Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);        if (uriTemplateVariables == null) {            return null;        }        return uriTemplateVariables.get(pathVarName);    }    public String redirect() {        return "redirect:" + redirectPath + SUFFIX;    }    public void dataPrepare() {    }    public final HttpServletRequest getRequest() {        return nativeWebRequest.getNativeRequest(HttpServletRequest.class);    }    public final HttpServletResponse getResponse() {        return nativeWebRequest.getNativeResponse(HttpServletResponse.class);    }    public String getRedirectPath() {        return redirectPath;    }    public void setRedirectPath(String redirectPath) {        this.redirectPath = redirectPath;    }    public ModelMap getModel() {        return model;    }    public void setModel(ModelMap model) {        this.model = model;    }    public void setModel(String key, Object value) {        if (this.model == null) {            model = new ModelMap();        }        this.model.put(key, value);    }    public GeneralDao getGeneralDao() {        return generalDao;    }    @Resource(name = "generalDao")    public void setGeneralDao(GeneralDao generalDao) {        this.generalDao = generalDao;    }    public Class<T> getClazz() {        if (clazz == null) {            clazz = (Class<T>) ((ParameterizedType) this.getClass()                    .getGenericSuperclass()).getActualTypeArguments()[0];        }        return clazz;    }    protected void addTipText(Object message) {        getRequest().getSession().setAttribute("tipText", message);    }    public NativeWebRequest getNativeWebRequest() {        return nativeWebRequest;    }    public void setNativeWebRequest(NativeWebRequest nativeWebRequest) {        this.nativeWebRequest = nativeWebRequest;    }}package com.louis.ssh.web.controller;import java.io.Serializable;import javax.annotation.Resource;import org.springframework.ui.ModelMap;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.context.request.NativeWebRequest;import com.louis.ssh.domain.DomainObject;import com.louis.ssh.util.JodaUtil;import com.louis.ssh.util.LowerAndUpperCase;import com.louis.ssh.web.converter.Converter;public abstract class FormController<T extends DomainObject> extends        AbstractController<T> {    private String formView;    private Converter converter;    private static final String MODELATTRIBUTE = "domainObject";    public FormController() {        String name = LowerAndUpperCase.toLowerCase(getClazz().getSimpleName());        this.formView = name + "/" + name + "form";        setRedirectPath("/" + name + "/" + "list");    }    @InitBinder    public void initBinder(WebDataBinder binder) {        registerPropertyEditor(binder);    }    public void registerPropertyEditor(WebDataBinder binder) {    }    @ModelAttribute(MODELATTRIBUTE)    public T getDomainObject(NativeWebRequest nativeWebRequest) {        setNativeWebRequest(nativeWebRequest);        Serializable id = getConverter().converter(getPathVariable("id"));        return getDomainObject(id);    }    @RequestMapping(value = { "/form" + SUFFIX, "/form/{id}" + SUFFIX }, method = RequestMethod.GET)    public String setupForm(ModelMap model) {        setModel(model);        dataPrepare();        return formView;    }    @RequestMapping(value = { "/form" + SUFFIX, "/form/{id}" + SUFFIX }, method = RequestMethod.POST)    public String save(@ModelAttribute(MODELATTRIBUTE)T domainObject) {        saveBefore(domainObject);        if (domainObject.getId() != null) {            domainObject.setUpdateTime(JodaUtil.now());        }        getGeneralDao().saveOrUpdate(domainObject);        saveAfter(domainObject);        return redirect();    }    @RequestMapping(value = "/delete/{id}" + SUFFIX)    public String delete() {        Serializable id = getConverter().converter(getPathVariable("id"));        T item = getGeneralDao().get(id, getClazz());        deleteBefore(item);        getGeneralDao().delete(item);        deleteAfter();        return redirect();    }    public void saveBefore(T domainObject) {    }    public void saveAfter(T domainObject) {    }    public void deleteAfter() {    }    public void deleteBefore(T domainObject) {    }    public String getFormView() {        return formView;    }    public void setFormView(String formView) {        this.formView = formView;    }    public T getDomainObject(Serializable id) {        T t = null;        if (id != null) {            t = getGeneralDao().get(id, getClazz());        }        if (t == null) {            try {                t = getClazz().newInstance();            } catch (InstantiationException e) {                e.printStackTrace();            } catch (IllegalAccessException e) {                e.printStackTrace();            }        }        return t;    }    public Converter getConverter() {        return converter;    }    @Resource(name = "converter")    public void setConverter(Converter converter) {        this.converter = converter;    }}
  相关解决方案