SpringMVC入门

    技术2022-05-20  43

    一个简单的SpringMVC入门的例子。

    准本工作:

    首先:创建一个JavaWeb项目,添加如下Jar包

    -->spring.jar(必须的)

    -->commons-logging-api-1.1.jar( 必须的 )

    -->spring-webmvc.jar(使用SpringMVC用的)

    其次:配置webx.ml文件

    <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <!-- 配置Spring的配置文件的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置Spring的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置SpringMVC的核心控制器DispatcherServlet --> <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:applicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <!-- 配置首页 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

    1.首页index.jsp

    <% response.sendRedirect("index.html"); %>

    2.跳转到index.html映射的控制器TestController.java

    package cdl; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; /** * 自定义的控制器要实现Controller接口 * @author Root * */ public class TestController implements Controller { /** * 注入一个参数 */ private String name; public void setName(String name) { this.name = name; } /** * 注入Service */ private TestService testService; public void setTestService(TestService testService) { this.testService = testService; } /** * Controller接口中唯一的方法 * @param request current HTTP request * @param response current HTTP response * @return a ModelAndView * 第一参数映射视图名称.比如:ok.jsp * 第二个和第三个参数相当于request.setAttribute(String name, Object obj); * @throws Exception */ public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse resonse) throws Exception { System.out.println("我在控制器中..."); return new ModelAndView("ok", "msg", this.testService.say(this.name)); } }

    3.编写控制器调用的接口TestService.java与其实现类TestServiceImpl.java

    package cdl; public interface TestService { /** * 一个说的方法 * @param name 谁说的 * @return 说话的内容 */ public String say(String name); }

    package cdl; public class TestServiceImpl implements TestService { /** * 一个说的方法 * @param name 谁说的 * @return 说话的内容 */ public String say(String name){ return name+"说:I Like SpringMVC...."; } }

    4.编写映射路径的配置applicationContext.xml

    <?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 配置Service --> <bean id="testService" class="cdl.TestServiceImpl"/> <!-- 配置控制器 --> <bean name="/index.html" class="cdl.TestController"> <property name="name" value="root"/> <property name="testService" ref="testService"/> </bean> <!-- 另一种处理方式 <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/index.html">testController</prop> </props> </property> </bean> <bean name="testController" class="cdl.TestController"> <property name="name" value="root"/> <property name="testService" ref="testService"/> </bean> --> <!-- 配置视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/"/> <!-- 前缀:/jsp/说明在WebRoot/jsp/目录下 --> <property name="suffix" value=".jsp"/> <!-- 后缀:对应的视图后缀名,比如视图是jsp文件 --> </bean> </beans>

    运行成功的话,输出:root说:I Like SpringMVC....

    附 :


    最新回复(0)