您的位置:首页 > 编程语言 > Java开发

SpringMVC 登录DEMO

2014-08-29 16:56 197 查看
       引言:最近工作上接触到了Spring的mvc,之前没有接触过,感觉很陌生。今天有点闲就在网上找了点资料学习了,也算是成功了吧!之前一直都在用Struts2的mvc,相比之下,我更喜欢用Spring的mvc。总之各有各的优点吧!看看各位怎么用了。

   下面是我基于Spring MVC写的一个简单的登录demo。

工程一览图:



下面简单的看下具体的代码:

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"> <display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern><!-- 意思是拦截所有请求 -->
</servlet-mapping>
</web-app>
springMVC-servlet.xml:

<?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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
<mvc:annotation-driven />
<!-- 注释资源扫描包路径 -->
<context:component-scan base-package="com.controllers" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
TestController:

package com.controllers;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.models.Student;
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "/test")
public String test(HttpServletRequest request) {
Student s = new Student();
System.out.println("name===" + s.getName());
System.out.println("password===" + s.getPassword());
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
request.setAttribute("userName", userName);
if (s.getName().equals(userName) && s.getPassword().equals(userPwd)) {
return "success";
}
return "fail";
}
}


    至于jsp里面的代码就不展示了,感兴趣的可以直接去下载案例,里面的Spring的包都齐全,可以直接运行的,但愿对像我一样的初学者有所帮助!

demo地址http://download.csdn.net/detail/javaweiming/7839841
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: