SpringMVC

编程

1.2、优点

简单,容易上手

灵活,容易扩展

性能优异

1.3、处理流程

2、spring mvc入门案列

2.1、说明

spring mvc案例需要建立的是maven web项目

2.1、依赖包

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<dependency>

<artifactId>org.springframework</artifactId>

<groupId>spring-aop</groupId>

<version>3.2.7.RELEASE</version>

</dependency>

<dependency>

<artifactId>org.springframework</artifactId>

<groupId>spring-aspects</groupId>

<version>3.2.7.RELEASE</version>

</dependency>

<dependency>

<artifactId>org.springframework</artifactId>

<groupId>spring-beans</groupId>

<version>3.2.7.RELEASE</version>

</dependency>

<dependency>

<artifactId>org.springframework</artifactId>

<groupId>spring-context</groupId>

<version>3.2.7.RELEASE</version>

</dependency>

<dependency>

<artifactId>org.springframework</artifactId>

<groupId>spring-context-support</groupId>

<version>3.2.7.RELEASE</version>

</dependency>

<dependency>

<artifactId>org.springframework</artifactId>

<groupId>spring-core</groupId>

<version>3.2.7.RELEASE</version>

</dependency>

<dependency>

<artifactId>org.springframework</artifactId>

<groupId>spring-expression</groupId>

<version>3.2.7.RELEASE</version>

</dependency>

<dependency>

<artifactId>org.springframework</artifactId>

<groupId>spring-web</groupId>

<version>3.2.7.RELEASE</version>

</dependency>

<dependency>

<artifactId>org.springframework</artifactId>

<groupId>spring-webmvc</groupId>

<version>3.2.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.aopalliance</groupId>

<artifactId>com.springsource.org.aopalliance</artifactId>

<version>1.0</version>

</dependency>

<dependency>

<artifactId>commons-logging</artifactId>

<groupId>commons-logging</groupId>

<version>1.1</version>

</dependency>

<!-- jstl标签包 -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

<dependency>

<groupId>jstl</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

<dependency>

<groupId>taglibs</groupId>

<artifactId>standard</artifactId>

<version>1.1.2</version>

</dependency>

2.3、controller层

package controller;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.Controller;

public class LoginController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String username = request.getParameter("username");

        ModelAndView mve = new ModelAndView();

        mve.addObject("username", username);

        mve.setViewName("index");

        return mve;

    }

}

2.4、spring mvc配置文件

spring建议命名规则****-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"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- 将url映射到具体的业务控制器 -->

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

<property name="mappings">

<props>

<prop key="login.do">login</prop>

</props>

</property>

</bean>

<bean id="login" class="controller.LoginController"></bean>

<!-- 配置视图解析器 -->

<!-- InternalResourceViewResolver:用于支持servlet,jsp的解析 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<!-- 当jsp模板需要使用jstl标签库时,需要配置,没有则不需要配置 -->

<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>

<!-- 查找视图页面的前缀和后缀 -->

<property name="prefix" value="/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

</beans>

2.5、web.xml

<!DOCTYPE web-app PUBLIC

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

<display-name>Archetype Created Web Application</display-name>

<!-- spring mvc提供了防止乱码的过滤器 -->

<filter>

<filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

<init-param>

<param-name>forceEncoding</param-name>

<param-value>true</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!-- 前端控制器 -->

<servlet>

<servlet-name>spring</servlet-name>

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

<!-- 默认情况下,DispatcherServlet会寻找WEB-INF下命名规范为[servlet-name]-servlet.xml的文件。 -->

<!-- 如果需要修改,就需要在servlet中添加<init-param></init-param> -->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/spring-servlet.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>spring</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

</web-app>

2.6、前端页面

login.jsp、一个简单的表单

<body>

<form action="login.do" method="post">

<table>

<tr>

<td>用户名:</td>

<td><input type="text" name="username" /></td>

</tr>

<tr>

<td>班级</td>

<td><input type="text" name="clazz" /></td>

</tr>

<tr>

<td colspan="2"><input type="submit" value="提交" /></td>

</tr>

</table>

</form>

</body>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>

<!-- 导入jstl标签库 -->

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!-- 开启el表达式 -->

<%@ page isELIgnored="false"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1>输入用户:${username }</h1>

</body>

</html>

2.7、测试及结果

以上是 SpringMVC 的全部内容, 来源链接: utcz.com/z/514295.html

回到顶部