springmvc 实例应用
springmvc 应用实例
今天学习,网上好多实例,好多照着坐下来都报错,找了好多资料,总算是搞出来了,方面以后更多的人学习,这里贴出全部代码.与同仁共同交流.
项目结构图:
应用实体
View Code
package com.icreate.entity;
import java.io.Serializable;
/**
*
*
* @version : 1.0
*
* @author : 苏若年 <a href="mailto:DennisIT@163.com">发送邮件</a>
*
* @since : 1.0 创建时间: 2013-4-9 上午11:15:50
*
* @function: TODO
*
*/
public class User implements Serializable{
private int id;
private String username;
private String password;
private int sex; //1表示男 0表示女
private String email;
//getter() and setter ()
public String toString(){
return this.id + "\t" + this.username + "\t" + this.password + "\t" + this.sex + "\t" + this.email;
}
}
数据dao接口定义
View Code
package com.icreate.dao;
import java.util.List;
import com.icreate.entity.User;
/**
*
*
* @version : 1.0
*
* @author : 苏若年 <a href="mailto:DennisIT@163.com">发送邮件</a>
*
* @since : 1.0 创建时间: 2013-4-9 上午11:15:50
*
* @function: TODO
*
*/
public interface UserDao {
public int insert(User user);
public int delete(User user);
public int countAll();
public List<User> selectAll();
public int update(User user);
public User detail(int id);
}
Dao实现,由于模拟所以,这里数据用虚拟数据,并非从数据库中读到的数据,模块功能类似,这里只实现查询模块模拟
package com.icreate.dao.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.springframework.stereotype.Repository;
import com.icreate.dao.UserDao;
import com.icreate.entity.User;
/**
*
*
* @version : 1.0
*
* @author : 苏若年 <a href="mailto:DennisIT@163.com">发送邮件</a>
*
* @since : 1.0 创建时间: 2013-4-9 上午11:15:50
*
* @function: TODO
*
*/
@Repository
public class UserDaoImpl implements UserDao{
public int countAll() {
// TODO Auto-generated method stub
return 0;
}
public int delete(User user) {
// TODO Auto-generated method stub
return 0;
}
public int insert(User user) {
// TODO Auto-generated method stub
return 0;
}
public List<User> selectAll() {
List<User> list = new ArrayList<User>();
for(int i=0; i<5; i++){
User user = new User();
user.setId(i);
user.setUsername(UUID.randomUUID().toString().substring(0,8));
user.setPassword(UUID.randomUUID().toString().substring(0,15));
user.setSex(i%2);
user.setEmail(UUID.randomUUID().toString().substring(0,8)+"@163.com");
list.add(user);
}
return list;
}
public int update(User user) {
// TODO Auto-generated method stub
return 0;
}
public User detail(int id) {
// TODO Auto-generated method stub
return null;
}
}
业务service接口定义
package com.icreate.service;
import java.util.List;
import com.icreate.entity.User;
/**
*
*
* @version : 1.0
*
* @author : 苏若年 <a href="mailto:DennisIT@163.com">发送邮件</a>
*
* @since : 1.0 创建时间: 2013-4-9 上午11:15:50
*
* @function: TODO
*
*/
public interface UserService {
public int insert(User user);
public int delete(User user);
public int countAll();
public List<User> selectAll();
public int update(User user);
public User detail(int id);
}
业务service实现
package com.icreate.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.icreate.dao.UserDao;
import com.icreate.entity.User;
import com.icreate.service.UserService;
/**
*
*
* @version : 1.0
*
* @author : 苏若年 <a href="mailto:DennisIT@163.com">发送邮件</a>
*
* @since : 1.0 创建时间: 2013-4-9 上午11:15:50
*
* @function: TODO
*
*/
@Service
public class UserServiceImpl implements UserService{
@Autowired(required = true)
private UserDao userDao;
public int countAll() {
return this.userDao.countAll();
}
public int delete(User user) {
return this.userDao.delete(user);
}
public int insert(User user) {
return this.userDao.insert(user);
}
public List<User> selectAll() {
return this.userDao.selectAll();
}
public int update(User user) {
return this.userDao.update(user);
}
public User detail(int id) {
return this.userDao.detail(id);
}
}
业务模块相关类就这么点
下面是Springmvc的控制器应用类
package com.icreate.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.icreate.entity.User;
import com.icreate.service.UserService;
/**
*
*
* @version : 1.0
*
* @author : 苏若年 <a href="mailto:DennisIT@163.com">发送邮件</a>
*
* @since : 1.0 创建时间: 2013-4-9 上午11:15:50
*
* @function: TODO
*
*/
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired(required = true)
private UserService userService;
//@RequestMapping("/user.do")是说明这个方法处理user.do这个请求
@RequestMapping(value="/insert.do")
public String insert(ModelMap modelMap, User user){
return "";
}
@RequestMapping(value="/list.do")
public String list(ModelMap modelMap){
modelMap.put("users", this.userService.selectAll());
System.out.println("控制器将执行结果转发给试图/注意转发试图结果同dispatcher-servlet.xml对比");
return "userlist";
}
}
Web.xml文件内容
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置Spring上线文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 如果放在/WEB-INF/目录下则值应该为:/WEB-INF/applicationContext.xml -->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- 配置log4j
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
-->
<!-- Spring的log4j监听器 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!--配置Sring MVC的核心控制器DispatcherServlet Spring view分发器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 如果放在/WEB-INF/目录下则值应该为:/WEB-INF/dispatcher-servlet.xml -->
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--为DispatcherServlet建立映射 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- session超时定义,单位为分钟 -->
<session-config>
<session-timeout>20</session-timeout>
</session-config>
<!-- 出错页面定义 -->
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/common/500.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/common/500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/common/404.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/common/403.jsp</location>
</error-page>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
applicationContext.xml内容
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.icreate" /> <!-- 自动扫描所有注解该路径 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="userDao" class="com.icreate.dao.impl.UserDaoImpl" />
<bean id="userService" class="com.icreate.service.impl.UserServiceImpl" />
</beans>
dispatcher-servlet.xml内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.icreate.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
index.jsp中进行事物请求
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<jsp:forward page="${basePath}/user/list.do"></jsp:forward>
dispatcher-servlet.xml中我们配置的试图界面为/WEB-INF/view/下,后缀为.jsp文件
所以我们的userlist.jsp中jsp文件的名字一定要与 UserController 类中
@RequestMapping(value="/list.do")
public String list(ModelMap modelMap){
modelMap.put("users", this.userService.selectAll());
System.out.println("控制器将执行结果转发给试图/注意转发试图结果同dispatcher-servlet.xml对比");
return "userlist";
}
方法的返回值一致.这样才能查找到对应的试图页面.
/WEB-INF/view/下的userlist.jsp中核心内容如下,因为我们在控制器中将执行结果数据放入到ModelMap中,所以试图中直接取出该数据显示即可注意items=${users}大括号中的值要跟modelMap中存放的键值对中的键名称一致,同为users
modelMap.put("users", this.userService.selectAll());
<c:forEach items="${users}" var="user" varStatus="status">
<tr>
<!-- ${status.index+1}编号 -->
<td><c:out value="${user.id}" /></td>
<td><c:out value="${user.username}" /></td>
<td><c:out value="${user.password}" /></td>
<td><c:if test="${user.sex == 1}">
<c:out value="男" />
</c:if> <c:if test="${user.sex == 0}">
<c:out value="女" />
</c:if></td>
<td><c:out value="${user.email}" /></td>
<td>
<a href="/user/detail/${user.id}">detail</a> |
<a href="/user/toupdate/${user.id}">update</a> |
<a href="/user/delete/${user.id}">delete</a>
</td>
</tr>
</c:forEach>
访问URL:http://localhost:8080/springmvc/ (springmvc是我建立的项目名称)
程序运行结果:
附录:
应用所需jar包
转载请注明出处:[http://www.cnblogs.com/dennisit/archive/2013/04/10/3012993.html]
热爱生活,热爱Coding,敢于挑战,用于探索 ...