需要节点:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!--beans 自动将依赖的core引入了--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.3.RELEASE</version> </dependency> <!--context 依赖N个--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--织入的依赖--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!--Servlet API--> <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.10.RELEASE</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
web.xml配置
<web-app> <display-name>Archetype Created Web Application</display-name> <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:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--对静态资源访问方式二--> <!--<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping>--> </web-app>
FirstController 类
public class FirstController implements Controller { public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mv=new ModelAndView(); mv.addObject("uname","小仙女"); mv.setViewName("/index"); return mv; } } springMVC.xml
<bean id="test01" class="cn.ljl.controller.FirstController"></bean> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> <!--对静态资源访问方式一--> <!-- <mvc:default-servlet-handler/>--> <!--对静态资源访问方式三--> <mvc:resources location="/img/" mapping="/img/**"></mvc:resources> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!----> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <!--<property name="mappings"> <props> <prop key="/hello">test01</prop> <prop key="/ljl">test01</prop> </props> </property>--> <property name="urlMap"> <map> <entry key="/hello" value="test01"></entry> <entry key="/haha" value="test01"></entry> </map> </property> </bean>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <body> <h2>我还是很喜欢你 像风走了八千里 不问归期</h2> <h3>${uname}</h3> <img src="img/01.jpg"/> </body> </html>
在换另一个配置时,在web.xml中更改
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC02hander.xml</param-value> </init-param>
在类中更改:
public class SecondController extends AbstractController{ protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mv=new ModelAndView(); mv.addObject("uname","你是不是傻"); mv.setViewName("/index"); return mv; } }