SpringMvc基础基础最基础

来源:互联网 发布:快消行业数据分析 编辑:程序博客网 时间:2024/04/26 01:26

配置步骤:

1.首先我们需要配置web.xml

<servlet>    <servlet-name>springmvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <init-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:springmvc.xml</param-value>  </init-param>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>springmvc</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping>  <!--<servlet-mapping>-->    <!--<servlet-name>default</servlet-name>-->    <!--<url-pattern>.jpg</url-pattern>-->  <!--</servlet-mapping>--></web-app>
2.创建实体cn.happy.Controller下创建类 firstController
import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Created by Administrator on 2017/8/14. */public class firstController implements Controller{    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {    ModelAndView modelAndView=new ModelAndView();        modelAndView.addObject("uname","y2165");        modelAndView.setViewName("/index");        return modelAndView;    }}
3.配置小配置文件名字为springmvc.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:p="http://www.springframework.org/schema/p"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx.xsd">    <!--<mvc:default-servlet-handler/>-->    <mvc:resources    mapping="/img/**" location="/img/"></mvc:resources>    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="WEB-INF"></property>        <property name="suffix" value=".jsp"></property>    </bean>    <bean id="/happy.do" class="cn.happy.Controller.firstController"></bean></beans>
4.创建index.jsp文件

<%--  Created by IntelliJ IDEA.  User: Administrator  Date: 2017/8/14  Time: 9:52  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %><html><head>    <title>title</title></head><body><h2>SpringMVC,${uname}</h2><img src="img/1.jpg"></body></html>