Easy spring mvc
Spring 注解方式实现 MVC
1、 添加 spring 相应的 jar
2、 修改 web.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5" >
< servlet >
< servlet-name > spring </ servlet-name >
< servlet-class >
org.springframework.web.servlet.DispatcherServlet
</ servlet-class >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name > spring </ servlet-name >
<!-- 这里在配成 spring,
下边也要写一个名为 spring-servlet.xml 的文件,
主要用来配置它的 controller -->
< url-pattern > *.do </ url-pattern >
</ servlet-mapping >
</ web-app >
|
3、 这里只是演示 srping MVC ,所以对 applicationContext.xml 未说明,如果要注入 dao,servic 、事务,还需要在 applicationContext.xml 进行添加
4、 创建 WEB-INF/spring-servlet.xml( 对应 web.xml 中 servlet 的名字:创建 xxx-servlet.xml) ,这里使用注解的方式: spring-serlet.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:p = "http://www.springframework.org/schema/p"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd" >
<!-- 这里要对应 controller(Action) 包名 -->
< context:component-scan base-package = "org.springtest.mvc" />
</ beans >
|
5、 Controller 示例:
@Controller
public class UserController {
/*****
* 请求路径: login.do
* @param uid 前台传递参数 ?uid="", 或表单,会添充至参数 uid 中
* @param request
* @param response
* @throws IOException
*/
@RequestMapping ( "/login.do" )
public void login( @RequestParam ( "uid" )String uid,HttpServletRequest request,HttpServletResponse response) throws IOException {
String u = uid;
System. out .println(u);
PrintWriter out = response.getWriter();
out.println(u);
out.flush();
out.close();
}
/******
* dispatcher 至 user.jsp 页面
* request 范围:保存了 "user" 对象,直接页面访问
* @return
*/
@RequestMapping ( "/getAll.do" )
public ModelAndView getAll() {
ModelAndView mv = new ModelAndView( "/user.jsp" );
mv.addObject( "user" , "ddddddddd" );
return mv;
}
/********
* 使用 @ModelAttribute 可以直接添加 vo 自动填充 vo 参数中
* 只能是基础类型或 string,
* 如若是 data 类型需要 自定义数据绑定
* @param pet
* @param result
* @param response
*/
@RequestMapping ( "/user_save.do" )
public void savePet( @ModelAttribute ( "pet" )Pet pet,BindingResult result ,HttpServletResponse response) {
System. out .println(pet.getPid()+ ":" +pet.getPname()+ ":" +pet.getBirthday());
System. out .println( "===================save a pet ===============================" );
}
}
|
6、 VO è Pet.java 示例
7、 表单示例: à user.jsp
< form action = "user_save.do" method = "post" >
id: < input type = "text" name = "pid" >< br >
<!-- 对应 pet 的 pid 属性 以下同 -->
pname: < input type = "text" name = "pname" >< br >
pbirthday: < input type = "text" name = "birthday" >< br >
isLive: < input type = "text" name = "isLive" >< br >
< input type = "submit" >< input type = "reset" >
</ form >
|
8、 解决上传表单有日期类型
1)
创建绑定类,实现接口 WebBindingInitializer
接口,实现其方法
public void initBinder(WebDataBinder binder,
WebRequest request) {
// TODO Auto-generated method stub
SimpleDateFormat sdf =
new SimpleDateFormat( "yyyy-MM-dd" );
sdf.setLenient( false );
binder.registerCustomEditor(Date. class ,
new CustomDateEditor(sdf, false ));
}
|
2) 在 spring-servlet.xml 中进行配置
<!-- 通用 date 转换 -->
< bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
< property name = "cacheSeconds" value = "0" />
< property name = "webBindingInitializer" >
< bean class = "org.springtest.mvc. 实现类名 " />
</ property >
</ bean >
|
分享到:
相关推荐
Spring mvc整合mybatis例子
to show how Spring MVC answers those hard questions (as well as the easy questions!) that web developers are faced with, like managing state in non-trivial multi-page use cases, for example, or ...
With the power of annotation-based configuration, Spring MVC makes web application development easy for developers. This book is a great companion for beginners who want to learn Spring MVC. With ...
spring mvc+mybatis+oracle+jquery easy ui实现增删改查,带拦截器
NULL 博文链接:https://nethub2.iteye.com/blog/2329387
Secure your developments with easy-to-write, reliable unit and end-to-end tests Deploy your application on the cloud for free and invite the whole world to see 作者简介 · · · · · · Geoffroy ...
spring mvc sample ,very easy to use it. Best regards
郑重声明:本文分享系统来自互联网,分享目的在于传递更多信息,帮助大家,并不代表本人赞同其观点和 对其真实性负责。如涉及作品内容、版权和其它问题,请在七日内与本人联系,我将在第一时间删除内容!...
从头打造Spring WEB MVC
基于spring mvc mybatis spring security 和jquery easy-ui的用户权限管理项目。
You will then learn about a variety of authentication mechanisms and how to integrate them easily with the Spring MVC application. You will also understand how to achieve authorization in a Spring ...
Spring MVC MyBatis + MyBatis Plus 数据访问(开启分页) Spring Boot 调试工具和项目处理器 Spring AOP 切面编程 Spring Scheduler 定时任务 Spring 事务注解 业务特性: 业务代码生成器(支持自动生成 Service、...
Spring makes it easy to create RESTful applications, merge with social services, communicate with modern databases, secure your system, and make your code modular and easy to test. With the arrival of...
这个框架简单,精炼,不超过10M,其中包括了SprigMVC的@Controller,@Transactional,@RequestMapping 部署和使用起来,so easy,ant的配置要适当修改一下。 部署的时候数据库链接要做适当的修改。
Build full-featured web applications such as Spring MVC applications efficiently that will get you up and running with Spring web development Learn dependency injection and aspect-oriented programming...
基于r2dbc ,easy-orm 的通用响应式CRUD H2,Mysql,SqlServer,PostgreSQL 响应式r2dbc事务控制 响应式权限控制,以及权限信息获取 RBAC权限控制 数据权限控制 双因子验证 多维度权限管理功能 响应式缓存 非...
easy to isolate these dependencies from the rest of your code base. This document is a reference guide to Spring Framework features. If you have any requests, comments, or questions on this document, ...
SpringMVC MyBits Jquery-Easy-UI 后台管理系统 源码 由SpringMVC MyBits Jquery-Easy-UI组成的后台管理系统
SMEU技术栈支持JQuery Easy UI,Spring MVC4,spring4, MyBatis 3。 本版支持下拉列表,使用者只需要在域对象相应的外键字段设定dropdown:DomainName fieldName; 即下拉列表:外键域名 字段名,即可一键支持下拉列表...
SMEU技术栈支持JQuery Easy UI,Spring MVC4, spring4, MyBatis 3。1.5版支持多对多关系,只要在多对多关系的主域对象中定义了 manytomanyslave:slaveDomainName即可在生成的功能和数据库定义中支持了两者的多对多 ...