之前在学习SpringMVC的过程中,写了一个springMVC的实例,今天终于有时间把这个例子传上来。这个例子就是做了登陆这个最最简单的功能,使用springMVC尽可能简单的配置来实现的。下面是这个SpringMVC的目录结构,源码可以下载SpringMVC实例。
首先要对项目的web.xml进行配置,这个项目的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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springMVC1</display-name> <welcome-file-list> <welcome-file>view/login.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/config/spring/Context.xml classpath:/config/spring/springDao.xml </param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/config/log4j.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>/WEB-INF/config/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> <filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>具体的解释可以参照 点击这里。
在完成web.xml的配置后,根据web.xml中的需要的配置文件分别对context.xml(数据库相关的配置)、log4j.xml(日志文件的配置,具体配置解释可以点击这里)、spring-mvc.xml以及springDao.xml的配置。
其中spring-mvc.xml的配置内容是最基本的springMVC的配置,其内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 注解扫描包 --> <context:component-scan base-package="com.spri.controller" /> <!-- 开启注解 --> <mvc:annotation-driven/> <!-- 静态资源访问 --> <mvc:resources location="/img/" mapping="/img/**"/> <mvc:resources location="/js/" mapping="/js/**"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <mvc:view-controller path="/" view-name="redirect:/user/welcome" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize" value="10485760000" /> <property name="maxInMemorySize" value="40960" /> </bean> </beans>springDao.xml中配置了一些bean,在使用时通过set方法注入到相应的都相中去,下面是本文的基本的bean的配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [ <!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml"> ]> <beans> <bean id="userDao" class="com.spri.dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userSerciceImpl" class="com.spri.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userManager" parent="transactionBase"> <property name="target" ref="userSerciceImpl"></property> </bean> </beans>
以上这四个配置文件配置完成后就可以进行前后台的编码工作了。后台分别由controller、dao及其实现、service及其实现以及entity,这些都完成了这个登陆流程中相对简单的实现。
此例controller中没有对登陆密码进行验证,只是读取数据库中的用户名进行用户名的验证,在spring-mvc.xml中还对springmvc实例的项目登陆的首页进行了设置,contrler的内容如下:
package com.spri.controller; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.omg.IOP.ExceptionDetailMessage; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.spri.entity.User; import com.spri.service.UserService; @Controller @RequestMapping("/user") public class UserController { @Resource(name="userSerciceImpl") private UserService userSerciceImpl; @RequestMapping("/welcome") public String login(){ return "/login"; } @RequestMapping("/checkLogin") public String checkLogin(User luser, HttpServletRequest request ,HttpSession session){ User user = userSerciceImpl.getUser(luser.getUserName()); //User user = null; if(user!= null){ session.setAttribute("user",user); request.setAttribute("user",user); return "WEB-INF/view/loginMain"; } else{ return "error"; } } @RequestMapping("/loginSuccess") public String loginSuccess(){ return "WEB-INF/view/loginMain"; } }
登陆首页:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.9.1.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.validate.min.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.form.min.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>用户登陆</title> <script type="text/javascript"> function login(){ var form = document.forms[0]; form.action="/springMVC/user/checkLogin"; form.method="post"; form.submit(); } </script> </head> <body> <div id="logindlg" class="easyui-dialog" title="登陆"bstyle="width: 400px; height: 200px;"> <br> <form id="loginform" method="post"> <center> <table cellspacing="5px;"> <tr align="left"> <td>用户:</td> <td><input type="text" name="userName"/></td> </tr> <tr align="left"> <td>密码:</td> <td><input type="password" name="passWord" /></td> </tr> </table> <br> <a href="#1" onclick="login()">登陆</a> <a href="#2" onclick="user_register()">取消</a> </center> </form> </div> </body> </html>
登陆成功后的效果:
由于本人也是在学习的过程中,可能有些地方写的不对,还麻烦大家指正……
资料:
springMVC快速入门指导:http://jeromejaglale.com/doc/spring4_tutorial/mvc_web_application
springMVC和模板,怎么使用模板访问数据:http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html
什么是springMVC的Modelview:http://stackoverflow.com/questions/3423262/what-is-modelattribute-in-spring-mvc
springMVC中的表单和数据模型:http://bitbybitblog.com/forms-and-data-models-in-spring-mvc/
springMVC指导:http://www.mkyong.com/tutorials/spring-mvc-tutorials/