Spring MVC一些知识

  1. 首先,我们使用spring mvc需要导入的jar包
    需要导入的jar包信息
  2. 在wen.xml需要配置的信息
    web.xml需要的配置信息

从上往下一次介绍配置文件的信息


  1. controller层(servlet层)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- Controller方法调用规则定义 -->
    <beanid="paraMethodResolver"class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="paramName" value="action"/>
        <property name="defaultMethodName" value="list"/>
    </bean>
   <!-- 页面View层基本信息设定 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <!-- 完善具体Controller里面的返回信息,如:return index -->
        <!-- 增加前缀/myjsp/index -->
        <!--<property name="prefix" value="/myjsp/"/>-->
        <!-- 增加后缀:index.jsp -->
        <property name="suffix" value=".jsp"/>
    </bean>
<!-- servlet映射列表,所有控制层Controller的servlet在这里定义 -->
    <bean id="urlMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <!-- 如果jsp页面中有user.do的action,就交给userController处理:相当于一个servlet处理user.do的请求 
                    userController参照web.config.xml里面配置bean的id名-->
                <prop key="user.do">userController</prop>
            </props>
        </property>
    </bean>

<!-- 配置userController具体的类名信息 -->
<bean id="userController" class="com.sxt.action.UserController">
    <!-- 实现注入:将userService注入到UserController里面,
    这里的ref参照service-config.xml里面的bean的id 
    这里的name要与userCotroller里面的属性名对应-->
    <property name="userService" ref="userService"></property>
</bean>
</beans>
相应userController的写法

public class UserController implements Controller {

//接受web-config.xml里面注入的参数
private UserService userService;

//实现Controller里面的方法
public ModelAndView handleRequest(HttpServletRequest req,
        HttpServletResponse resp) throws Exception {
    System.out.println("HelloController.handleRequest()");
    req.setAttribute("a", "aaaa");
    userService.add(req.getParameter("uname")); 
    //更加web-config.xml里面的配置信息,可以知道跳转到WebRoot/index.jsp页面
    return new ModelAndView("index");
}

public UserService getUserService() {
    return userService;
}

public void setUserService(UserService userService) {
    this.userService = userService;
}

}

  1. service层:
    配置文件信息
    这里写图片描述
    具体类的信息:
    这里写图片描述

  2. dao层
    配置文件信息这里写图片描述
    代码段信息

最后jsp页面的信息

 <body>
    <form action=user.do>
        用户名:<input type=text name=uname /><br/>
        <input type=hidden name=method value="reg"/>
        <input type=submit value= 注册   />
    </form>     
  </body>

这是用spring mvc实现的一个小功能!能够观察整个框架的结构

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值