SpringMVC4 注解配置实例结构:
maven配置:
4.0.0 java-springmvc-demo6 java-springmvc-demo6 war 1.0-SNAPSHOT java-springmvc-demo6 Maven Webapp http://maven.apache.org junit junit 3.8.1 test org.slf4j slf4j-log4j12 1.7.21 javax.servlet javax.servlet-api 3.1.0 javax.servlet.jsp jsp-api 2.2 javax.servlet jstl 1.2 org.springframework spring-web 4.3.1.RELEASE org.springframework spring-webmvc 4.3.1.RELEASE org.apache.commons commons-lang3 3.4 commons-fileupload commons-fileupload 1.3.1 maven-springmvc ${basedir}/src/main/java **/*.properties **/*.xml ${basedir}/src/main/resources org.eclipse.jetty jetty-maven-plugin 9.3.10.v20160621 org.apache.maven.plugins maven-compiler-plugin 1.6 1.6
web.xml:
java-springmvc-demo5 SpringMVC org.springframework.web.servlet.DispatcherServlet <!-- 默认的servlet.xml文件是放在WEB-INF目录下,名称与的名字要相关。但可以通过以下设置重新自定义servlet.xml配置文件的位置和名称。--> <!----> <!--contextConfigLocation--> <!--/WEB-INF/SpringMVC-servlet.xml--> <!----> 1 SpringMVC / index.jsp
SpringMVC-servlet.xml:
HelloController.java:
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller//该注解用来识别控制器 @RequestMapping("hello")//该注解用来控制url书写时,控制器的选择 public class HelloController{ @RequestMapping("helloworld")//该注解用来控制url书写时,具体方法的选择(还可以绑定参数等) public String helloworld() { return "hello_world";//跳转到jsp页面 } }
之后通过“http://localhost:8080/hello/helloworld”可访问到hello_world.jsp页面
:http://www.linuxidc.com/Linux/2017-01/139642.htm