现在的位置: 首页 > 综合 > 正文

一个Spring MVC简单的登录小实例(不带数据库)

2013年08月17日 ⁄ 综合 ⁄ 共 4866字 ⁄ 字号 评论关闭

本文由菜鸟编写。如发现文中有什么不对的地方,希望留言给予指出。谢谢

1、Spring mvc相关的jar包。

Spring 3.2.2的下载地址。。。从官网下载直接上传

这是我的目录:

 

2、建好项目,加载相关包。首先配置web.xml。DispatcherServlet 是Spring MVC 的入口。所有请求都会调用此接口。
需要在 web.xml 中注册 DispatcherServlet

<?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控制器和映射 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- 拦截所有以action 结尾的请求 --> 
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
	
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

3、在web.xml相同的路径下新建一个配置文件。名字为springmvc-servlet.xml。名字要与web.xml中的<servlet-name>springmvc</servlet-name>相同,再加上-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="

http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans.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.xsd">
	
	<!-- 默认扫描的包路径 -->
    <context:component-scan base-package="org.cfc.com.spring" />
     
    <!-- 启用spring mvc 注解 -->
    <context:annotation-config /> 
	
    <!-- 定义跳转的文件的前后缀   -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/" />
    	<property name="suffix" value=".jsp" />
    </bean>
</beans>

问题:如将 <property name="prefix" value="/" /> 配置为 <property name="prefix" value="/WEB-INF/user/" /> 总是出现找不到。但路径是没有问题的。请指教??

总会出现“The requested resource (/springmvcdemo1/user/success.jsp) is not available.”这个问题

 

4、框架已经搭建好了。现在开始做一个简单的登录。前台建好三个页面。login.html、fail.jsp和success.jsp以下是其代码。

login.html  登录页面

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>login.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
    <form action="login.action" method="post">
    	<label>username:</label><input type="text" name="username" /><br>
    	<label>password:</label><input type="text" name="password" /><br>
    	<input type="submit" value="submit" />
    </form>
  </body>
</html>

success.jsp   登录成功页面,用EL获取用户名

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'MyJsp.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    	欢迎${username }登录
  </body>
</html>

 

fail.jsp  登录失败页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'MyJsp.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   	<h1>ERROR</h1>
  </body>
</html>

 

5、前台建好了,在后台建立控制器。

在org.cfc.com.spring下建立一个类。

package org.cfc.com.spring;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class ControllerDemo01 {
	
	//@Controller注解标识一个控制器,@RequestMapping注解标记一个访问的路径(/index)
	//return "index"标记返回视图(index)
	@RequestMapping("/user/login")
	public ModelAndView regist( String username,String password,HttpServletRequest request){
		
		if("admin".equals(username) && "admin".equals(password)){
			request.setAttribute("username", username);
			return new ModelAndView("/user/success");
		}
		return new ModelAndView("/user/fail");
	}
}

 

6、已经完成。启动tomcat。结果如下图。

附上spring mvc工作原理图。有助于理解。

 

抱歉!评论已关闭.