首发地址:http://inmethetiger.iteye.com/blog/1682586
最近没什么时间看spring。刚刚逛博客,发现一个写ModelAttribute的博客。很简单。而我对这个标签没使用过。所以,试着写了一下。这个主要是用来干什么的,不清楚。先写在这里再说。
目录结构如下:
src
--org.spring.controller
UserController.java
--org.spring.model
User.java
WebContent
WEB-INF
--view
login.jsp
user.jsp
index.jsp
//其他web.xml.dispatchserlet.xml略
代码如下:
模型类:
package org.spring.model; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
控制器:
package org.spring.controller; import java.util.Map; import org.spring.model.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class UserController { @RequestMapping(value = "/show.do") public String show(@ModelAttribute("user") User user, Map<String, Object> model) { model.put("user", user); return "user"; } // 我们不能直接访问使用Spring表单标签的JSP页面,要先通过controller处理modelAttribute和表单标签的绑定才能访问 @RequestMapping(value="/login.do",method=RequestMethod.GET) public String login(Map model) { User user = new User(); model.put("user", user); return "login"; } }
配置文件。因为采用的注解。所以很简单
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="org.spring.controller"></context:component-scan> <bean id="defaultViewResover" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
登录页面。刚开始把他放到WebContext下。但是,报错。说是没有WebApplicationContext。(我没有配置这个东西。)所以,把他放到view下。然后通过index.jsp的forward方法,跳转到这个页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@ page import="org.spring.model.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录</title> </head> <body> <form:form modelAttribute="user" method="POST" action="show.do"> <table> <tr> <td>用户名</td> <td><form:input path="username" /></td> </tr> <tr> <td><form:input path="password" /></td> </tr> <tr> <td>密码</td> <td><input type="submit" value="O K"></td> </tr> </table> </form:form> </body> </html>
显示页面。到今天才知道,想要用jstl标签库。除了一个jstl.jar外还需要一个standard.jar。查看了一下。貌似jstl.jar是一套接口,而standard.jar则是实现该接口的类。而且http://java.sun.com/jsp/jstl/core没有在jstl.jar中存在,而是在standard.jar中存在
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; utf-8"> <title>Insert title here</title> </head> <body>name=${user.username},password=${user.password} </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>LOGIN</title> </head> <body> <jsp:forward page="login.do"></jsp:forward> </body> </html>
一些介绍ModelAttribute的资料
[@Controller]4 详解@ModelAttribute
http://blog.sina.com.cn/s/blog_6d3c1ec601017q4p.html
Spring3MVC 在JSP中使用@ModelAttribute
http://liukai.iteye.com/blog/973717