开发者学堂课程【 SpringMVC 框架入门:springmvc hello】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/22/detail/451
Springmvc hello
内容介绍:
1. 导入相关jar包
2. 配置web.xml分发器
3. 添加spring MVC配置文件
4. 编写HelloController.Java
5. 编写spring mvc配置文件
6. 测试
l 导入相关jar包
commons-logging-1.1.1.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-context-support-4.1.6.RELEASE.jar spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar spring-web-4.1.6.RELEASE.jar
l 配置web.xml分发器<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.Dispat
cherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
l 添加spring MVC配置文件
添加spring mvc配置文件,默认在WEB-INF下面添加【Dispatcher Servlet Name】-servlet.xml文件
l 编写HelloController.Java
public class HelloController implements
Controller{
public ModelAndView
handleRequest(HttpServletRequest reg,
HttpServletResponse resp) throws Exception {
ModelAndView mv = new ModelAndView();
//封装要显示到视图中的数据
mv.addObject("msg","hello springmvc");
//视图名
mv.setViewName("hello");
return mv;
}
}
l 编写spring mvc配置文件
<!--配置
handlerMapping -->
<bean
class="org.springframework.web.servlet.handler.BeanNameUrLHan dlerMapping"/>
<!
--
配置
handlerAdapter -->
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHa ndlerAdapter"/>
<!--配置渲染器
-->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"/>
<!--结果视图的前缀
-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--结果视图的后缀
-->
<property name="suffix" value=".jsp"/>
</bean>
<!--配置请求和处理器
-->
<bean name="/hello,do"
class="cn.sxt.controller.HelloController"/>
l 测试
http:// localhost:8080/ hello/ hello.do
</div>