spring登陆简单小案例

用spring写的登陆案例
开发环境:eclipse+jdk1.7
目录结构:
这里写图片描述
jar包
这里写图片描述
loginAction的代码

package cn.spring.action;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class loginAction{
    @RequestMapping("/login.spring") // @RequestMapping 注解可以用指定的URL路径访问本控制层
    public String login(@RequestParam("username") String username, @RequestParam("password") String password,
            Model model) {

        if ("admin".equals(username) && "123".equals(password)) {
            model.addAttribute("username", username);
            return "jsp/success.jsp";
        } else {
            model.addAttribute("username", username);
            return "jsp/error.jsp";
        }
    }

}

User的代码

package cn.spring.entity;

public class User {
    private String username;
    private String password;

    public User(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }


    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}

applicationContext代码

<?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"  
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:task="http://www.springframework.org/schema/task"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
       http://www.springframework.org/schema/context   
       http://www.springframework.org/schema/context/spring-context-4.2.xsd  
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.2.xsd  
       http://www.springframework.org/schema/aop  
       http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  
       http://www.springframework.org/schema/cache  
       http://www.springframework.org/schema/cache/spring-cache-4.2.xsd  
       http://www.springframework.org/schema/task    
       http://www.springframework.org/schema/task/spring-task-4.2.xsd ">  


    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

    </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    </beans>

springmvc-servlet代码

<?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" xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:mvc="http://www.springframework.org/schema/mvc"   
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
       http://www.springframework.org/schema/context   
       http://www.springframework.org/schema/context/spring-context-4.2.xsd  
       http://www.springframework.org/schema/mvc  
       http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd  
       http://www.springframework.org/schema/aop  
       http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  
       http://www.springframework.org/schema/cache  
                http://www.springframework.org/schema/cache/spring-cache-4.2.xsd">  

        <context:component-scan base-package="cn.spring.action"></context:component-scan> 
    </beans>

web代码

<?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" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0">

  <display-name>spring1</display-name>  
  <web:welcome-file-list>
    <web:welcome-file>index.jsp</web:welcome-file>
  </web:welcome-file-list>

    <!-- spring配置文件 -->  
    <!-- <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:applicationContext*.xml</param-value>  
    </context-param>  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>   -->

    <!-- springmvc配置 -->  
    <!-- <servlet>  
        <servlet-name>spring</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:spring-servlet.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>spring</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping> -->  


    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.spring</url-pattern>
    </servlet-mapping>  

</web-app>

index.jsp代码

<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <form action="login.spring" method="post">
        用户名:<input type="text" name = "username" ><br/> 
        密     码:<input type="password" name = "password" ><br/>
    <input type="submit" value="登录"> 
</form>
</body>
</html>

error与success两个jsp里只是输出一句登陆成功和失败
就不上图了

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
包含基本步骤实现完整的JAVA框架搭建 1 创建web项目,创建dao包,service包,pojo包,controller包,mapper包, 2 导入架包,将架包导入到项目的lib 文件中,如图 3 导入配置文件,将如下配置文件导入到src下面 3.1 修改generatorConfig.xml文件,这个是mybatis的逆向工程,修改数据库名,用户名,密码,对应的包名,对应的项目。如图 3.2 修改jdbc.properits文件, 3.3 log4j.properties 日志文件 不用修改 3.4 修改spring-mvc 文件 .1 修改controller包的自动扫描注解,如图 2 定义jsp文件存放的路径 <property name="prefix" value="/jsp/" /> 如图 3.5修改spring-mabtis.xml 1 修改自动扫描的包 我建的包是com.hqyj.mana就配置 base-package="com.hqyj.mana" 2 修改mybatis的配置映射文件 找到id="sqlSessionFactory"的bean 修改它的name="mapperLocations"的属性的value值 我的mybaties映射文件放在com.hqyj.mana.mapper下的,那么value= classpath:com/hqyj/mana/mapper/*.xml。如图 3 修改mybatis接口所在的包名 找到如下图所示的bean , name="basePackage"的属性的value值就是你建的mybatis接口包名 4 修改web.xml 1 Web.xml在项目的src/main/webapp/web-inf/下面 如图 将老师给的web.xml文件覆盖进去, 下面几步我都配置好了,这个文件不管是新建了一个 项目,还是包名发生了变化,都是不要修改的。 所以以后新建了项目后,只需要把这个文件覆盖即可。 如果有同学自己建立的话,可以按照以下几个步骤建立 1 配置web表头 2 配置加载spring-mybatis.xml 3 配置编码过滤器 4 配置加载sping-mybaties文件所需要的类 5 配置处理javabean类销毁的类 6 配置sping-mvc的前端控制器 7 配置 项目默认访问页面和设置session的会话时间(可选,不是必须的) Ssm项目搭建完了!现在建立一张表,随便写一个功能,测试下项目是否搭建成功
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值