01.SpringMVC基础教程HelloWorld之登录

本文介绍如何搭建SpringMVC环境并实现简单登录功能,包括项目结构、关键代码及配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

         SpringMVC都不陌生,Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还可以是 Struts 这样的 Web 框架。


不废话了,本文主要是演示springMVC环境搭建以及一个简单的登录,不涉及到dao操作


首先是本工程截图 (dao是象征性的写进去的,后续再修改)



创建工程,并创建package后,在domain包下创建一个User对象

package cn.wangsitu.domain;

import java.io.Serializable;

public class User implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private Integer id;
	private String username;
	private String password;


	public User() {
	}
	public User(String username, String password) {
		this.username = username;
		this.password = password;
	}
<span style="white-space:pre">	</span>setXXX getXXX 略
}


UserDao代码

package cn.wangsitu.dao;

import org.springframework.stereotype.Repository;
import cn.wangsitu.domain.User;

@Repository	//通过spring注解定义一个dao
public class UserDao {
	public User login(String username,String password){
		User u = new User(username, password);
		return u;
	}
	
}

UserService代码

package cn.wangsitu.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import cn.wangsitu.dao.UserDao;
import cn.wangsitu.domain.User;

@Repository
public class UserService {
	
	@Autowired //将UserDao自动注入进来
	private UserDao userDao;
	
	public User login(String username,String password){
		return userDao.login(username, password);
	}

}

LoginCommad代码

package cn.wangsitu.web;

public class LoginCommad {

	private String username;
	private String 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;
	}
}

下面是比较重要的LoginController

package cn.wangsitu.web;

import javax.servlet.http.HttpServletRequest;

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

import cn.wangsitu.domain.User;
import cn.wangsitu.service.UserService;

@Controller
@RequestMapping(value="/admin")
public class LoginController {

	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/login.html")
	public String loginPage(){
		return "login";
	}
	
	@RequestMapping(value = "logon.html")
	public ModelAndView logon(HttpServletRequest request,LoginCommad loginCommand){
		if("zhangsan".equals(loginCommand.getUsername())){
			User user = userService.login(loginCommand.getUsername(), loginCommand.getPassword());
			request.getSession().setAttribute("user", user);
			return new ModelAndView("index");
		}else{
			return new ModelAndView("login","error","用户名或密码错");
		}
	}
	
}


 下面我们来看配置 

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_3_0.xsd"
	id="WebApp_ID" version="3.0">
	
	<!-- SpringMVC配置 -->
	<context-param>
		<param-name>contextConfiguration</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<servlet>
		<servlet-name>viewspace</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>viewspace</servlet-name>
		<url-pattern>*.html</url-pattern>  //这里写.html 是方便搜索引擎记录 可自定义
	</servlet-mapping>
	
</web-app>

applicationContext.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"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
    
    <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
    <context:component-scan base-package="cn.wangsitu.dao"/>
    <context:component-scan base-package="cn.wangsitu.service"/>
    
    
    <!-- 配置数据源 -->
</beans>


viewspace-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"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
	<context:component-scan base-package="cn.wangsitu.web"/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    	  p:viewClass="org.springframework.web.servlet.view.JstlView"
    	  p:prefix="/WEB-INF/jsp/"
    	  p:suffix=".jsp"
      />
      
</beans>      


文章对于的jar下载地址:http://download.youkuaiyun.com/detail/wang_situ/8379109



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值