springmvc(3)

来源:互联网 发布:网络古风歌曲排行榜 编辑:程序博客网 时间:2024/05/23 00:06
书写实体bean对象时,如果属性有boolean类型的,在快捷方式生成get/set方法之后需要修改boolean类型的get/set方法,默认是用的is来设置的




类型转换:
1、当页面获取的数据是普通类型,可以直接通过参数获取
2、当页面获取的数据是一个实体对象的属性,且名称一模一样,可以用对象的方式获取
3、当需要从页面获取List类型的数据时,需要通过对象的方式获取(对象包含这个list集合),且需要创建对象(ArrayList)
public class Classes{  
    private List<Student> stuLst = new ArrayList<Student>();  



请求处理方法:
public void test2(Classes classBean){}
4、当需要从页面获取set对象的数据时,和list一样,只是需要在获取之前指定获取的个数
public class Classes{  
    private Set<Student> stuSet = new HashSet<Student>();  
    public Classes(){
    stuSet.add(new Student());
  stuSet.add(new Student());
  stuSet.add(new Student());
    }
}
5、map对象也需要通过对象来绑定
public class User{  
    private Map<String String> userMap = new HashMap<String String>();  



请求处理方法:
public void test5(User user){}


JSP页面上的提交项命名:参数名[‘键名’]
6、复合类型的数据,所谓复合数据类型,指代自定义Bean中某个属性又是一个自定义Bean.
public class UserBean{
private String name;
private int age;
private AddressBean address;
}
请求处理方法:
public void test5(User user){}


JSP页面提交项命名:复合属性名.属性名(第二个属性名是自定义的实体对象的属性名称)




数据类型转换:
1.属性编辑器(传统方式):局部,在方法内部有效
/**
 * 局部类型转换器,需要使用InitBinder注解
 * 固定参数WebDataBinder
 * @param binder
@InitBinder
public void convertDate(WebDataBinder binder){
//第一个参数代表你的需要转换的类型
binder.registerCustomEditor(Date.class, new DateConvertEditor());
}


public class DateConvertEditor extends PropertyEditorSupport{


/**
* text 代表页面传入的String的值
*/
@Override
public void setAsText(String text) throws IllegalArgumentException {
// TODO Auto-generated method stub
Date date = null;
try {
//此处可用“正则表达式”来制定一些规则
if(text.contains("-")){//     2012-12-12
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
date = format.parse(text);
}
if(text.contains("/")){//     2012/12/12
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
date = format.parse(text);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
this.setValue(date);
}
}


2.类型转换器:全局,框架内都可以使用,且是在获取页面数据之前就进行了数据转换,也就是说获取到的数据就已经是被转换后的数据了
/**
 * 全局类型转换器
 * org.springframework.core.convert.converter.Converter
 * @author Administrator
 * 
 */
public class GlobalDateConverter implements Converter<String, Date> {


public Date convert(String text) {
// TODO Auto-generated method stub
Date date = null;
try {
// 此处可用“正则表达式”来制定一些规则
if (text.contains("-")) {// 2012-12-12
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
date = format.parse(text);
}
if (text.contains("/")) {// 2012/12/12
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
date = format.parse(text);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return date;
}
}
这个全局类型转换器是针对springmvc的,所以需要在服务器启动springmvc的上下文(写在web.xml内,在容器启动是加载):
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/springmvc.xml</param-value>
</context-param>


日志,需要在WEB-INF下导入log4j.dtd、log4j.xml两个文件,在WEB-INF/lib下导入log4j的jar包:
private Logger log = Logger.getLogger(this.getClass());      //启用日志
log.info(userName);                                          //打印日志信息
需要在web.xml中启动日志框架:
<!-- 启动LOG4J日志框架 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<!-- 设置系统的部署路径获取方式 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>springmvc.root</param-value>
</context-param>


<!-- 设置LOG4J配置文件的路径 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<!-- 设值LOG4J刷新时间,单位毫秒 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>




后台实现数据验证:
第一步、在实体bean中做验证规则(注解的方式实现):
public class UserBean {
@NotEmpty(message="用户名不能为空!")
@Pattern(regexp="\\w{6,12}",message="用户名必须是6-12位的!")
private String userName;//用户名
@Pattern(regexp="\\d{6}")
private String password;//密码
@Min(18)
@Max(60)
private int age;//年龄
}
第二部:在jsp网页中先导入标签库
<%@taglib prefix="springform" uri="http://www.springframework.org/tags/form" %>
第三步:在表单内书写
!-- 使用spring表单标签库 -->
<springform:form action="users/regist" method="post" modelAttribute="user">
用戶名:<input type="text" name="userName" />
<font style="color: red;"size="3"><springform:errors path="userName"delimiter=","></springform:errors></font> <br/>
密碼:<input type="text" name="password" value="123456"/>
<springform:errors path="password" delimiter=","></springform:errors>  <br/>
年齡:<input type="text" name="age" value="12"/> 
<springform:errors path="age" delimiter=","></springform:errors><br/>
</springform:form>
0 0
原创粉丝点击