springmvc 第一课

    springmvc4.0demo入门

 1、先从导入jar包开始

    只要运行一个pringmvc   hello world要导入如下jar包


2、jar导入好了之后建一个springmvc的配置文件  springmvc-servlet.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-4.0.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="cn.ynbd.controller"/>
    <mvc:annotation-driven />
     
<!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

3、配置文件建好之后配置web.xml文件

  首先配置dispatcherServlet

  <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>
      <!-- <load-on-startup>1</load-on-startup> -->
</servlet>
 
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的.
默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml


4、接下来就到写controller了

import java.io.UnsupportedEncodingException;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


@Controller
@RequestMapping("/mvc")
public class TestController {


@RequestMapping("/hello")
    public String hello() throws UnsupportedEncodingException{
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
System.err.println("hello world");
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
session.setAttribute("message1", "request测试");
        return "test";
    }
@RequestMapping("/hh")
public String hh() throws UnsupportedEncodingException{
    HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        session.setAttribute("message", 1);
return "hh";
}

@RequestMapping("/model")
public String medels(Model model,@RequestParam(value="id",required=false,defaultValue="0") String id){
        model.addAttribute("models", "model测试");
        try {
id = new String(id.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();

        model.addAttribute("ids", id);
System.out.println("RequestParam:"+id);
return "model";
}

}

5、编写jsp页面

这里一共涉及到了三个jsp页面  hello.jsp  hh.jsp  model.jsp

三个页面建好了之后可以开始访问了

访问地址为 http://localhost:8080/springmvc/mvc/hello  http://localhost:8080/springmvc/mvc/hh    http://localhost:8080/springmvc/mvc/model


springmvc demo 大功告成



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值