1.cope jar包到lib中
2.配置web.xml文件
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
3.在web-inf中创建springmvc的配置文件springmvc-servlet.xml
分别配置注解驱动、扫描器、视图解析器
<!-- springmvc注解驱动 --> <mvc:annotation-driven/> <!-- 扫描器 --> <context:component-scan base-package="com.tideway.springmvc"></context:component-scan> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"></property> <property name="suffix" value=".jsp"></property> </bean>
4.创建发送请求页面index.jsp
<form action="hello.do" method="post"> ID:<input type="text" name="userName"/> <input type="submit" value="submit"/> </form>
5.创建控制器HelloWorld.java
package com.tideway.springmvc; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorld { @RequestMapping(value="/hello.do") public String hello(String userName,Model model){ System.out.println(userName); model.addAttribute("helloworld", "Hello:"+userName); return "success"; } }
6.在WebContent文件夹下创建views文件夹下创建响应页面success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <!-- el方式 --> <h1>${helloworld }</h1> </body> </html>
时间: 2024-10-26 10:12:07