我们现在使用SpringMVC来做一个小的用户管理系统,由于重点在学习SpringMVC,这里我们就不用数据库了,直接把用户的信息写在一个Map中。
1.创建User类,放在org.h3c.model包中
package org.h3c.model; import javax.validation.constraints.Size; import org.hibernate.validator.constraints.NotEmpty; import org.hibernate.validator.constraints.Range; public class User { private int id; private String username; private String password; private String nickname; // private Group group; public User() { super(); // TODO Auto-generated constructor stub } public User(String username, String password, String nickname) { super(); this.username = username; this.password = password; this.nickname = nickname; } public User(int id, String username, String password, String nickname) { super(); this.id = id; this.username = username; this.password = password; this.nickname = nickname; } // public Group getGroup() { // return group; // } // public void setGroup(Group group) { // this.group = group; // } public int getId() { return id; } public void setId(int id) { this.id = id; } @NotEmpty(message="用户名不能为空!") public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Size(min=1,max=10,message="密码的长度应该在1-10位之间!") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", nickName=" + nickname + ", group=" // + group + "]"; } }
2.创建LoginController和UserController,放在org.h3c.controller包中
3.编写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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SSM</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- <param-value>classpath:spring-mvc.xml</param-value> --> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 --> <url-pattern>/</url-pattern> </servlet-mapping> <!--字符编码--> <filter> <filter-name>CharacterFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3.编写spring-mvc.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: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.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <mvc:annotation-driven/> <context:component-scan base-package="org.h3c.controller"/> <mvc:resources mapping="/images/**" location="/images/" /> <mvc:resources mapping="/css/**" location="/css/" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 据说3.0.5之后,下面这句话可以不要,为了保险起见还是加上吧! --> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <!-- 配置前缀,此处前缀代表的是页面放在/WEB-INF/jsp文件夹下面 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 配置后缀,文件后缀为.jsp --> <property name="suffix" value=".jsp"/> </bean> </beans>
4.编写jsp页面---list.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!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> <a href="add" >link</a> <br/> <c:forEach items="${users }" var="userMap"> ${userMap.key }----><a href="${userMap.value.username }">${userMap.value.username }</a>----->${userMap.value.password }---->${userMap.value.nickname } </br> </c:forEach> </body> </html>
时间: 2024-10-16 12:41:16