spring_mvc(二)Mapping Request

http://localhost:8080/spring_mvc_test/simple
@RequestMapping("simple")
public @ResponseBody String helloWorld() {
String message = "Hello, this is a simple example";
System.out.println(message);
return message;
}


[color=blue]mapping by path[/color]

http://localhost:8080/spring_mvc_test/mapping/path
@RequestMapping("/mapping/path")
public @ResponseBody String mappingByPath() {
String message = "Mapping by path";
return message;
}


[color=blue]mapping by method[/color]
<form action="http://localhost:8080/spring_mvc_test/mapping/method" method="post">
<input type="submit" value="提交"/>
</form>

@RequestMapping(value="/mapping/method", method=RequestMethod.POST)
public @ResponseBody String mappingByMethod() {
String message = "Mapping by Method";
return message;
}


[color=blue]By path,method,and presence of parameter[/color]

http://localhost:8080/spring_mvc_test//mapping/parameter?foo=111
@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="foo")
public @ResponseBody String byParameter() {
String message = "By path,method,and presence of parameter";
return message;
}


[color=blue]Mapped by path + method + not presence of query parameter!
[/color]

http://localhost:8080/spring_mvc_test//mapping/parameter?foo1=111

@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="!foo") 
public @ResponseBody String mappedNotParams(){
String message = "Mapped by path + method + not presence of query parameter!";
return message;
}


[color=blue]Mapped by path + method + presence of header[/color]
@RequestMapping(value="/mapping/header", method=RequestMethod.GET, headers="Accept=text/plain")
public @ResponseBody String byHeader() {
String message = "Mapped by path + method + presence of header!";
return message;
}


[color=blue]Mapped by path + method + not presence header![/color]

http://localhost:8080/spring_mvc_test/notheader
@RequestMapping(value="/notheader", method=RequestMethod.GET, headers="!FooHeader")
public @ResponseBody String byHeaderNegation() {
String message = "Mapped by path + method + not presence header!";
return message;
}


[color=blue]Mapping by regexp![/color]

http://localhost:8080/spring_mvc_test/regexp/ddd
http://localhost:8080/spring_mvc_test/regexp/test
@RequestMapping(value="/regexp/*", method=RequestMethod.GET)
public @ResponseBody String regexp() {
String message = "Mapping by regexp!";
return message;
}
一个非常简单的MVC框架,实现了类似Spring MVC的基本功能。 1、包括自动扫描绑定映射路径,只要在web.xml中指定扫描包,系统启动后会将请求url绑定到指定的处理方法上。如: 在web.xml中定义如下: <context-param> <param-name>ScanPackage</param-name> <param-value>com.mvc.controller</param-value> </context-param> 容器在启动时候,会将com.mvc.controller下所有映射路径绑定处理方法上,假如在扫描包中定义下列类: import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.hxw.simple.light.mvc.annotation.MappingMethodAnnotation; import com.hxw.simple.light.mvc.view.SimpleModelView; import com.mvc.verification.VerificationCode; public class LoginController { @MappingMethodAnnotation(mappingMethod = "/login.do") public String login(HttpServletRequest request, HttpServletResponse response) { return "login/main"; } @MappingMethodAnnotation(mappingMethod = "/loginvm.do") public SimpleModelView loginView(HttpServletRequest request, HttpServletResponse response) { SimpleModelView mv = new SimpleModelView("login/mainmv"); Map<String, String> m = new HashMap<String, String>(); m.put("beij", "北京"); m.put("sha", "上海"); m.put("nanj", "南京"); mv.setAttribute("city", m); return mv; } @MappingMethodAnnotation(mappingMethod = "/Verify.do") public void service(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException { HttpServletRequest request = (HttpServletRequest) arg0; HttpServletResponse response = (HttpServletResponse) arg1; response.setContentType("image/jpeg"); response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); VerificationCode vCode = new VerificationCode(); BufferedImage bufferImage = vCode.getImageData(); HttpSession session = request.getSession(); response.addCookie(new Cookie("JSSESIONID", session.getId())); ServletOutputStream responseOutputStream = response.getOutputStream(); ImageIO.write(bufferImage, "JPEG", responseOutputStream); responseOutputStream.flush(); responseOutputStream.close(); } } 那么在接到url请求如:http://localhost:8080/TestSimpleMVC/loginvm.do 会调用指定的方法处理。 2、支持视图定义,在web.xml定义了视图路径后: <servlet> <servlet-name>DelegateForwardServlet</servlet-name> <servlet-class>com.hxw.simple.light.mvc.servlet.DelegateForwardServlet</servlet-class> <init-param> <param-name>prefix</param-name> <param-value>/WEB-INF/view/</param-value> </init-param> <init-param> <param-name>suffix</param-name> <param-value>.jsp</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DelegateForwardServlet</servlet-name> <url-pattern>/SYS_FORWARD_URL.fo</url-pattern> </servlet-mapping> 只需在执行完方法后,返回字符串:如return "login/main"就会跳转到指定视图,还可以在视图中用EL表单式访问modelview数据,例子如: SimpleModelView mv = new SimpleModelView("login/mainmv"); Map<String, String> m = new HashMap<String, String>(); m.put("beij", "北京"); m.put("sha", "上海"); m.put("nanj", "南京"); mv.setAttribute("city", m); return mv; 3、支持数据参数自动绑定如: @ParamAttribute(type = ParamaAttributeType.REQUEST, key = "userPassword") String s, @ParamAttribute(type = ParamaAttributeType.SESSION, key = "11212") String s1, @ParamAttribute(type = ParamaAttributeType.SERVLETCONTEXT, key = "32312") String s2, @ParamAttribute(type = ParamaAttributeType.REQUEST, key = "userNames") String[] s3 系统根据参数指定的范围,这指定范围内赋值到参数上,你还可以直接使用javabean做参数绑定,如: public class User extends PaginatedHelper { private static final long serialVersionUID = -8225389551152428829L; private String userName; private String userPassword; public User() { super(); } public User(String userName, String userPassword) { super(); this.userName = userName; this.userPassword = userPassword; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } 在方法上带上 Uer user后,属性名称相同的数据就会赋值到javabean上。不必再使用繁琐的user.setUserName(request.getParameter("userName")); 3、简便的jdbc操作 有查询模板QueryTemplate,命名查询NamedQueryTemplate及bean作为参数的BeanQueryTemplate等。支持返回javabean类型,javabean列表, MAP类型,map列表类型,统计结果queryForInt等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值