springmvc登陆例子详情解析

来源:互联网 发布:mysql存储过程 编辑:程序博客网 时间:2024/06/16 13:10

springmvc经典的例子就是用户登陆和用户注册,可以说通过这两个例子,你就可以对spring有一个简单的了解。也可以和struts框架做一个比较。

接下来通过代码和文字来详细的解析一下springmvc登陆小例子。

首先是创建一个项目,本例都是在myeclipse环境开发


增加spring支持jar包


选择两个标红的jar,当然你也可以通过过在官网(https://spring.io/)下载最新的jar来引入你的项目也是可以的,这种方式比较自由,myeclipse自动加载的jar包并不是最新的jar.


首先开发view视图层:index.jsp.

error.jsp.success.jsp文件可以自行开发,只要能够标记即可。


核心代码:

 <body>   <form action="login.do" method="post">    username:<input type="text" name = "username" ><p>     password:<input type="password" name = "password" ><p>    <input type="submit" value="登录"> </form> <br>  </body>

控制层代码:

import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class LoginAction {        @RequestMapping("login.do")    public String login(String username,String password){        if ("test".equals(username) && "123".equals(password)) {            System.out.println(username +" 登录成功");            return "success";//        }        return "error";    }    }

业务层和模型层通过heibernate和mybatis框架来实现。由于是一个简单的入门例子,所以相对做的简单一点。接下来会做相关深层次的例子。

spmvc-servlet.xml文件配置代码该文件放在src目录下。

<?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:context="http://www.springframework.org/schema/context"       xmlns:p="http://www.springframework.org/schema/p"       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-3.0.xsd">    <!-- 启用spring mvc注解 -->    <context:annotation-config></context:annotation-config>    <!--  扫描包 -->    <context:component-scan base-package="com.mvc.*"></context:component-scan>        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  p:suffix=".jsp"></bean></beans>

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>  <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:applicationContext.xml</param-value>  </context-param>    <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <servlet>  <servlet-name>mvc</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param>     <param-name>contextConfigLocation</param-name>     <param-value>classpath:spmvc-servlet.xml</param-value> </init-param>  </servlet>    <servlet-mapping>  <servlet-name>mvc</servlet-name>  <url-pattern>*.do</url-pattern>  </servlet-mapping>    <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener></web-app>

最终运行结果:







1 0
原创粉丝点击