新建Javaweb项目配置SpringMvc

实现功能:

使用myeclipse新建javaweb项目,配置springmvc,实现简单的控制器来访问视图页面

一、新建javaweb项目

新建Javaweb项目配置SpringMvc_第1张图片
新建Javaweb项目配置SpringMvc_第2张图片
新建Javaweb项目配置SpringMvc_第3张图片
新建Javaweb项目配置SpringMvc_第4张图片
新建Javaweb项目配置SpringMvc_第5张图片
创建成功:
新建Javaweb项目配置SpringMvc_第6张图片

二、导入jar包

WEB-INF文件夹新建lib文件夹:
新建Javaweb项目配置SpringMvc_第7张图片
导入spring jar包:
下载地址:https://github.com/joytom/spring_jarhttps://github.com/joytom/spring_jar

三、新建视图文件

新建Javaweb项目配置SpringMvc_第8张图片
新建一个hehe.jsp就行了:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'hehe.jsp' starting pagetitle>
    
	<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">
	

  head>
  
  <body>
    This is my JSP page. 12312312<br>
  body>
html>

四、新建控制器

新建Javaweb项目配置SpringMvc_第9张图片
新建一个HelloController就行:

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import vo.User;
@Controller
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping("/hehe")
    public String hehe()
    {
        System.out.println(123);
        return "hehe";
    }
}

五、引入配置文件

引入两个就行
applicationContext.xml


<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="controller">context:component-scan>
beans>

springmvc-config.xml


<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="controller">context:component-scan>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        <property name="prefix" value="/WEB-INF/view/" />
        
        <property name="suffix" value=".jsp" />
    bean>
beans>

新建Javaweb项目配置SpringMvc_第10张图片

六、web.xml的配置

web.xml

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <display-name>Archetype Created Web Applicationdisplay-name>
  
  <context-param>
    
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xmlparam-value>
  context-param>
  <listener>
    
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  
  <servlet>
    <servlet-name>dispatcherservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springmvc-config.xmlparam-value>
    init-param>

    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>dispatcherservlet-name>
    
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

七、浏览器访问

新建Javaweb项目配置SpringMvc_第11张图片

你可能感兴趣的:(SpringMVC)