spring mvc 2
2016-03-10 00:00
393 查看
Model model,HttpServletRequest request, ModelMap map声明变量
request.getSession().setAttribute("test", "haiwei2Session"); request.setAttribute("test", "haiwei1request"); map.addAttribute("test", "haiweiModelMap"); model.addAttribute("test", "haiweiModel");我通过${test}这个方式取值,优先取Model和ModelMap的,Model和ModelMap是同一个东西,谁最后赋值的就取谁的,然后是request,最后是从session中获取
第一个Controller:
package com.minx.crm.web.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/index")
public String index() {
return "index";
}
}
package com.minx.crm.web.controller; 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(<span style="color: rgb(255, 0, 0);">@PathVariable</span>("username") String username) {
System.out.print(username);
return "index";
}
}
@Controller public class IndexController { @RequestMapping("/index/{username}") public String index(@PathVariable("username") String username) { System.out.print(username); return "index"; } }
在@RequestMapping中定义访问页面的URL模版,使用{}传入页面参数,使用@PathVariable 获取传入参数,即可通过地址:http://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 = request.getParameter("username").trim();
System.out.println(username);
return "login2";
}
}
@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 = request.getParameter("username").trim(); System.out.println(username); return "login2"; } }
在视图页面,通过地址栏访问login.htm,是通过GET请求访问页面,因此,返回登陆表单视图login.jsp;当在登陆表单中使用POST请求提交数据时,则访问login2方法,处理登陆业务逻辑;
防止重复提交数据,可以使用重定向视图:
return "redirect:/login2"
return "redirect:/login2"
可以传入方法的参数类型:
strong>@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
String username = request.getParameter("username");
System.out.println(username);
return null;
}</strong>
@RequestMapping(value = "login", method = RequestMethod.POST) public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session) { String username = request.getParameter("username"); System.out.println(username); return null; }
可以传入HttpServletRequest、HttpServletResponse、HttpSession,值得注意的是,如果第一次访问页面,HttpSession没被创建,可能会出错;
其中,String username = request.getParameter("username");可以转换为传入的参数:
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session,@RequestParam("username") String username) {
String username = request.getParameter("username");
System.out.println(username);
return null;
}
@RequestMapping(value = "login", method = RequestMethod.POST) public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session,@RequestParam("username") String username) { String username = request.getParameter("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, <span style="color: rgb(255, 0, 0);">@RequestParam</span>("username") String username) {
out.println(username);
return null;
}
@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;
…此处省略getter,setter...
}
public class User{ private long id; private String username; private String password; …此处省略getter,setter... }
通过表单提交,直接可以把表单值封装到User对象中:
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(PrintWriter out, User user) {
out.println(user.getUsername());
return null;
}
@RequestMapping(value = "login", method = RequestMethod.POST) public String testParam(PrintWriter out, User user) { out.println(user.getUsername()); return null; }
可以把对象,put 入获取的Map对象中,传到对应的视图:
<strong>@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(User user, Map model) {
model.put("user",user);
return "view";
}</strong>
@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):
<strong>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 {
}
}</strong>
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="com.minx.crm.web.interceptor.MyInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/index.htm" /> <bean class="com.minx.crm.web.interceptor.MyInterceptor" /> </mvc:interceptor> </mvc:interceptors>
国际化:
在MVC配置文件中,配置国际化属性文件:<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="message">
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="message"> </bean>
那么,Spring就会在项目中搜索相关的国际化属性文件,如:message.properties、message_zh_CN.properties
在VIEW中,引入Spring标签:<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>,使用<spring:message code="key" />调用,即可;
如果一种语言,有多个语言文件,可以更改MVC配置文件为:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>message01</value>
<value>message02</value>
<value>message03</value>
</list>
</property>
</bean>
相关文章推荐
- Hashtable HashMap ConcurrentHashMap 源码分析
- 那些年spring声明式事务@Transaction的坑
- Android Studio导入eclipse项目
- spring @service注解
- javassist报错javassist.util.poxy.Poxy
- Spring数据库失效sa'@'localhost'nied for user '
- spring 定时任务
- wrong number of arguments,java方法反射时数组参数的坑
- 商城开发之商品分类表的设计、使用、管理
- Java多态机制的特点
- 5.7 Spring与Hibernate整合应用
- struts2学习 : 数据校验
- Eclipse 如何访问源码中@hide,{@hide}注解的对象
- ECLIPSE处理错误:Access restriction: The type JPEGImageEncoder is not accessible..
- 在eclipse下的SVN插件配置忽略文件和文件夹
- Java线程:并发协作-生产者消费者模型
- 关于Java文件路径问题
- java获得项目绝对路径
- eclipse 环境下 FreeMarker 编辑器插件
- Java回调函数的理解