Java WEB SpringMvc框架简单入门

来源:互联网 发布:淘宝宝贝拍照灯光选择 编辑:程序博客网 时间:2024/04/29 14:47

1.Jar包准备(最小jar包集合)


  导入上述jar包

2.修改web.xml配置文件(WEB-INF下的web工程配置文件)

<?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>springmvc</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   <init-param>   <param-name>contextConfigLocation</param-name>   <param-value>classpath:springmvc-servlet.xml</param-value>   </init-param>  </servlet>   <servlet-mapping>      <servlet-name>springmvc</servlet-name>     <url-pattern>/</url-pattern>  </servlet-mapping> </web-app>
3.配置spring框架配置文件(放在src下)

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">   <!-- scan the package and the sub package -->  <context:component-scan base-package="top.mzp.test"/>   <!-- don't handle the static resource -->  <mvc:default-servlet-handler />  <!-- if you use annotation you must configure following setting --> <mvc:annotation-driven />   <!-- configure the InternalResourceViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean>  </beans>
上述配置文件: base-package="top.mzp.test" 配置处理类(后面有描述)
property name="prefix" value="/WEB-INF/" 代表的是视图文件的路径

3.新建处理类

package top.mzp.test; import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/mvc")public class mvcController {@RequestMapping("/hello")public String hello() {return "index";}}
其中:
("/mvc")是访问的路径,
"/hello"是访问的URL
return "index"; index是访问的视图文件的名字(index.jsp) 

4.不熟项目之后,访问:http://localhost:8080/springMvc02/mvc/hello



原创粉丝点击