<servlet>
<servlet-name> appfuse</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name> appfuse</servlet-name>
<url-pattern>*.do</url-pattern> //处理以什么方式结尾的动作
</servlet-mapping>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/> </bean>
<bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver" id="paramResolver">
<property name="paramName" value="do"/>
</bean>
//下面当url为person.do的时候请求进入PersonController控制器。
<bean class="com.nbw.test.web.action.PersonController" name="/person.do">
<property name="methodNameResolver">
<ref bean="paramResolver"/> //这是当控制器需要实现多个方法的时候,指定使用哪个方法名,引用上面
</property>
<property name="sessionForm"> <value>false</value>
</property>
<property name="commandClass"> <value>com.nbw.test.domain.Person</value>
</property>
</bean>
/**
* Handles two cases: form submissions and showing a new form.
* Delegates the decision between the two to #isFormSubmission},
* always treating requests without existing form session attribute
* as new form when using session form mode.
* @see #isFormSubmission
* @see #showNewForm
* @see #processFormSubmission
*/
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
throws Exception {
if (isFormSubmission(request)) {
// Fetch form object from HTTP session, bind, validate, process submission.
try {
Object command = getCommand(request);
ServletRequestDataBinder binder = bindAndValidate(request, command);
BindException errors = new BindException(binder.getBindingResult());
return processFormSubmission(request, response, command, errors);
}
catch (HttpSessionRequiredException ex) {
// Cannot submit a session form if no form object is in the session.
if (logger.isDebugEnabled()) {
logger.debug("Invalid submit detected: " + ex.getMessage());
}
return handleInvalidSubmit(request, response);
}
}
else {
// New form to show: render form view.
return showNewForm(request, response);
}
}
/**
* Return the form object for the given request.
* <p>Calls #formBackingObject} if not in session form mode.
* Else, retrieves the form object from the session. Note that the form object
* gets removed from the session, but it will be re-added when showing the
* form for resubmission.
* @param request current HTTP request
* @return object form to bind onto
* @throws org.springframework.web.HttpSessionRequiredException
* if a session was expected but no active session (or session form object) found
* @throws Exception in case of invalid state or arguments
* @see #formBackingObject
*/
protected final Object getCommand(HttpServletRequest request) throws Exception {
// If not in session-form mode, create a new form-backing object.
if (!isSessionForm()) {
return formBackingObject(request);
}
// Session-form mode: retrieve form object from HTTP session attribute.
HttpSession session = request.getSession(false);
if (session == null) {
throw new HttpSessionRequiredException("Must have session when trying to bind (in session-form mode)");
}
String formAttrName = getFormSessionAttributeName(request);
Object sessionFormObject = session.getAttribute(formAttrName);
if (sessionFormObject == null) {
throw new HttpSessionRequiredException("Form object not found in session (in session-form mode)");
}
// Remove form object from HTTP session: we might finish the form workflow
// in this request. If it turns out that we need to show the form view again,
// we'll re-bind the form object to the HTTP session.
if (logger.isDebugEnabled()) {
logger.debug("Removing form session attribute [" + formAttrName + "]");
}
session.removeAttribute(formAttrName);
return currentFormObject(request, sessionFormObject);
}
protected final Object createCommand() throws Exception {
if (this.commandClass == null) {
throw new IllegalStateException( "Cannot create command without commandClass being set - " +
"either set commandClass or (in a form controller) override formBackingObject ");
}
if (logger.isDebugEnabled()) {
logger.debug( "Creating new command of class [ " + this.commandClass.getName() + "] ");
}
return BeanUtils.instantiateClass(this.commandClass);//就这里!!!
}
protected ModelAndView processFormSubmission(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception
{
if (errors.hasErrors()) {
if (logger.isDebugEnabled()) {
logger.debug("Data binding errors: " + errors.getErrorCount());
}
return showForm(request, response, errors);
} else {
String methodName = methodNameResolver.getHandlerMethodName(request);
Method method = null;
Method[] methods = this.getClass().getMethods();
for(int i = 0; i <methods.length ; i++){
if(methods[i].getName().equals(methodName)){
method = methods[i];
}
}
//java
//Class dd= (Class<T>)((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
//method = getClass().getMethod(methodName,new Class[]{HttpServletRequest.class, HttpServletResponse.class, (Class<T>)((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0], BindException.class});
// method = getClass().getMethod(methodName,new Class[]{HttpServletRequest.class, HttpServletResponse.class, getCommandClass(), BindException.class});
List params = new ArrayList(4);
params.add(request);
params.add(response);
//Java 5.0
params.add(getCommandClass().cast(command));
//Java 1.4
//params.add(command);
params.add(errors);
try{
return (ModelAndView) method.invoke(this, params.toArray(new Object[params.size()]));
}
catch (InvocationTargetException e){
//找到实际的异常,并抛出
throw (Exception)e.getTargetException();
}
catch(Exception e){
throw e;
}
}
}
/**
* 转向编辑页面
*
* @param request
* @param response
* @param command
* @param errors
* @return
*
* @throws ServletException, IOException
*/
public ModelAndView toEditPage(HttpServletRequest request,
HttpServletResponse response, Person command, BindException errors)
throws ServletException, IOException {
String id = request.getParameter("objectId");
command = this.personmanager.findById(id);
return new ModelAndView("test/personEdit","person",command);
}
http://www.lfpsoft.com