springMVC配置(XML配置详解)
原⽂出⾃:
servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>加载/WEB-INF/spring-mvc/⽬录下的所有XML作为Spring MVC的配置⽂件</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
这样,所有的.htm的请求,都会被DispatcherServlet处理;
初始化 DispatcherServlet 时,该框架在 web 应⽤程序WEB-INF ⽬录中寻⼀个名为[servlet-名称]-l的⽂件,并在那⾥定义相关的Beans,重写在全局中定义的任何Beans,像上⾯的l中的代码,对应的是l;当然也可以使⽤<init-param>元素,⼿动指定配置⽂件的路径;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:mvc="/schema/mvc"
xmlns:p="/schema/p"
xmlns:context="/schema/context"
xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans-3.0.xsd
/schema/context
/schema/context/spring-context-3.0.xsd
/schema/aop
/schema/aop/spring-aop-3.0.xsd
/schema/tx
/schema/tx/spring-tx-3.0.xsd
/schema/mvc
/schema/mvc/spring-mvc-3.0.xsd
/schema/context
/schema/context/spring-context-3.0.xsd">
<!--
使Spring⽀持⾃动检测组件,如注解的Controller
-
->
<context:component-scan base-package="ller"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
第⼀个Controller:
package ller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/index")
public String index() {
return "index";
}
}
@Controller注解标识⼀个控制器,@RequestMapping注解标记⼀个访问的路径(/index.htm),return "index"标记返回视图
(index.jsp);
注:如果@RequestMapping注解在类级别上,则表⽰⼀相对路径,在⽅法级别上,则标记访问的路径;
从@RequestMapping注解标记的访问路径中获取参数:
Spring MVC ⽀持RESTful风格的URL参数,如:
@Controller
public class IndexController {
@RequestMapping("/index/{username}")
public String index(@PathVariable("username") String username) {
System.out.print(username);
return "index";
}
}
在@RequestMapping中定义访问页⾯的URL模版,使⽤{}传⼊页⾯参数,使⽤@PathVariable 获取传⼊参数,即可通过地
址:localhost:8080/crm/index/tanqimin.htm 访问;
根据不同的Web请求⽅法,映射到不同的处理⽅法:
使⽤登陆页⾯作⽰例,定义两个⽅法分辨对使⽤GET请求和使⽤POST请求访问login.htm时的响应。可以使⽤处理GET请求的⽅法显⽰视图,使⽤POST请求的⽅法处理业务逻辑;
@Controller
public class LoginController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login2(HttpServletRequest request) {
String username = Parameter("username").trim();
el表达式获取map的值System.out.println(username);
return "login2";
}
}
在视图页⾯,通过地址栏访问login.htm,是通过GET请求访问页⾯,因此,返回登陆表单视图login.jsp;当在登陆表单中使⽤POST请求提交数据时,则访问login2⽅法,处理登陆业务逻辑;
防⽌重复提交数据,可以使⽤重定向视图:
return "redirect:/login2"
可以传⼊⽅法的参数类型:
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
String username = Parameter("username");
System.out.println(username);
return null;
}
可以传⼊HttpServletRequest、HttpServletResponse、HttpSession,值得注意的是,如果第⼀次访问页⾯,HttpSession没被创建,可能会出错;
其中,String username = Parameter("username");可以转换为传⼊的参数:
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session,@RequestParam("username") String username) {
String username = Parameter("username");
System.out.println(username);
return null;
}
使⽤@RequestParam 注解获取GET请求或POST请求提交的参数;
获取Cookie的值:使⽤@CookieValue :
获取PrintWriter:
可以直接在Controller的⽅法中传⼊PrintWriter对象,就可以在⽅法中使⽤:
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(PrintWriter out, @RequestParam("username") String username) {
out.println(username);
return null;
}
获取表单中提交的值,并封装到POJO中,传⼊Controller的⽅法⾥:
POJO如下(User.java):
public class User{
private long id;
private String username;
private String password;
…此处省略
}
通过表单提交,直接可以把表单值封装到User对象中:
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(PrintWriter out, User user) {
out.Username());
return null;
}
可以把对象,put ⼊获取的Map对象中,传到对应的视图:
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(User user, Map model) {
model.put("user",user);
return "view";
}
在返回的view.jsp中,就可以根据key来获取user的值(通过EL表达式,${user }即可);
Controller中⽅法的返回值:
void:多数⽤于使⽤PrintWriter输出响应数据;
String 类型:返回该String对应的View Name;
任意类型对象:
返回ModelAndView:
⾃定义视图(JstlView,ExcelView):
(Inteceptors):
public class MyInteceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o)
throws Exception {
return false;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView mav)
throws Exception {
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception excptn)
throws Exception {
}
}
需要实现HandleInterceptor接⼝,并实现其三个⽅法:
preHandle:的前端,执⾏控制器之前所要处理的⽅法,通常⽤于权限控制、⽇志,其中,Object o表⽰下⼀个;postHandle:控制器的⽅法已经执⾏完毕,转换成视图之前的处理;
afterCompletion:视图已处理完后执⾏的⽅法,通常⽤于释放资源;
在MVC的配置⽂件中,配置与需要拦截的URL:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/index.htm" />
<bean class="web.interceptor.MyInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
国际化:
在MVC配置⽂件中,配置国际化属性⽂件:
<bean id="messageSource" class="t.support.ResourceBundleMessageSource" p:basename="message"> </bean>
那么,Spring就会在项⽬中搜索相关的国际化属性⽂件,如:message.properties、message_zh_CN.properties
在VIEW中,引⼊Spring标签:<%@taglib uri="/tags" prefix="spring" %>,使⽤<spring:message code="key" />调⽤,即可;
如果⼀种语⾔,有多个语⾔⽂件,可以更改MVC配置⽂件为:
<bean id="messageSource"
class="t.support.ResourceBundleMessageSource" p:basename="message">
</bean>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论