Spring MVC学习总结一

  1. Place the following <servlet> declaration in your application’s web.xml file:
    <servlet>
    <servlet-name>training</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>By default, when Dispatcher-
    Servlet is loaded, it will load the Spring application context from an XML file
    whose name is based on the name of the servlet. In this case, because the servlet is
    named training, DispatcherServlet will try to load the application context from
    a file named training-servlet.xml.
  2. Next you must indicate what URLs will be handled by the DispatcherServlet.
    Add the following <servlet-mapping> to web.xml to let DispatcherServlet handle
    all URLs that end in “.htm”:
    <servlet-mapping>
    <servlet-name>training</servlet-name>
    <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
  3. you’ll be deploying to a web container that supports the
    Servlet 2.3 specification (or higher) and initializes servlet listeners before servlets.
    If that’s the case, you’ll want to configure ContextLoaderListener in your
    web.xml file as follows:
    <listener>
    <listener-class>org.springframework.
    web.context.ContextLoaderListener</listener-class>
    </listener>
  4. You can specify one or more Spring configuration files for the context loader
    to load by setting the contextConfigLocation parameter in the servlet context:
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/training-service.xml,
    /WEB-INF/training-data.xml</param-value>
    </context-param>
  5. The default handler mapping used by DispatcherServlet is BeanNameUrlHandler-
    Mapping, which uses the base name as the URL pattern.The following chunk of XML declares the
    HomeController:
    <bean name="/home.htm"
    class="com.springinaction.training.mvc.HomeController">
    <property name="greeting">
    <value>Welcome to Spring Training!</value>
    </property>
    </bean>
  6. InternalResourceViewResolver:
    <bean id="viewResolver" class="org.springframework.web.
    servlet.view.InternalResourceViewResolver">
    <property name="prefix">
    <value>/WEB-INF/jsp/</value>
    </property>
    <property name="suffix">
    <value>.jsp</value>
    </property>
    </bean>
  7. SimpleUrlHandlerMapping:                                                                                                              <bean id="simpleUrlMapping" class=
    "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
    <props>
    <prop key="/listCourses.htm">listCoursesController</prop>
    <prop key="/admin/register.htm">registerStudentController</prop>
    <prop key="/admin/displayCourse.htm">displayCourseController</prop>
    <prop key="/login.htm">loginController</prop>
    <prop key="/enroll.htm">enrollController</prop>
    </props>
    </property>
    </bean>
  8. To use CommonsPathMapHandlerMapping, simply declare it as a <bean> in your
    context configuration file as follows:

    <bean id="urlMapping" class="org.springframework.web.
    ➥ servlet.handler.metadata.CommonsPathMapHandlerMapping"/>                                                     to map DisplayCourseController to “/displayCourse.htm”, tag DisplayCourseController as follows:
    /**
    * @@org.springframework.web.servlet.handler.
    commonsattributes.PathMap("/displayCourse.htm")
    */
    public class DisplayCourseController
    extends AbstractCommandController {

    }See chapter
    5, section 5.5.2 for details on how to add the attributes compiler to your build.
  9. ■ BeanNameUrlHandlerMapping—Maps controllers to URLs that are based on
    the controllers’ bean name
    ■ SimpleUrlHandlerMapping—Maps controllers to URLs using a property collection
    defined in the context configuration file

    ■ CommonsPathMapHandlerMapping—Maps controllers to URLs using sourcelevel
    metadata placed in the controller code

  10. Working with multiple handler mappings  ,You may also have to declare it explicitly if you are using multiple handler mappings and need to specify ordering                                                                <bean id="beanNameUrlMapping" class="org.springframework.web.
    servlet.handler.BeanNameUrlHandlerMapping">
    <property name="order"><value>1</value></property>
    </bean>
    <bean id="simpleUrlMapping" class="org.springframework.web.
    servlet.handler.SimpleUrlHandlerMapping">
    <property name="order"><value>0</value></property>
    <property name="mappings">

    </property>
    </bean>

  11.  public class DisplayCourseController
    extends AbstractCommandController {
    public DisplayCourseController() {
    setCommandClass(DisplayCourseCommand.class);
    }
    protected ModelAndView handle(HttpServletRequest request,
    HttpServletResponse response, Object command,
    BindException errors) throws Exception {
    DisplayCourseCommand displayCommand =
    (DisplayCourseCommand) command;
    Course course = courseService.getCourse(displayCommand.getId());
    return new ModelAndView("courseDetail", "course", course);
    }
    private CourseService courseService;
    public void setCourseService(CourseService courseService) {
    this.courseService = courseService;
    }
    }

  12. public class RegisterStudentController
    extends SimpleFormController {
    public RegisterStudentController() {
    setCommandClass(Student.class);
    }
    protected void doSubmitAction(Object command)
    throws Exception {
    Student student = (Student) command;
    studentService.enrollStudent(student);
    }
    private StudentService studentService;
    public void setStudentService(StudentService studentService) {
    this.studentService = studentService;
    }
    }                                                                                                                                                           <bean id="registerStudentController" class="com.springinaction.
    training.mvc.RegisterStudentController">
    <property name="studentService">
    <ref bean="studentService"/>
    </property>
    <property name="formView">
    <value>newStudentForm</value>
    </property>
    <property name="successView">
    <value>studentWelcome</value>
    </property>
    </bean>Just as with the other controllers, the registerStudentController bean is wired
    with any services that it may need (e.g., studentService). But here you also specify
    a formView property and a successView property. The formView property is the
    logical name of a view to display when the controller receives an HTTP GET
    request or when any errors are encountered. Likewise, the successView is the logical
    name of a view to display when the form has been submitted successfully. A
    view resolver (see section 8.4) will use these values to locate the View object that
    will render the output to the user.for you, depending on whether you need to display model data on the success view.
    If you need to send data to be displayed by the view, you should override the
    onSubmit() method instead of doSubmitAction(). For example, suppose that
    after enrolling the new student you want to send the user to a page where the
    student’s information is displayed. You’ll need to send the Student object to
    the view. To do this, replace the doSubmitAction() from listing 8.4 with the following
    onSubmit() method:                                                                                                                           protected ModelAndView onSubmit(Object command,
    BindException errors) throws Exception {
    Student student = (Student) command;
    studentService.enrollStudent(student);
    return new ModelAndView(getSuccessView(),"student", student);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值