标签:out org list() new tis pac let jar enc
SpringMVC的核心也是Servlet
ModelAndView的作用是保存数据,跳转页面
ResoluteView视图解析器
步骤解析:
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
在浏览器中访问的时候Controller第一个字母大写,其他全部小写,也可以省略掉Controller
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!-- <bean name="/my.do" class="com.alibaba.wlq.controller.Mycontroller"></bean> -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
<bean class="com.alibaba.wlq.controller.MyController1"></bean>
<bean class="com.alibaba.wlq.controller.MyController2"></bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value="/WEB-INF/view/"/>
<property name = "suffix" value=".jsp"/>
</bean>
</beans>
package com.alibaba.wlq.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class MyController1 extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
ModelAndView mv = new ModelAndView();
mv.setViewName("login");
mv.addObject("username","Hello World!");
System.out.println("111111111111111");
return mv;
}
}
package com.alibaba.wlq.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class MyController2 extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
ModelAndView mv = new ModelAndView();
mv.setViewName("login");
mv.addObject("username","Hello World!");
System.out.println("2222222222222222");
return mv;
}
}
<!--根据controller类的名字查找对应的Controller-->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
<!--注册自己的controller类-->
<bean class="com.alibaba.wlq.controller.Mycontroller"></bean>
? 3. SimpleUrlHandlerMapping:根据id查询相应的Controller类
<!--这里包含了id与url地址的映射关系-->
<bean class="org.springframework.web.servlet.handle.SimpleUrlHandlerMapping">
<property name = "mappings">
<props>
<prop key="/a.do">my1</prop>
<prop key="/b.do">my2</prop>
</props>
</property>
</bean>
<!--注册自己的controller类-->
<bean id = "my1" class = "com.alibaba.wlq.controller.MyController1"></bean>
<bean id = "my2" class = "com.alibaba.wlq.controller.MyController2"></bean>
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--1.包扫描:扫描注解所在的包(controller类所在的包)-->
<context:component-scan base-package="com.alibaba.wlq.controller">
</context:component-scan>
<!-- 2.开启注解驱动-->
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value="/WEB-INF/view/"/>
<property name = "suffix" value=".jsp"/>
</bean>
</beans>
controller类中:
package com.alibaba.wlq.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController3 {
@RequestMapping("/list.do")
public String list() {
System.out.println("查询所有");
return "login";
}
}
标签:out org list() new tis pac let jar enc
原文地址:https://www.cnblogs.com/wuliqqq/p/11449469.html