springmvc 实例应用
springmvc 應(yīng)用實(shí)例
今天學(xué)習(xí),網(wǎng)上好多實(shí)例,好多照著坐下來都報(bào)錯,找了好多資料,總算是搞出來了,方面以后更多的人學(xué)習(xí),這里貼出全部代碼.與同仁共同交流.
項(xiàng)目結(jié)構(gòu)圖:
應(yīng)用實(shí)體
View Code package com.icreate.entity;import java.io.Serializable;/*** ** @version : 1.0* * @author : 蘇若年 <a href="mailto:DennisIT@163.com">發(fā)送郵件</a>* * @since : 1.0 創(chuàng)建時間: 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;} }數(shù)據(jù)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">發(fā)送郵件</a>* * @since : 1.0 創(chuàng)建時間: 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實(shí)現(xiàn),由于模擬所以,這里數(shù)據(jù)用虛擬數(shù)據(jù),并非從數(shù)據(jù)庫中讀到的數(shù)據(jù),模塊功能類似,這里只實(shí)現(xiàn)查詢模塊模擬
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">發(fā)送郵件</a>* * @since : 1.0 創(chuàng)建時間: 2013-4-9 上午11:15:50* * @function: TODO **/@Repository public class UserDaoImpl implements UserDao{public int countAll() {// TODO Auto-generated method stubreturn 0;}public int delete(User user) {// TODO Auto-generated method stubreturn 0;}public int insert(User user) {// TODO Auto-generated method stubreturn 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 stubreturn 0;}public User detail(int id) {// TODO Auto-generated method stubreturn null;}}業(yè)務(wù)service接口定義
package com.icreate.service;import java.util.List;import com.icreate.entity.User;/*** ** @version : 1.0* * @author : 蘇若年 <a href="mailto:DennisIT@163.com">發(fā)送郵件</a>* * @since : 1.0 創(chuàng)建時間: 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);}業(yè)務(wù)service實(shí)現(xiàn)?
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">發(fā)送郵件</a>* * @since : 1.0 創(chuàng)建時間: 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);}}業(yè)務(wù)模塊相關(guān)類就這么點(diǎn)
下面是Springmvc的控制器應(yīng)用類
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">發(fā)送郵件</a>* * @since : 1.0 創(chuàng)建時間: 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("控制器將執(zhí)行結(jié)果轉(zhuǎn)發(fā)給試圖/注意轉(zhuǎn)發(fā)試圖結(jié)果同dispatcher-servlet.xml對比");return "userlist";} }Web.xml文件內(nèi)容
<?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/目錄下則值應(yīng)該為:/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防止內(nèi)存泄露 --> <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監(jiān)聽器 --><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener><!--配置Sring MVC的核心控制器DispatcherServlet Spring view分發(fā)器--> <servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><!-- 如果放在/WEB-INF/目錄下則值應(yīng)該為:/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內(nèi)容
<?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的映射 --><beanclass="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內(nèi)容
<?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/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/txhttp://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中進(jìn)行事物請求
<%@ 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("控制器將執(zhí)行結(jié)果轉(zhuǎn)發(fā)給試圖/注意轉(zhuǎn)發(fā)試圖結(jié)果同dispatcher-servlet.xml對比");return "userlist";}方法的返回值一致.這樣才能查找到對應(yīng)的試圖頁面.
/WEB-INF/view/下的userlist.jsp中核心內(nèi)容如下,因?yàn)槲覀冊诳刂破髦袑?zhí)行結(jié)果數(shù)據(jù)放入到ModelMap中,所以試圖中直接取出該數(shù)據(jù)顯示即可注意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是我建立的項(xiàng)目名稱)
程序運(yùn)行結(jié)果:
附錄:
應(yīng)用所需jar包
轉(zhuǎn)載請注明出處:[http://www.cnblogs.com/dennisit/archive/2013/04/10/3012993.html]
轉(zhuǎn)載于:https://www.cnblogs.com/dennisit/archive/2013/04/10/3012993.html
總結(jié)
以上是生活随笔為你收集整理的springmvc 实例应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: grunt 0.4.1构建工具入门实践(
- 下一篇: WindowsFormsHost使用问题