其步骤如下:
**********常用的开发配置 stringMVC********************
在springMVC-servlet.xml中配置
<!-- 配置controller映射 最方便的方法,也是最常用的开发方法 -->
<context:component-scan base-package="com.tgb.web.controller"></context:component-scan>
<mvc:annotation-driven/>
然后再Controller中
@Controller //注入
public class welloController {
@RequestMapping({"/hello","/"})
public ModelAndView hello(){
Map<String, Object> map=new HashMap<String, Object>();
map.put("1", "1111111");
map.put("2", "2222222222");
map.put("3", "3333333333");
return new ModelAndView("/hello","map",map);
}
@RequestMapping({"/NewFile","/"})
public String NewFile(){
return "NewFile";
}
//RequestMapping表示用哪个url来对应
//一个函数映射一个视图,使用起来很方便
}
*******************************************************************************
1.在后端中的handleRequest函数中,设置数据。如string 或者Map数据类型
eg. Map<String,Objet> map=new HashMap<String,Objet>();
map.put("1","11");
map.put("2","22");
2.把数据返回
public class HelloWordController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
String hello="hello";
Map<String, Object> map=new HashMap<String, Object>();
map.put("1", "1111111");
map.put("2", "2222222222");
map.put("3", "3333333333");
return new ModelAndView("/hello","map",map);
//ModelAndView("/hello","map",map); 第一个参数是返回的视图,第二个是前台访问数据使用的变量名,第三个返回数据的变量对象
}
3.使用jstl条件
前台中得在jsp页面顶部加上 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
在web-inf/lib中得加入 jstl-1.2.jar standard-1.1.2.jar
4.前台对数据进行访问
对于map之类的数据,可以用forEach进行遍历 ${名称 } 可以直接输出数据
<c:forEach items="${map }" var="m">
${m.key }---->${m.value }
</c:forEach>