01-SpringMVC-第一个Hello示例程序

starrylsi
2024-09-13 / 0 评论 / 22 阅读 / 正在检测是否收录...

1. 创建一个动态的web工程

2. 导入SpringMVC的包

commons-logging-1.1.3.jar
log4j-1.2.17.jar
spring-aop-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar

3、创建工程需要的配置文件

(1) log4j.properties

# Global logging configuration
log4j.rootLogger=INFO, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

(2) 创建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"
    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-4.0.xsd">
    <!--扫描包-->
    <context:component-scan base-package="com.zhuama"></context:component-scan>
    
</beans>

4、创建jsp页面

WebContent/index.jsp

    <body>
      <h1>This is my first application</h1>
    </body>

5、编写一个类HelloController

/**
 *  @Controller 表示当前类是一个控制器
 */
@Controller
public class HelloController {
    /**
     * @RequestMapping("/hello") <br/>
     *      表示在SpringMVC中注册一个控制器,请求地址是http://ip:port/工程名/hello
     * @return
     */
    @RequestMapping("/hello")
    public String hello() {
        System.out.println("这是SpringMVC的hello程序");
        return "index.jsp";
    }
    
}
0

评论 (0)

取消