-
引入jar包
2. web.xml 配置 DispatcherServlet 及 springmvc.xml 的路径
<!-- Spring MVC --> <servlet> <servlet-name>springServlet</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>springServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
3. spring-mvc.xml 配置
<!-- 注解扫描包 --> <context:component-scan base-package="com.*" /> <!-- 配置视图解析 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean>
4. 编写 controller 类
package com.web; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping(value = "/helloworld") public class HelloWorldController{ @RequestMapping(value="sayHello") public String sayHello(Model model){ model.addAttribute("hello", "helloworld"); return "/modules/helloworld"; } }
5. 访问;
<a href="/springmvc-helloworld/helloworld/sayHello">sayHello</a>
6. 显示:
${hello}
7. 结果:
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/8010.html