http://7xvpsh.com1.z0.glb.clouddn.com/publish/21-2/the-dispatcher-servlet.htmlhtml
springmvc4.1.7:配置 前端
复制转载大神笔记java
https://blog.csdn.net/lpch0825/article/details/79391982web
一、导入jar包spring

注解主要在spring-webmvc-3.2.8.RELEASE.jar中spring-mvc
二、web.xml配置文件session
web.xml中主要配置springMVC的前端控制器(核心控制器)mvc
注:这里采用xxx-servlet.xml默认命名方式,而且文件位于/WEB-INF目录下,因此在web.xml中不须要配置<init-param></init-param>app
-
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
-
<display-name>spring_mvc_annotation
</display-name>
-
-
-
-
<servlet-name>springmvc
</servlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
-
-
-
<servlet-name>springmvc
</servlet-name>
-
<url-pattern>/
</url-pattern>
-
-
-
三、springmvc-servlet.xml配置文件框架
注意:表头这里添加mvc声明,声明的地址在 spring-webmvc-3.2.8.RELEASE.jar 中的 META-INF/spring.schemas 中。这样之后<mvc:xxx>标签才会有效
-
<?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:mvc=
"http://www.springframework.org/schema/mvc" <!
-- 注意添加
mvc声明
-->
-
xmlns:aop="http://www.springframework.org/schema/aop"
-
xmlns:tx="http://www.springframework.org/schema/tx"
-
xmlns:context="http://www.springframework.org/schema/context"
-
-
http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
-
http://www.springframework.org/schema/aop
-
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
-
http://www.springframework.org/schema/tx
-
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
-
http://www.springframework.org/schema/mvc
<!-- 注意添加mvc声明 -->
-
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
<!-- 注意添加mvc声明,注意版本,不写版本会有默认版本 -->
-
http://www.springframework.org/schema/context
-
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
-
-
-
<context:component-scan base-package="com.hfxt.controller">
</context:component-scan>
-
<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 根据控制器返回的字符串拼接成jsp路径:/WEB-INF/page/xx.jsp -->
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
<property name="prefix" value="/WEB-INF/page/"/>
<!-- 前缀 -->
-
<property name="suffix" value=".jsp"/>
<!-- 后缀 -->
-
-
四、控制层(controller层)
注意:这里的login.jsp已经放入/WEB-INF/page目录下,为了方便视图解析器处理:是进入index.jsp页面仍是返回login.jsp登陆页面。这时初次进入login.jsp就须要利用toLogin()方法了。
-
package com.hfxt.controller;
-
-
import javax.servlet.http.HttpSession;
-
import org.springframework.stereotype.Controller;
-
import org.springframework.ui.Model;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RequestMethod;
-
@Controller //在类上面定义,代表该类为控制器,返回字符串与redirect:xxx
-
@RequestMapping(value="/") //在类或方法上面使用此注解,设置URL访问地址。它有两个属性,value指定访问路径,method指定指定请求方式,请求方式在RequestMethod这个类中,所有以常量形式定义,它默认使用GET请求。
-
public class LoginController {
-
@RequestMapping(value="/login",method=RequestMethod.GET) //访问.../login,方式为get时,该方法处理请求
-
-
-
-
-
@RequestMapping(value="/login",method=RequestMethod.POST)
-
public String doLogin(String username , String password , Model model , HttpSession session){ //访问.../login,方式为post时,该方法处理请求
-
if("admin".equals(username)&&"123".equals(password)){
-
session.setAttribute("username", username);
-
model.addAttribute("message", "登陆成功!");
-
-
-
model.addAttribute("message", "登陆失败!");
-
-
-
-
五、jsp页面
login.jsp页面代码
-
<?xml version="1.0" encoding="UTF-8" ?>
-
<%@ page language="java" contentType="text/html; charset=UTF-8"
-
-
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-
<title>Insert title here
</title>
-
-
-
<form action="/spring_mvc_annotation/login" method="post">
-
用户名:
<input type="text" name="username" value=""/>
<br />
-
密码:
<input type="password" name="password" value=""/>
<br />
-
<input type="submit" value="登陆"/>
-
-
<div style="color: red">${message }
</div>
-
-
index.jsp页面代码
-
<?xml version="1.0" encoding="UTF-8" ?>
-
<%@ page language="java" contentType="text/html; charset=UTF-8"
-
-
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-
<title>Insert title here
</title>
-
-
-
<h1>${sessionScope.username },${message }
</h1>
-
-
Spring MVC是当前最优秀的MVC框架,自从Spring 2.5版本发布后,因为支持注解配置,易用性有了大幅度的提升。上一篇博文已经介绍了最简单的配置文件的详情,这里再介绍一下最简单的注解配置详情,毕竟springMVC是鼓励使用注解的。
一、导入jar包

注解主要在spring-webmvc-3.2.8.RELEASE.jar中
二、web.xml配置文件
web.xml中主要配置springMVC的前端控制器(核心控制器)
注:这里采用xxx-servlet.xml默认命名方式,而且文件位于/WEB-INF目录下,因此在web.xml中不须要配置<init-param></init-param>
-
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
-
<display-name>spring_mvc_annotation
</display-name>
-
-
-
-
<servlet-name>springmvc
</servlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
-
-
-
<servlet-name>springmvc
</servlet-name>
-
<url-pattern>/
</url-pattern>
-