springMVC_02hello案例
程序员文章站
2023-11-02 14:25:10
springMVC第一个案例---hellospringMVC ......
1.导入jar包
commons-logging-1.1.1.jar
jackson-annotations-2.5.4.jar
jackson-core-2.5.4.jar
jackson-databind-2.5.4.jar
spring-aop-4.1.6.release.jar
spring-beans-4.1.6.release.jar
spring-context-4.1.6.release.jar
spring-core-4.1.6.release.jar
spring-expression-4.1.6.release.jar
spring-tx-4.1.6.release.jar
spring-web-4.1.6.release.jar
spring-webmvc-4.1.6.release.jar
2.配置web.xml-配置分发器,在web.xml文件添加如下代码
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
3.添加springmvc配置文件,添加到web-inf目录下,名称规范:[dispatcherservlet]-servlet.xml,本案例为:springmvc-servlet.xml
4.编写控制器类hellocontroller,实现controller接口
package com.ahd.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 hellocontroller implements controller{ public modelandview handlerequest(httpservletrequest req, httpservletresponse resp) throws exception { // todo auto-generated method stub modelandview mv=new modelandview(); //设置要显示到视图的信息 mv.addobject("msg", "hello springmvc"); //设置视图名称 mv.setviewname("hello"); return mv; } }
5.编写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:p="http://www.springframework.org/schema/p" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置handlermapping --> <bean class=" org.springframework.web.servlet.handler.beannameurlhandlermapping"></bean> <!-- 配置handleradapter --> <bean class="org.springframework.web.servlet.mvc.simplecontrollerhandleradapter"></bean> <!-- 配置渲染器 --> <bean id="jspviewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="viewclass" value="org.springframework.web.servlet.view.jstlview"/> <!-- 将视图名 渲染后视图的前缀 --> <property name="prefix" value="/web-inf/jsp/"/> <!-- 渲染后视图的后缀 --> <property name="suffix" value=".jsp"/> <!-- 例:视图名为:hello 渲染后:/web-inf/jsp/hello.jsp 该页面--> </bean> <!-- 配置请求和处理器 --> <bean name="/hello.do" class="com.ahd.controller.hellocontroller"></bean>
</beans>
6.测试
下一篇: 媳妇翻东西发现两粒维生素D软胶囊