一个简单的springmvc实例 helloword

1.导入相关jar包

2.springMvc.xml配置

<?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/beans  
       http://www.springframework.org/schema/beans/spring-beans.xsd  
       http://www.springframework.org/schema/context  
       http://www.springframework.org/schema/context/spring-context.xsd  
       http://www.springframework.org/schema/mvc  
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
  
  
    <!-- 扫描web包,应用Spring的注解 -->  
   
       
       <!-- 配置
       
        -->
       <context:component-scan base-package="com.springMvc.handler"></context:component-scan>
        <!-- 试图解析器 逻辑视图.物理视图 
         hello
       /hello.jsp
      -->
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/"></property>
       <property name="suffix" value=".jsp"></property>
       </bean>
       
       

       </beans>

3.web.xml配置springMvc.xml文件

<!--  
   初始化配置文件路径
  
  -->
  <servlet>
  <servlet-name>springDispatherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <!--路径  -->
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:springMvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
 <servlet-mapping>
 <servlet-name>springDispatherServlet</servlet-name>
 <!--映射方法 /代表所有  -->
 <url-pattern>/</url-pattern>

 </servlet-mapping>



  4.编写hello方法类 ,  hello方法中return "success"
       即跳转至success.jsp

package com.springMvc.handler;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;




@Controller
public class HelloWordHandler {
/**
 * 控制器处理请求方法格式
 * Public String 方法名称(){
 * }
 * string 当方法处理完成后,所返回逻辑视图名称
 * method为请求方法
 * params为返回值限制
 * 
 **/
@RequestMapping(value="/hello",method=RequestMethod.GET,params={"name=tom","age!=12"})
public String hello() {
System.out.println("hello 方法中-----");
return "success";
}

}

5.jsp

<body>
<a href="hello?name=tom&age=13">hello word</a>
</body>

猜你喜欢

转载自blog.csdn.net/qq_38474916/article/details/79966319