首页 > 代码库 > Spring MVC xml configuration

Spring MVC xml configuration

1. Web.xml

    id="WebApp_ID" version="3.0">    <display-name>springMvcDemo</display-name>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list>    <servlet>        <servlet-name>springMVC</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>/WEB-INF/config/spring-mvc.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></web-app>
View Code

 

2. spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd">    <bean id="navigationController" class="com.jnl.springMvcDemo.controller.NavigationController">    </bean>    <bean id="urlMapping"        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">        <property name="mappings">            <props>                <prop key="/navigation">navigationController</prop>            </props>        </property>    </bean>    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix">            <value>/views/</value>        </property>        <property name="suffix">            <value>.jsp</value>        </property>    </bean></beans>
View Code

 

3. NavigationController.java

package com.jnl.springMvcDemo.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;@Controllerpublic class NavigationController extends AbstractController {    @Override    protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {        ModelAndView mv = new ModelAndView();        mv.setViewName("result");        return mv;    }}
View Code

 

4. Add a jsp file with name "result" in folder "views"

 

5. visit http://localhost:8080/springMvcDemo/navigation