`
hz_chenwenbiao
  • 浏览: 1019654 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Spring 注解学习手札(二) 控制层梳理

阅读更多

昨天对Spring注解有了一个整体认识,至少完成了一个简单的web应用搭建。当然,还不完善,这仅仅只是个开始!
今天看了Spring 3.0的注解,我感觉自己被颠覆了。多年前,为了减少代码依赖我们用配置文件进行模块间耦合,降低模块之间的黏度。现如今,所有可配置的内容都塞进了代码中,我只能说:这多少有点顾此失彼,有点倒退的意思!使用注解的好处是:代码通读性增强。这既是优势也是劣势!如果我要改一段配置,就要打开代码逐行扫描;如果恰巧这是别人封装的jar包,那我只好反编译;如果碰巧遇上这个jar包经过了混淆,那我只好求助于AOP了。 为了这么一个配置,我的代码观几乎将要被颠覆!

相关参考:
Spring 注解学习手札(一) 构建简单Web应用
Spring 注解学习手札(二) 控制层梳理
Spring 注解学习手札(三) 表单页面处理
Spring 注解学习手札(四) 持久层浅析
Spring 注解学习手札(五) 业务层事务处理
Spring 注解学习手札(六) 测试


言归正传,研究一下注解下的控制层。
我习惯于使用JSTL展示页面,因此需要在原lib基础上增加jstl.jar和standard.jar,详细lib依赖如下:

引用

aopalliance-1.0.jar
commons-logging-1.1.1.jar
log4j-1.2.15.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-context-support-2.5.6.jar
spring-core-2.5.6.jar
spring-tx-2.5.6.jar
spring-web-2.5.6.jar
spring-webmvc-2.5.6.jar
standard.jar
jstl.jar


上一篇文中,我们定义了控制器AccountController:
AccountController.java

Java代码 复制代码
  1. /**  
  2.  * 2010-1-23  
  3.  */  
  4. package org.zlex.spring.controller;   
  5.   
  6. import javax.servlet.http.HttpServletRequest;   
  7. import javax.servlet.http.HttpServletResponse;   
  8.   
  9. import org.springframework.beans.factory.annotation.Autowired;   
  10. import org.springframework.stereotype.Controller;   
  11. import org.springframework.web.bind.ServletRequestUtils;   
  12. import org.springframework.web.bind.annotation.RequestMapping;   
  13. import org.springframework.web.bind.annotation.RequestMethod;   
  14. import org.zlex.spring.service.AccountService;   
  15.   
  16. /**  
  17.  *   
  18.  * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>  
  19.  * @version 1.0  
  20.  * @since 1.0  
  21.  */  
  22. @Controller  
  23. @RequestMapping("/account.do")   
  24. public class AccountController {   
  25.   
  26.     @Autowired  
  27.     private AccountService accountService;   
  28.   
  29.     @RequestMapping(method = RequestMethod.GET)   
  30.     public void hello(HttpServletRequest request, HttpServletResponse response)   
  31.             throws Exception {   
  32.   
  33.         String username = ServletRequestUtils.getRequiredStringParameter(   
  34.                 request, "username");   
  35.         String password = ServletRequestUtils.getRequiredStringParameter(   
  36.                 request, "password");   
  37.         System.out.println(accountService.verify(username, password));   
  38.     }   
  39. }  
/**
 * 2010-1-23
 */
package org.zlex.spring.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.zlex.spring.service.AccountService;

/**
 * 
 * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
 * @version 1.0
 * @since 1.0
 */
@Controller
@RequestMapping("/account.do")
public class AccountController {

	@Autowired
	private AccountService accountService;

	@RequestMapping(method = RequestMethod.GET)
	public void hello(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String username = ServletRequestUtils.getRequiredStringParameter(
				request, "username");
		String password = ServletRequestUtils.getRequiredStringParameter(
				request, "password");
		System.out.println(accountService.verify(username, password));
	}
}


先说注解@RequestMapping
这里使用注解@RequestMapping(method = RequestMethod.GET)指定这个方法为get请求时调用。同样,我们可以使用注解@RequestMapping(method = RequestMethod.POST)指定该方法接受post请求。

Java代码 复制代码
  1. @Controller  
  2. @RequestMapping("/account.do")   
  3. public class AccountController {   
  4.   
  5.     @RequestMapping(method = RequestMethod.GET)   
  6.     public void get() {   
  7.     }   
  8.   
  9.     @RequestMapping(method = RequestMethod.POST)   
  10.     public void post() {   
  11.     }   
  12. }  
@Controller
@RequestMapping("/account.do")
public class AccountController {

	@RequestMapping(method = RequestMethod.GET)
	public void get() {
	}

	@RequestMapping(method = RequestMethod.POST)
	public void post() {
	}
}


这与我们久别的Servlet很相像,类似于doGet()和doPost()方法!
我们也可以将其改造为多动作控制器,如下代码所示:

Java代码 复制代码
  1. @Controller  
  2. @RequestMapping("/account.do")   
  3. public class AccountController {   
  4.   
  5.     @RequestMapping(params = "method=login")     
  6.     public void login() {   
  7.     }   
  8.   
  9.     @RequestMapping(params = "method=logout")     
  10.     public void logout() {   
  11.     }  
@Controller
@RequestMapping("/account.do")
public class AccountController {

	@RequestMapping(params = "method=login")  
	public void login() {
	}

	@RequestMapping(params = "method=logout")  
	public void logout() {
	}


这样,我们可以通过参数“method”指定不同的参数值从而通过请求("/account.do?method=login"和"/account.do?method=logout")调用不同的方法!
注意:使用多动作控制器必须在配置文件中加入注解支持!

Xml代码 复制代码
  1. <bean  
  2.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />


当然,我们也可以将注解@RequestMapping指定到某一个方法上,如:

Java代码 复制代码
  1. @Controller  
  2. public class AccountController {   
  3.        
  4.     @RequestMapping("/a.do")   
  5.     public void a() {}   
  6.   
  7.     @RequestMapping("/b.do")   
  8.     public void b() {}   
  9. }  
@Controller
public class AccountController {
	
	@RequestMapping("/a.do")
	public void a() {}

	@RequestMapping("/b.do")
	public void b() {}
}


这样,请求“a.do”和“b.do”将对应不同的方法a() 和b()。这使得一个控制器可以同时承载多个请求!
@RequestMapping("/account.do")@RequestMapping(value="/account.do")的简写!
再说输入参数!
这里的方法名可以随意定义,但是参数和返回值却又要求!
为什么?直接看源代码,我们就能找到答案!
AnnotationMethodHandlerAdapter.java部分源代码——有关参数部分:

Java代码 复制代码
  1. @Override  
  2. protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest)   
  3.         throws Exception {   
  4.   
  5.     HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();   
  6.     HttpServletResponse response = (HttpServletResponse) webRequest.getNativeResponse();   
  7.   
  8.     if (ServletRequest.class.isAssignableFrom(parameterType)) {   
  9.         return request;   
  10.     }   
  11.     else if (ServletResponse.class.isAssignableFrom(parameterType)) {   
  12.         this.responseArgumentUsed = true;   
  13.         return response;   
  14.     }   
  15.     else if (HttpSession.class.isAssignableFrom(parameterType)) {   
  16.         return request.getSession();   
  17.     }   
  18.     else if (Principal.class.isAssignableFrom(parameterType)) {   
  19.         return request.getUserPrincipal();   
  20.     }   
  21.     else if (Locale.class.equals(parameterType)) {   
  22.         return RequestContextUtils.getLocale(request);   
  23.     }   
  24.     else if (InputStream.class.isAssignableFrom(parameterType)) {   
  25.         return request.getInputStream();   
  26.     }   
  27.     else if (Reader.class.isAssignableFrom(parameterType)) {   
  28.         return request.getReader();   
  29.     }   
  30.     else if (OutputStream.class.isAssignableFrom(parameterType)) {   
  31.         this.responseArgumentUsed = true;   
  32.         return response.getOutputStream();   
  33.     }   
  34.     else if (Writer.class.isAssignableFrom(parameterType)) {   
  35.         this.responseArgumentUsed = true;   
  36.         return response.getWriter();   
  37.     }   
  38.     return super.resolveStandardArgument(parameterType, webRequest);   
  39. }  
		@Override
		protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest)
				throws Exception {

			HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
			HttpServletResponse response = (HttpServletResponse) webRequest.getNativeResponse();

			if (ServletRequest.class.isAssignableFrom(parameterType)) {
				return request;
			}
			else if (ServletResponse.class.isAssignableFrom(parameterType)) {
				this.responseArgumentUsed = true;
				return response;
			}
			else if (HttpSession.class.isAssignableFrom(parameterType)) {
				return request.getSession();
			}
			else if (Principal.class.isAssignableFrom(parameterType)) {
				return request.getUserPrincipal();
			}
			else if (Locale.class.equals(parameterType)) {
				return RequestContextUtils.getLocale(request);
			}
			else if (InputStream.class.isAssignableFrom(parameterType)) {
				return request.getInputStream();
			}
			else if (Reader.class.isAssignableFrom(parameterType)) {
				return request.getReader();
			}
			else if (OutputStream.class.isAssignableFrom(parameterType)) {
				this.responseArgumentUsed = true;
				return response.getOutputStream();
			}
			else if (Writer.class.isAssignableFrom(parameterType)) {
				this.responseArgumentUsed = true;
				return response.getWriter();
			}
			return super.resolveStandardArgument(parameterType, webRequest);
		}


也就是说,如果我们想要在自定义的方法中获得一些个“标准”输入参数,参数类型必须包含在以下类型中:

引用

ServletRequest
ServletResponse
HttpSession
Principal
Locale
InputStream
OutputStream
Reader
Writer


当然,上述接口其实都是对于HttpServletRequest和HttpServletResponse的扩展。
此外,我们还可以定义自己的参数。
注意:自定义参数必须是实现类,绝非接口!Spring容器将帮你完成对象初始化工作!
比如说上文中,我们需要参数username和password。我们可以这么写:

Java代码 复制代码
  1. @RequestMapping(method = RequestMethod.GET)   
  2. public void hello(String username,String password) {   
  3.     System.out.println(accountService.verify(username, password));   
  4. }  
	@RequestMapping(method = RequestMethod.GET)
	public void hello(String username,String password) {
		System.out.println(accountService.verify(username, password));
	}


如果参数名不能与这里的变量名保持一致,那么我们可以使用注解@RequestParam进行强制绑定,代码如下所示:

Java代码 复制代码
  1. @RequestMapping(method = RequestMethod.GET)   
  2. public void hello(@RequestParam("username") String u,   
  3.         @RequestParam("password") String p) {   
  4.     System.out.println(accountService.verify(u, p));   
  5. }  
	@RequestMapping(method = RequestMethod.GET)
	public void hello(@RequestParam("username") String u,
			@RequestParam("password") String p) {
		System.out.println(accountService.verify(u, p));
	}


这比起我们之前写的代码有所简洁:

Java代码 复制代码
  1. @RequestMapping(method = RequestMethod.GET)   
  2. public void hello(HttpServletRequest request, HttpServletResponse response)   
  3.         throws Exception {   
  4.   
  5.     String username = ServletRequestUtils.getRequiredStringParameter(   
  6.             request, "username");   
  7.     String password = ServletRequestUtils.getRequiredStringParameter(   
  8.             request, "password");   
  9.     System.out.println(accountService.verify(username, password));   
  10. }  
	@RequestMapping(method = RequestMethod.GET)
	public void hello(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String username = ServletRequestUtils.getRequiredStringParameter(
				request, "username");
		String password = ServletRequestUtils.getRequiredStringParameter(
				request, "password");
		System.out.println(accountService.verify(username, password));
	}


ServletRequestUtils类的工作已经由Spring底层实现了,我们只需要把参数名定义一致即可,其内部取参无需关心!
除了传入参数,我们还可以定义即将传出的参数,如加入ModelMap参数:

Java代码 复制代码
  1. @SuppressWarnings("unchecked")   
  2. @RequestMapping(method = RequestMethod.GET)   
  3. public Map hello(String username, String password, ModelMap model) {   
  4.   
  5.     System.out.println(accountService.verify(username, password));   
  6.        
  7.     model.put("msg", username);   
  8.   
  9.     return model;   
  10. }  
	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET)
	public Map hello(String username, String password, ModelMap model) {

		System.out.println(accountService.verify(username, password));
		
		model.put("msg", username);

		return model;
	}


这时,我们没有定义页面名称,Spring容器将根据请求名指定同名view,即如果是jap页面,则account.do->account.jsp!
不得不承认,这样写起来的确减少了代码量!
接着说输出参数!
通过ModelMap,我们可以绑定输出到的页面的参数,但最终我们将要返回到何种页面呢?再次查看AnnotationMethodHandlerAdapter源代码!
AnnotationMethodHandlerAdapter.java部分源代码——有关返回值部分:

Java代码 复制代码
  1. @SuppressWarnings("unchecked")   
  2. public ModelAndView getModelAndView(Method handlerMethod, Class handlerType, Object returnValue,   
  3.         ExtendedModelMap implicitModel, ServletWebRequest webRequest) {   
  4.   
  5.     if (returnValue instanceof ModelAndView) {   
  6.         ModelAndView mav = (ModelAndView) returnValue;   
  7.         mav.getModelMap().mergeAttributes(implicitModel);   
  8.         return mav;   
  9.     }   
  10.     else if (returnValue instanceof Model) {   
  11.         return new ModelAndView().addAllObjects(implicitModel).addAllObjects(((Model) returnValue).asMap());   
  12.     }   
  13.     else if (returnValue instanceof Map) {   
  14.         return new ModelAndView().addAllObjects(implicitModel).addAllObjects((Map) returnValue);   
  15.     }   
  16.     else if (returnValue instanceof View) {   
  17.         return new ModelAndView((View) returnValue).addAllObjects(implicitModel);   
  18.     }   
  19.     else if (returnValue instanceof String) {   
  20.         return new ModelAndView((String) returnValue).addAllObjects(implicitModel);   
  21.     }   
  22.     else if (returnValue == null) {   
  23.         // Either returned null or was 'void' return.   
  24.         if (this.responseArgumentUsed || webRequest.isNotModified()) {   
  25.             return null;   
  26.         }   
  27.         else {   
  28.             // Assuming view name translation...   
  29.             return new ModelAndView().addAllObjects(implicitModel);   
  30.         }   
  31.     }   
  32.     else if (!BeanUtils.isSimpleProperty(returnValue.getClass())) {   
  33.         // Assume a single model attribute...   
  34.         ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);   
  35.         String attrName = (attr != null ? attr.value() : "");   
  36.         ModelAndView mav = new ModelAndView().addAllObjects(implicitModel);   
  37.         if ("".equals(attrName)) {   
  38.             Class resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);   
  39.             attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);   
  40.         }   
  41.         return mav.addObject(attrName, returnValue);   
  42.     }   
  43.     else {   
  44.         throw new IllegalArgumentException("Invalid handler method return value: " + returnValue);   
  45.     }   
  46. }   
		@SuppressWarnings("unchecked")
		public ModelAndView getModelAndView(Method handlerMethod, Class handlerType, Object returnValue,
				ExtendedModelMap implicitModel, ServletWebRequest webRequest) {

			if (returnValue instanceof ModelAndView) {
				ModelAndView mav = (ModelAndView) returnValue;
				mav.getModelMap().mergeAttributes(implicitModel);
				return mav;
			}
			else if (returnValue instanceof Model) {
				return new ModelAndView().addAllObjects(implicitModel).addAllObjects(((Model) returnValue).asMap());
			}
			else if (returnValue instanceof Map) {
				return new ModelAndView().addAllObjects(implicitModel).addAllObjects((Map) returnValue);
			}
			else if (returnValue instanceof View) {
				return new ModelAndView((View) returnValue).addAllObjects(implicitModel);
			}
			else if (returnValue instanceof String) {
				return new ModelAndView((String) returnValue).addAllObjects(implicitModel);
			}
			else if (returnValue == null) {
				// Either returned null or was 'void' return.
				if (this.responseArgumentUsed || webRequest.isNotModified()) {
					return null;
				}
				else {
					// Assuming view name translation...
					return new ModelAndView().addAllObjects(implicitModel);
				}
			}
			else if (!BeanUtils.isSimpleProperty(returnValue.getClass())) {
				// Assume a single model attribute...
				ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
				String attrName = (attr != null ? attr.value() : "");
				ModelAndView mav = new ModelAndView().addAllObjects(implicitModel);
				if ("".equals(attrName)) {
					Class resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
					attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
				}
				return mav.addObject(attrName, returnValue);
			}
			else {
				throw new IllegalArgumentException("Invalid handler method return value: " + returnValue);
			}
		}
	}


返回值的定义十分庞大,或者说可怕的if-else多少有点让我觉得厌恶!
我们可以定义以下类型的返回值:

引用

ModelAndView
Model
View
Map
String
null


ModelAndView、Model和View都是Spring之前版本所特有的元素,Map对应于传入参数ModelMap,String定义页面名称,null即对应void类型方法!
最常用的实现方式如下:

Java代码 复制代码
  1. @SuppressWarnings("unchecked")   
  2. @RequestMapping(method = RequestMethod.GET)   
  3. public String hello(String username, String password, ModelMap model) {   
  4.   
  5.     System.out.println(accountService.verify(username, password));   
  6.   
  7.     model.put("msg", username);   
  8.   
  9.     return "account";   
  10. }  
	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET)
	public String hello(String username, String password, ModelMap model) {

		System.out.println(accountService.verify(username, password));

		model.put("msg", username);

		return "account";
	}


当然,对于我来说在返回值中写入这么一个字符串多少有点不能接受,于是我还是乐于使用输入参数ModelMap+输出参数Map的方式。
给出一个完整的AccountController实现:
AccountController.java

Java代码 复制代码
  1. /**  
  2.  * 2010-1-23  
  3.  */  
  4. package org.zlex.spring.controller;   
  5.   
  6. import java.util.Map;   
  7.   
  8. import org.springframework.beans.factory.annotation.Autowired;   
  9. import org.springframework.stereotype.Controller;   
  10. import org.springframework.ui.ModelMap;   
  11. import org.springframework.web.bind.annotation.RequestMapping;   
  12. import org.springframework.web.bind.annotation.RequestMethod;   
  13. import org.zlex.spring.service.AccountService;   
  14.   
  15. /**  
  16.  *   
  17.  * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>  
  18.  * @version 1.0  
  19.  * @since 1.0  
  20.  */  
  21. @Controller  
  22. @RequestMapping("/account.do")   
  23. public class AccountController {   
  24.   
  25.     @Autowired  
  26.     private AccountService accountService;   
  27.   
  28.     @SuppressWarnings("unchecked")   
  29.     @RequestMapping(method = RequestMethod.GET)   
  30.     public Map hello(String username, String password, ModelMap model) {   
  31.   
  32.         System.out.println(accountService.verify(username, password));   
  33.   
  34.         model.put("msg", username);   
  35.         return model;   
  36.     }   
  37. }  
/**
 * 2010-1-23
 */
package org.zlex.spring.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.zlex.spring.service.AccountService;

/**
 * 
 * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
 * @version 1.0
 * @since 1.0
 */
@Controller
@RequestMapping("/account.do")
public class AccountController {

	@Autowired
	private AccountService accountService;

	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET)
	public Map hello(String username, String password, ModelMap model) {

		System.out.println(accountService.verify(username, password));

		model.put("msg", username);
		return model;
	}
}


最后说注解@Session
如果想将某个ModelMap中的参数指定到Session中,可以使用@Session注解,将其绑定为Session熟悉,代码如下所示:

Java代码 复制代码
  1. @Controller  
  2. @RequestMapping("/account.do")   
  3. @SessionAttributes("msg")   
  4. public class AccountController {   
  5.   
  6.     @Autowired  
  7.     private AccountService accountService;   
  8.   
  9.     @SuppressWarnings("unchecked")   
  10.     @RequestMapping(method = RequestMethod.GET)   
  11.     public Map hello(String username, String password, ModelMap model) {   
  12.   
  13.         System.out.println(accountService.verify(username, password));   
  14.   
  15.         model.put("msg", username);   
  16.         return model;   
  17.     }   
  18.   
  19. }  
@Controller
@RequestMapping("/account.do")
@SessionAttributes("msg")
public class AccountController {

	@Autowired
	private AccountService accountService;

	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET)
	public Map hello(String username, String password, ModelMap model) {

		System.out.println(accountService.verify(username, password));

		model.put("msg", username);
		return model;
	}

}


当然,我们还需要配置一下对应的视图解析器,给出完整配置:
servelt.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xmlns:context="http://www.springframework.org/schema/context"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
  9.     <context:component-scan  
  10.         base-package="org.zlex.spring.controller" />  
  11.     <bean  
  12.         id="urlMapping"  
  13.         class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
  14.     <bean  
  15.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
  16.     <bean  
  17.         id="jstlViewResolver"  
  18.         class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  19.         p:viewClass="org.springframework.web.servlet.view.JstlView"  
  20.         p:prefix="/WEB-INF/page/"  
  21.         p:suffix=".jsp" />  
  22. </beans>  
<?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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<context:component-scan
		base-package="org.zlex.spring.controller" />
	<bean
		id="urlMapping"
		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
	<bean
		id="jstlViewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:viewClass="org.springframework.web.servlet.view.JstlView"
		p:prefix="/WEB-INF/page/"
		p:suffix=".jsp" />
</beans>


这里使用了JstlView作为视图解析器。同时,指定前缀路径为"/WEB-INF/page/",后缀路径为".jsp"。也就是说,Spring容器将会在这个路径中寻找匹配的jsp文件!
注意加入xmlns:p="http://www.springframework.org/schema/p"命名空间!
再给出页面内容:
taglib.jsp

相关推荐

    基于模糊故障树的工业控制系统可靠性分析与Python实现

    内容概要:本文探讨了模糊故障树(FFTA)在工业控制系统可靠性分析中的应用,解决了传统故障树方法无法处理不确定数据的问题。文中介绍了模糊数的基本概念和实现方式,如三角模糊数和梯形模糊数,并展示了如何用Python实现模糊与门、或门运算以及系统故障率的计算。此外,还详细讲解了最小割集的查找方法、单元重要度的计算,并通过实例说明了这些方法的实际应用场景。最后,讨论了模糊运算在处理语言变量方面的优势,强调了在可靠性分析中处理模糊性和优化计算效率的重要性。 适合人群:从事工业控制系统设计、维护的技术人员,以及对模糊数学和可靠性分析感兴趣的科研人员。 使用场景及目标:适用于需要评估复杂系统可靠性的场合,特别是在面对不确定数据时,能够提供更准确的风险评估。目标是帮助工程师更好地理解和预测系统故障,从而制定有效的预防措施。 其他说明:文中提供的代码片段和方法可用于初步方案验证和技术探索,但在实际工程项目中还需进一步优化和完善。

    风力发电领域双馈风力发电机(DFIG)Simulink模型的构建与电流电压波形分析

    内容概要:本文详细探讨了双馈风力发电机(DFIG)在Simulink环境下的建模方法及其在不同风速条件下的电流与电压波形特征。首先介绍了DFIG的基本原理,即定子直接接入电网,转子通过双向变流器连接电网的特点。接着阐述了Simulink模型的具体搭建步骤,包括风力机模型、传动系统模型、DFIG本体模型和变流器模型的建立。文中强调了变流器控制算法的重要性,特别是在应对风速变化时,通过实时调整转子侧的电压和电流,确保电流和电压波形的良好特性。此外,文章还讨论了模型中的关键技术和挑战,如转子电流环控制策略、低电压穿越性能、直流母线电压脉动等问题,并提供了具体的解决方案和技术细节。最终,通过对故障工况的仿真测试,验证了所建模型的有效性和优越性。 适用人群:从事风力发电研究的技术人员、高校相关专业师生、对电力电子控制系统感兴趣的工程技术人员。 使用场景及目标:适用于希望深入了解DFIG工作原理、掌握Simulink建模技能的研究人员;旨在帮助读者理解DFIG在不同风速条件下的动态响应机制,为优化风力发电系统的控制策略提供理论依据和技术支持。 其他说明:文章不仅提供了详细的理论解释,还附有大量Matlab/Simulink代码片段,便于读者进行实践操作。同时,针对一些常见问题给出了实用的调试技巧,有助于提高仿真的准确性和可靠性。

    基于西门子S7-200 PLC和组态王的八层电梯控制系统设计与实现

    内容概要:本文详细介绍了基于西门子S7-200 PLC和组态王软件构建的八层电梯控制系统。首先阐述了系统的硬件配置,包括PLC的IO分配策略,如输入输出信号的具体分配及其重要性。接着深入探讨了梯形图编程逻辑,涵盖外呼信号处理、轿厢运动控制以及楼层判断等关键环节。随后讲解了组态王的画面设计,包括动画效果的实现方法,如楼层按钮绑定、轿厢移动动画和门开合效果等。最后分享了一些调试经验和注意事项,如模拟困人场景、防抖逻辑、接线艺术等。 适合人群:从事自动化控制领域的工程师和技术人员,尤其是对PLC编程和组态软件有一定基础的人群。 使用场景及目标:适用于需要设计和实施小型电梯控制系统的工程项目。主要目标是帮助读者掌握PLC编程技巧、组态画面设计方法以及系统联调经验,从而提高项目的成功率。 其他说明:文中提供了详细的代码片段和调试技巧,有助于读者更好地理解和应用相关知识点。此外,还强调了安全性和可靠性方面的考量,如急停按钮的正确接入和硬件互锁设计等。

    CarSim与Simulink联合仿真:基于MPC模型预测控制实现智能超车换道

    内容概要:本文介绍了如何将CarSim的动力学模型与Simulink的智能算法相结合,利用模型预测控制(MPC)实现车辆的智能超车换道。主要内容包括MPC控制器的设计、路径规划算法、联合仿真的配置要点以及实际应用效果。文中提供了详细的代码片段和技术细节,如权重矩阵设置、路径跟踪目标函数、安全超车条件判断等。此外,还强调了仿真过程中需要注意的关键参数配置,如仿真步长、插值设置等,以确保系统的稳定性和准确性。 适合人群:从事自动驾驶研究的技术人员、汽车工程领域的研究人员、对联合仿真感兴趣的开发者。 使用场景及目标:适用于需要进行自动驾驶车辆行为模拟的研究机构和企业,旨在提高超车换道的安全性和效率,为自动驾驶技术研发提供理论支持和技术验证。 其他说明:随包提供的案例文件已调好所有参数,可以直接导入并运行,帮助用户快速上手。文中提到的具体参数和配置方法对于初学者非常友好,能够显著降低入门门槛。

    基于单片机的鱼缸监测设计(51+1602+AD0809+18B20+UART+JKx2)#0107

    包括:源程序工程文件、Proteus仿真工程文件、论文材料、配套技术手册等 1、采用51单片机作为主控; 2、采用AD0809(仿真0808)检测"PH、氨、亚硝酸盐、硝酸盐"模拟传感; 3、采用DS18B20检测温度; 4、采用1602液晶显示检测值; 5、检测值同时串口上传,调试助手监看; 6、亦可通过串口指令对加热器、制氧机进行控制;

    风电领域双馈永磁风电机组并网仿真及短路故障分析与MPPT控制

    内容概要:本文详细介绍了双馈永磁风电机组并网仿真模型及其短路故障分析方法。首先构建了一个9MW风电场模型,由6台1.5MW双馈风机构成,通过升压变压器连接到120kV电网。文中探讨了风速模块的设计,包括渐变风、阵风和随疾风的组合形式,并提供了相应的Python和MATLAB代码示例。接着讨论了双闭环控制策略,即功率外环和电流内环的具体实现细节,以及MPPT控制用于最大化风能捕获的方法。此外,还涉及了短路故障模块的建模,包括三相电压电流特性和离散模型与phasor模型的应用。最后,强调了永磁同步机并网模型的特点和注意事项。 适合人群:从事风电领域研究的技术人员、高校相关专业师生、对风电并网仿真感兴趣的工程技术人员。 使用场景及目标:适用于风电场并网仿真研究,帮助研究人员理解和优化风电机组在不同风速条件下的性能表现,特别是在短路故障情况下的应对措施。目标是提高风电系统的稳定性和可靠性。 其他说明:文中提供的代码片段和具体参数设置有助于读者快速上手并进行实验验证。同时提醒了一些常见的错误和需要注意的地方,如离散化步长的选择、初始位置对齐等。

    空手道训练测试系统BLE106版本

    适用于空手道训练和测试场景

    【音乐创作领域AI提示词】AI音乐提示词(deepseek,豆包,kimi,chatGPT,扣子空间,manus,AI训练师)

    内容概要:本文介绍了金牌音乐作词大师的角色设定、背景经历、偏好特点、创作目标、技能优势以及工作流程。金牌音乐作词大师凭借深厚的音乐文化底蕴和丰富的创作经验,能够为不同风格的音乐创作歌词,擅长将传统文化元素与现代流行文化相结合,创作出既富有情感又触动人心的歌词。在创作过程中,会严格遵守社会主义核心价值观,尊重用户需求,提供专业修改建议,确保歌词内容健康向上。; 适合人群:有歌词创作需求的音乐爱好者、歌手或音乐制作人。; 使用场景及目标:①为特定主题或情感创作歌词,如爱情、励志等;②融合传统与现代文化元素创作独特风格的歌词;③对已有歌词进行润色和优化。; 阅读建议:阅读时可以重点关注作词大师的创作偏好、技能优势以及工作流程,有助于更好地理解如何创作出高质量的歌词。同时,在提出创作需求时,尽量详细描述自己的情感背景和期望,以便获得更贴合心意的作品。

    linux之用户管理教程.md

    linux之用户管理教程.md

    基于单片机的搬运机器人设计(51+1602+L298+BZ+KEY6)#0096

    包括:源程序工程文件、Proteus仿真工程文件、配套技术手册等 1、采用51/52单片机作为主控芯片; 2、采用1602液晶显示设置及状态; 3、采用L298驱动两个电机,模拟机械臂动力、移动底盘动力; 3、首先按键配置-待搬运物块的高度和宽度(为0不能开始搬运); 4、按下启动键开始搬运,搬运流程如下: 机械臂先把物块抓取到机器车上, 机械臂减速 机器车带着物块前往目的地 机器车减速 机械臂把物块放下来 机械臂减速 机器车回到物块堆积处(此时机器车是空车) 机器车减速 蜂鸣器提醒 按下复位键,结束本次搬运

    基于下垂控制的三相逆变器电压电流双闭环仿真及MATLAB/Simulink/PLECS实现

    内容概要:本文详细介绍了基于下垂控制的三相逆变器电压电流双闭环控制的仿真方法及其在MATLAB/Simulink和PLECS中的具体实现。首先解释了下垂控制的基本原理,即有功调频和无功调压,并给出了相应的数学表达式。随后讨论了电压环和电流环的设计与参数整定,强调了两者带宽的差异以及PI控制器的参数选择。文中还提到了一些常见的调试技巧,如锁相环的响应速度、LC滤波器的谐振点处理、死区时间设置等。此外,作者分享了一些实用的经验,如避免过度滤波、合理设置采样周期和下垂系数等。最后,通过突加负载测试展示了系统的动态响应性能。 适合人群:从事电力电子、微电网研究的技术人员,尤其是有一定MATLAB/Simulink和PLECS使用经验的研发人员。 使用场景及目标:适用于希望深入了解三相逆变器下垂控制机制的研究人员和技术人员,旨在帮助他们掌握电压电流双闭环控制的具体实现方法,提高仿真的准确性和效率。 其他说明:本文不仅提供了详细的理论讲解,还结合了大量的实战经验和调试技巧,有助于读者更好地理解和应用相关技术。

    光伏并网逆变器全栈开发资料:硬件设计、控制算法及实战经验

    内容概要:本文详细介绍了光伏并网逆变器的全栈开发资料,涵盖了从硬件设计到控制算法的各个方面。首先,文章深入探讨了功率接口板的设计,包括IGBT缓冲电路、PCB布局以及EMI滤波器的具体参数和设计思路。接着,重点讲解了主控DSP板的核心控制算法,如MPPT算法的实现及其注意事项。此外,还详细描述了驱动扩展板的门极驱动电路设计,特别是光耦隔离和驱动电阻的选择。同时,文章提供了并联仿真的具体实现方法,展示了环流抑制策略的效果。最后,分享了许多宝贵的实战经验和调试技巧,如主变压器绕制、PWM输出滤波、电流探头使用等。 适合人群:从事电力电子、光伏系统设计的研发工程师和技术爱好者。 使用场景及目标:①帮助工程师理解和掌握光伏并网逆变器的硬件设计和控制算法;②提供详细的实战经验和调试技巧,提升产品的可靠性和性能;③适用于希望深入了解光伏并网逆变器全栈开发的技术人员。 其他说明:文中不仅提供了具体的电路设计和代码实现,还分享了许多宝贵的实际操作经验和常见问题的解决方案,有助于提高开发效率和产品质量。

    机器人轨迹规划中粒子群优化与3-5-3多项式结合的时间最优路径规划

    内容概要:本文详细介绍了粒子群优化(PSO)算法与3-5-3多项式相结合的方法,在机器人轨迹规划中的应用。首先解释了粒子群算法的基本原理及其在优化轨迹参数方面的作用,随后阐述了3-5-3多项式的数学模型,特别是如何利用不同阶次的多项式确保轨迹的平滑过渡并满足边界条件。文中还提供了具体的Python代码实现,展示了如何通过粒子群算法优化时间分配,使3-5-3多项式生成的轨迹达到时间最优。此外,作者分享了一些实践经验,如加入惩罚项以避免超速,以及使用随机扰动帮助粒子跳出局部最优。 适合人群:对机器人运动规划感兴趣的科研人员、工程师和技术爱好者,尤其是有一定编程基础并对优化算法有初步了解的人士。 使用场景及目标:适用于需要精确控制机器人运动的应用场合,如工业自动化生产线、无人机导航等。主要目标是在保证轨迹平滑的前提下,尽可能缩短运动时间,提高工作效率。 其他说明:文中不仅给出了理论讲解,还有详细的代码示例和调试技巧,便于读者理解和实践。同时强调了实际应用中需要注意的问题,如系统的建模精度和安全性考量。

    【KUKA 机器人资料】:kuka机器人压铸欧洲标准.pdf

    KUKA机器人相关资料

    光子晶体中BIC与OAM激发的模拟及三维Q值计算

    内容概要:本文详细探讨了光子晶体中的束缚态在连续谱中(BIC)及其与轨道角动量(OAM)激发的关系。首先介绍了光子晶体的基本概念和BIC的独特性质,随后展示了如何通过Python代码模拟二维光子晶体中的BIC,并解释了BIC在光学器件中的潜在应用。接着讨论了OAM激发与BIC之间的联系,特别是BIC如何增强OAM激发效率。文中还提供了使用有限差分时域(FDTD)方法计算OAM的具体步骤,并介绍了计算本征态和三维Q值的方法。此外,作者分享了一些实验中的有趣发现,如特定条件下BIC表现出OAM特征,以及不同参数设置对Q值的影响。 适合人群:对光子晶体、BIC和OAM感兴趣的科研人员和技术爱好者,尤其是从事微纳光子学研究的专业人士。 使用场景及目标:适用于希望通过代码模拟深入了解光子晶体中BIC和OAM激发机制的研究人员。目标是掌握BIC和OAM的基础理论,学会使用Python和其他工具进行模拟,并理解这些现象在实际应用中的潜力。 其他说明:文章不仅提供了详细的代码示例,还分享了许多实验心得和技巧,帮助读者避免常见错误,提高模拟精度。同时,强调了物理离散化方式对数值计算结果的重要影响。

    C#联合Halcon 17.12构建工业视觉项目的配置与应用

    内容概要:本文详细介绍了如何使用C#和Halcon 17.12构建一个功能全面的工业视觉项目。主要内容涵盖项目配置、Halcon脚本的选择与修改、相机调试、模板匹配、生产履历管理、历史图像保存以及与三菱FX5U PLC的以太网通讯。文中不仅提供了具体的代码示例,还讨论了实际项目中常见的挑战及其解决方案,如环境配置、相机控制、模板匹配参数调整、PLC通讯细节、生产数据管理和图像存储策略等。 适合人群:从事工业视觉领域的开发者和技术人员,尤其是那些希望深入了解C#与Halcon结合使用的专业人士。 使用场景及目标:适用于需要开发复杂视觉检测系统的工业应用场景,旨在提高检测精度、自动化程度和数据管理效率。具体目标包括但不限于:实现高效的视觉处理流程、确保相机与PLC的无缝协作、优化模板匹配算法、有效管理生产和检测数据。 其他说明:文中强调了框架整合的重要性,并提供了一些实用的技术提示,如避免不同版本之间的兼容性问题、处理实时图像流的最佳实践、确保线程安全的操作等。此外,还提到了一些常见错误及其规避方法,帮助开发者少走弯路。

    基于Matlab的9节点配电网中分布式电源接入对节点电压影响的研究

    内容概要:本文探讨了分布式电源(DG)接入对9节点配电网节点电压的影响。首先介绍了9节点配电网模型的搭建方法,包括定义节点和线路参数。然后,通过在特定节点接入分布式电源,利用Matlab进行潮流计算,模拟DG对接入点及其周围节点电压的影响。最后,通过绘制电压波形图,直观展示了不同DG容量和接入位置对配电网电压分布的具体影响。此外,还讨论了电压越限问题以及不同线路参数对电压波动的影响。 适合人群:电力系统研究人员、电气工程学生、从事智能电网和分布式能源研究的专业人士。 使用场景及目标:适用于研究分布式电源接入对配电网电压稳定性的影响,帮助优化分布式电源的规划和配置,确保电网安全稳定运行。 其他说明:文中提供的Matlab代码和图表有助于理解和验证理论分析,同时也为后续深入研究提供了有价值的参考资料。

    电力市场领域中基于CVaR风险评估的省间交易商最优购电模型研究与实现

    内容概要:本文探讨了在两级电力市场环境中,针对省间交易商的最优购电模型的研究。文中提出了一个双层非线性优化模型,用于处理省内电力市场和省间电力交易的出清问题。该模型采用CVaR(条件风险价值)方法来评估和管理由新能源和负荷不确定性带来的风险。通过KKT条件和对偶理论,将复杂的双层非线性问题转化为更易求解的线性单层问题。此外,还通过实际案例验证了模型的有效性,展示了不同风险偏好设置对购电策略的影响。 适合人群:从事电力系统规划、运营以及风险管理的专业人士,尤其是对电力市场机制感兴趣的学者和技术专家。 使用场景及目标:适用于希望深入了解电力市场运作机制及其风险控制手段的研究人员和技术开发者。主要目标是为省间交易商提供一种科学有效的购电策略,以降低风险并提高经济效益。 其他说明:文章不仅介绍了理论模型的构建过程,还包括具体的数学公式推导和Python代码示例,便于读者理解和实践。同时强调了模型在实际应用中存在的挑战,如数据精度等问题,并指出了未来改进的方向。

Global site tag (gtag.js) - Google Analytics