这篇文章是我学习的网络视频SpringMVC写的过程。
谢谢公布各位前辈的视频
以下评论SpringMVC几个关键步骤,注意事项启用:
首先需要加载配置文件(假设请使用自定义路径)
<?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>springMVC1</display-name> <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> <!-- springMVC 入口 dispatcher --> <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*:config/springAnnotation-servlet.xml</param-value> </init-param> <!-- 何时启动 大于0的值表示容器启动时初始化此servlet,正值越小优先级越高--> <load-on-startup>1</load-on-startup> </servlet> <!-- 拦截 --> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
其次,配置文件。最基本的是开启注解和Spring启动时载入扫描包
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- spring 启动时扫描包 --> <context:component-scan base-package="com.tgb.web.controller.annotation"> </context:component-scan> <!-- 开启注解 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> <!-- 路径相应调用的Controller --> <bean name="/test/helloworld" class="com.tgb.web.controller.HelloWorldController"/> <!-- 视图解析 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 静态资源訪问(不拦截此文件夹下的东西的訪问) --> <mvc:resources location="/img/" mapping="/img/**" /> </beans>
再次,书写Controller的java代码
package com.tgb.web.controller.annotation; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class UserController { @RequestMapping(value="/user/addUser",method=RequestMethod.POST) public ModelAndView addUser(){ String result = "addUser"; return new ModelAndView("/annotation","result",result); } @RequestMapping(value="/user/delUser",method=RequestMethod.GET) public ModelAndView delUser(){ String result = "delUser"; return new ModelAndView("/annotation","result",result); } @RequestMapping(value="/user/toUser",method=RequestMethod.GET) public ModelAndView toUser(){ String result = "toUser"; return new ModelAndView("/touser","result",result); } }
此处请注意,@Controller的使用。
@RequestMapping(value="/user/addUser",method=RequestMethod.POST)中的value表示跳转路径,method表示通过哪种方式调用这种方法
最后是前台的jsp界面
annotation.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h1>SpringMVC注解</h1> <br> ${result } </body> </html>
touser.jsp界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="/springMVC4/user/addUser" method="post"> <h1>SpringMVC注解</h1> <br> ${result } <input type="submit" value="post请求"> </form> </body> </html>