建立web項目html
導入jar包前端
在web.xml中配置mvc框架的前端控制器java
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springmvc02</display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定springmvc配置文件的位置 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
四、 在類路徑下提供springmvc 框架的核心配置文件springmvc.xml
五、 在web.xml中經過初始化參數指定springmvc配置文件的位置web
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定springmvc配置文件的位置 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
六、建立一個自定義的控制器,在類上使用@Controller 註解和@RequestMapping註解,建立一個方法,在方法上使用@RequestMapping註解
spring
package cn.internet.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * 使用註解的方式開發自定義的控制器 * @author zhangbin * */ @Controller @RequestMapping(value="/user")//請求根路徑 public class UserController { /** * 自定義一個方法 */ @RequestMapping(value="/save.action")//請求子路徑 public String saveUser(){ System.out.println("saveUser............."); return "success";//success邏輯視圖名 } @RequestMapping(value="/delete.action") public String deleteById(){ return null; } }
七、能夠經過指定的請求根路徑和子路徑訪問控制器中的方法mvc