springmvc 攔截器java
繼承了HandlerIntercepter的類能夠做爲攔截器類:web
package com.yawn.intercepter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class UserIntercepter implements HandlerInterceptor { // 最後執行的方法,釋放資源 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) throws Exception { System.out.println("afterCompletion------->>>>>>>>"); } // controller 執行完,生成視圖以前的動做 @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView modelAndView) throws Exception { System.out.println("postHandle------->>>>>>>>"); } // controller 執行以前的動做 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception { System.out.println("preHandle------->>>>>>>>"); return true; } }
攔截器的配置:spring
<!-- 攔截器棧的配置 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/start.do" /> <bean class="com.yawn.intercepter.UserIntercepter"></bean> </mvc:interceptor> <!-- 可定義多個攔截器,造成攔截器棧 --> <!-- <mvc:interceptor> --> <!-- <mvc:mapping path=""/> --> <!-- <bean></bean> --> <!-- </mvc:interceptor> --> </mvc:interceptors>
springmvc 國際化mvc
在classpath下簡歷message.properties、message_ch_CN.properties、message_en_US.properties
app
而後進行以下配置,就能夠在jsp中使用<spring:message code="welcome"/>來訪問了 :
jsp
<!-- 國際化的配置 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="message"></property> <!-- 在classpath下搜索message.properties文件 --> </bean>
springmvc 驗證ide
package com.yawn.controller; import javax.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import com.yawn.po.User; @Controller public class TestController { @RequestMapping(value="/start") private String start(@Valid User user, BindingResult result) { /* * 1.在User前添加@valid註解後,可自動根據User類中的規則對各個字段進行驗證 * * 2.表單的驗證信息都封裝在result中,若是有錯誤,則能夠從中得到錯誤的信息,將它返回給頁面模型 */ if (result.hasErrors()) { return "index"; } System.out.println(">>>>>>>>>>>>>>>>>>-------"); return "start"; } }