首先声明,我是一个菜鸟。一下文章中出现技术误导情况盖不负责
springmvc 应用实例
明天习学,网上很多多少实例,很多多少照着坐下来都报错,找了很多多少资料,总算是搞出来了,方面当前更多的人习学,这里贴出部全代码.与同仁独特流交.
项目结构图:
应用实体
View Code
数据dao口接定义
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;
}
}
View Code
Dao实现,由于模拟所以,这里数据用拟虚数据,并非从数据库中读到的数据,块模功能相似,这里只实现查询块模模拟
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);
}
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;
}
}
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);
}
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";
}
}
<?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>
<?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>
<?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>
<%@ 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>
所以我们的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>
程序运行结果:
附录:
应用所需jar包
转载请注明出处:[http://www.cnblogs.com/dennisit/archive/2013/04/10/3012993.html]
文章结束给大家分享下程序员的一些笑话语录: 一个合格的程序员是不会写出 诸如 “摧毁地球” 这样的程序的,他们会写一个函数叫 “摧毁行星”而把地球当一个参数传进去。