package com.zl.entity;
public class Users {
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 com.zl.dao;
import java.util.ArrayList;
import java.util.List;
import com.zl.entity.Users;
public class UserDao {
public List<Users> getUsers(){
List<Users> list=new ArrayList<Users>();
Users u=null;
for (int i = 1; i < 6; i++) {
u=new Users();
u.setPassWord("zhang"+i);
u.setUserName("long"+i);
list.add(u);
}
return list;
}
public static void main(String[] args) {
UserDao ud=new UserDao();
for (Users u: ud.getUsers()) {
System.out.println(u.getUserName()+"---------"+ u.getPassWord());
}
}
}
package com.zl.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.zl.dao.UserDao;
import com.zl.entity.Users;
public class UserAction implements Controller{
private String viewPage;
public String getViewPage() {
return viewPage;
}
public void setViewPage(String viewPage) {
this.viewPage = viewPage;
}
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
UserDao ud=new UserDao();
List<Users> list=ud.getUsers();
ModelAndView m= new ModelAndView(this.getViewPage());
m.getModel().put("list", list);
return m;
}
}
WebRoot/WEB-INF/applicationContext-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="userAction.do">userAction</prop>
</props>
</property>
</bean>
<!-- 定义视图及jsp存放的路径 -->
<bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- 定义控制器 -->
<bean id="userAction" class="com.zl.action.UserAction">
<property name="viewPage">
<value>index</value>
</property>
</bean>
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispacherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispacherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
WebRoot/WEB-INF/jsp/index.jsp:
<%@ page language="java" isELIgnored="false" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table border="1">
<tr>
<td>用户名</td>
<td>密码</td>
</tr>
<c:forEach var="u" items="${list}">
<tr>
<td>${u.userName}</td>
<td>${u.passWord}</td>
</tr>
</c:forEach>
</table>
</body>
</html>