SpringMVC简单配置

执行流程

页面请求---->web.xml---->DispatcherServlet---->HandlerMapping---->Controller---->ModelAndView---->ViewResolver---->View

一、配置文件实现MVC

1、导入相应的jar包

2、配置web.xml文件:配置DispatcherServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <servlet>
  	<servlet-name>springMVC</servlet-name>
    <!-- 在<servlet>标签中配置DispatcherServlet的访问路径 -->
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
        <!-- 指定springMVC的地址 -->
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:applicationContext.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springMVC</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>


a.	servlet-class的值是一个jar包中的DispatcherServlet类

b.	<init-param>标签中的<param-name>标签中写入contextConfigLocation这个值是固定的,他是dispathcherServlet中的一个属性。<param-value>标签中写入classpath:SpringMVC的配置文件地址。

c.	配置<load-on-startup>值为1即在项目启动时创建servlet对象

d.	配置servlet-mapping

3、配置Spring配置文件:配置HandlerMapping、ViewResolver

<?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:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
	
        <bean id="loginController" class="com.bb.controller.LoginController"></bean>
	<!-- 定义HandlerMapping,用于请求的映射 -->
	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="login.do">loginController</prop>
			</props>
		</property>
	</bean>
	<!-- 定义ViewResolver,实现视图映射 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
		<!-- 前缀 -->
		<property name="prefix" value="/"></property>
		<!-- 后缀 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

4、模拟创建一个Controller类

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class LoginController implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		req.setCharacterEncoding("utf-8");
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		System.out.println(username + ", " + password);
		if("老罗".equals(username) && "1234".equals(password)) {
                        //根据配置文件访问/success.jsp
			return new ModelAndView("success");
		}
		// 转发错误信息
		ModelMap map = new ModelMap();
		map.put("error", "用户名或密码错误");
                //根据配置文件访问/index.jsp
		return new ModelAndView("index", map);
	}
}

5、一个简单的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>登录</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>
    <form action="login.do">
    	用户名<input name="username"><span style="color:red;">${error}</span><br>
    	密码<input type="password" name="password"><br>
    	<input type="submit" value="登录">
    </form>
  </body>
</html>

二、注解实现MVC

1、jar包必不可少

2、web.xml和上面的一样

3、Spring配置文件里只需要两行

    <!-- 开启IOC注解 -->
    <context:component-scan base-package="com.oracle"></context:component-scan>
    <!-- 开启MVC注解 -->
    <mvc:annotation-driven></mvc:annotation-driven>

4、Controller类就不一样喽,这次我们不需要去实现Controller接口了。

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

@Controller
@RequestMapping("/user")
public class LoginController {

	@RequestMapping("/login.do")
	@ResponseBody	
	public String login(String username ,String password) {
		if("老罗".equals(username) && "1234".equals(password)) {
			//向页面返回success
			return "success";
		}
		//向页面返回失败信息
		return "fail";
	}
}


如果不加@ResponseBody	return返回值则内部消化

return "/index.jsp";  //转发到index.jsp页面
return "redirect:/index.jsp";  //重定向到index.jsp页面

return "/user/login.do";  //转发给/user/login.do方法
return "redirect:/user/login.do";  //重定向到/user/login.do方法

5、这回页面就需要通过js进行请求和判断了,很简单,我就不写了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值