学习spring mvc之路一:简单的Hello word

 new -》Dynamic web project 创建一个名为 testspringmvc-1的项目;


web.xml代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
     <!-- 配置DispatchcerServlet -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <!-- 配置DispatcherServlet 的一个参数配置Spring mvc下的配置文件的位置和名称  init-param用于配置spring mvc的配置文件的位置和名称,
          这里说明会新建一个springmvc.xml的配置文件 -->
          <!-- 我们也可以不新建springmvc.xml,而是用默认的,默认的配置文件格式为/WEB-INF/[servlet-name]-servlet.xml,
          对应这里的就是springDispatcherServlet-servlet.xml -->
         <init-param>
           <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/springmvc.xml</param-value>
       </init-param>
        <load-on-startup>1</load-on-startup>
   </servlet>
    <!--  这里的servlet-mapping表示拦截的模式,这里是“/”,表示对于所有的请求的拦截,包括静态资源如html, js, jpg等。
    这时候对于静态资源的访问就会报404的错误。-->
   <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
   </servlet-mapping>
     
</web-app>


根据           <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/springmvc.xml</param-value>

在这目录下创建名为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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


<context:component-scan base-package="com.jikexueyuan.testspringmvc"></context:component-scan>
<!-- 视图解析器,根据返回的字符组合成一个新写地址去调用它,得到真正的文件 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" ></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>


在包中创建HelloWord.java代码如下:

@Controller
public class HelloWord {
/*
1.使用@RequestMapping注解来映射请求的url
2.返回值会通过视图解析器解析为实际的物理视图,
通过(springmvc.xml 中的)prefix + returnVal + 后缀 这样的方式得到实际的物理视图,然后做转发操作
例:/WEB-INF/views/success.jsp
*/

@RequestMapping(value= "/helloword")
public String inputCustomer(){
System.out.println("成功调用了此方法!");
return "success";
}
}


然后在/WEB-INF/views下创建success.jsp文件,打印hello word


index.jsp代码如下:

<body>
<a href=helloword>Hello World</a>

</body>


调用:http://localhost:8080/testspringmvc-1/index.jsp点击链接使他调用inputCustomer()方法,返回success,让视图解析器组合成/WEB-INF/views/success.jsp ,服务就会根据这个地址去调用success.jsp文件


另外:@RequestMapping(value= "/helloword")的注解,如果是单一的属性,也可以写成@RequestMapping( "/helloword")


@RequestMapping除了value属性以外,还有其他属性。如method属性来指示改方法仅处理那些Http方法:

@RequestMapping(value= "/helloword",method={RequestMethod.POST,RequestMethod.GET})

也可以把他放到类前面,这样它就变成一个控制器类,调用就要先经过它,


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值