SpringMVC入门


首先你要Spring所有的包导入

1、配置前端控制器DispatcherServlet 在web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>



<servlet>
<servlet-name>spring</servlet-name><!--src下[servletname]-servlet.xml中的servletname一一致-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>


<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/classes/spring-servlet.xml</param-value><!--默认DispatcherServlet会加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件->
</init-param>



<load-on-startup>1</load-on-startup><!--表示启动容器室初始化该servlet-->
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern><!--表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。-->
</servlet-mapping>


</web-app>

2、建立一个HelloWorldController

package com.iss.control;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;


public class HelloWorldController implements Controller {


public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {


ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", "helloworld");
modelAndView.setViewName("hello");//设置视图名
return modelAndView;//返回模型数据和逻辑视图名
}


}

3、配置spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Bean头部 -->  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  


<!-- HandlerMapping -->  
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>  


<!-- HandlerAdapter -->  
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>


<!-- ViewResolver -->  
<bean 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" class="com.iss.control.HelloWorldController"/> 
</beans>

<!--

InternalResourceViewResolver:用于支持Servlet、JSP视图解析;

 viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,classpath中必须包含jstl的相关jar包;

 prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),比如传进来的逻辑视图名为hello,则该该jsp视图页面应该存放在“WEB-INF/jsp/hello.jsp”;

-->

4、在WEB-INF下建立jsp文件夹并新建一个hello.jsp文件注意hello已经在HelloWorldContoller中设置不可以随意命名

  <body>
${message } 
  </body>

5、在浏览器地址栏输入

http://localhost:8080/SpringMVC01/hello  页面出现helloworld!证明你成功了!


SpringMVC入门,布布扣,bubuko.com

SpringMVC入门

上一篇:MySQL数据库5 - 插入数据,修改数据,删除数据


下一篇:【JAVA】for,foreach遍历数组时候一些区别