搭建一个springmvc helloworld程序

Posted hy7873

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建一个springmvc helloworld程序相关的知识,希望对你有一定的参考价值。

1、加jar包,需要8个,从springframework里面选

logging core aop context expression bean web webmvc 

2、配置web.xml,在文件中配置一个servlet

<?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">
    
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

注意:1、contextConfigLocation 可以不配,默认找/WEB-INF/servlet-name  + "-servlet.xml";2、servlet配置为加载项目时自动启动;3、默认url-pattern为/处理所有请求

 

3、写一个pojo 注解为controller

package com.hy.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorld {
    
    @RequestMapping("/helloworld")
    public String hello() {
        System.out.println("hello");
        return "success";
    }
}

 

4、编写springmvc配置文件

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        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-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <!-- 配置包扫描 -->
    <context:component-scan base-package="com.hy.springmvc"></context:component-scan>
    <!-- 配置视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

分别配置包扫描和视图解析器InternalResourceResolver(prefix + rerturn string  + suffix)

 

5、建立jsp页面测试

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>welcome page</title>
</head>
<body>
<a href="helloworld">Hello World</a>
</body>
</html>

注意:href直接配置helloworld 不要/helloworld..

 

以上是关于搭建一个springmvc helloworld程序的主要内容,如果未能解决你的问题,请参考以下文章

Eclipse+Maven+springmvc+HelloWorld--Eclipse Maven springMVC第一个HelloWorld吐血配置整理

SpringMVC框架整理

SpringMVC基础入门,创建一个HelloWorld程序

SpringMVC_HelloWorld_01

springMVC-HelloWorld

学习SpringMVC——从HelloWorld开始