这篇文章是我学习的网络视频SpringMVC写的过程。
谢谢公布各位前辈的视频
以下评论SpringMVC几个关键步骤,注意事项启用:
首先需要加载配置文件(假设请使用自定义路径)
其次,配置文件。最基本的是开启注解和Spring启动时载入扫描包
> <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 > <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
touser.jsp界面