spring-13.web项目

1.实现步骤:

1.创建maven,web项目
2.加入依赖
3.创建一个jsp发起请求,有参数id,name,email,age
4.创建servlet,接收请求参数,调用service,调用dao完成注册
5.创建一个jsp作为显示结果页面

2.实现
  1. 添加依赖
 <!-- servlet依赖-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>

    <!-- jsp依赖-->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2.1-b03</version>
    </dependency>

2.创建一个jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <p>注册学生</p>
    <form action="reg" method="post">
        <table>
            <tr>
                <td>id</td>
                <td><input type="text" name="id"></td>
            </tr>

            <tr>
                <td>姓名:</td>
                <td><input type="text" name="name"></td>
            </tr>

            <tr>
                <td>email:</td>
                <td><input type="text" name="email"></td>
            </tr>

            <tr>
                <td>年龄</td>
                <td><input type="text" name="age"></td>
            </tr>

            <tr>
                <td></td>
                <td><input type="submit" name="注册学生"></td>
            </tr>
        </table>
    </form>
</body>
</html>

3.controller

public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strId=request.getParameter("id");
        String strname=request.getParameter("name");
        String strEmail=request.getParameter("email");
        String strAge=request.getParameter("age");


        //创建spring的容器对象
        String config="applicationContext.xml";
        ApplicationContext ctx=new ClassPathXmlApplicationContext(config);

        System.out.println("容器对象的信息------"+ctx);

        //获取service
        StudentService service= (StudentService) ctx.getBean("studentService");
        Student student=new Student();
        student.setId(Integer.parseInt(strId));
        student.setName(strname);
        student.setEmail(strEmail);
        student.setAge(Integer.valueOf(strAge));
        service.addStudent(student);

        request.getRequestDispatcher("/result.jsp").forward(request,response);

    }
}
3.加入监听器

1.在pom文件中加入依赖

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.5.RELEASE</version>
</dependency>

2.在web.xml文件中添加监听器

<!--    注册监听器ContextLoaderListener
        监听器被创建对象后,会读取/WEB-INF/applicationContext.xml
        为什么要读取文件:因为在监听器中要创建ApplicationContext对象,需要加载配置文件。
        可以修改默认的文件位置,使用context-param重新指定文件的位置

        配置监听器:目的是创建容器对象,创建了容器对象,就能把applicationContext.xml配置文件中的所有对象都创建好
        用户发起请求就可以使用对象了
-->
    
    <context-param>
<!--        contextConfigLocation:表示配置文件的路径-->
        <param-name>contextConfigLocation</param-name>
<!--        自定义配置文件的路径-->
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

<!--    监听器-->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3.controller类中的容器对象创建转变为:

//获取ServletContext中的容器对象,创建好的容器对象,拿来直接用
        WebApplicationContext ctx=null;//web项目中使用的容器对象
        String key=WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
        Object attr=getServletContext().getAttribute(key);
        if(attr!=null){
            ctx=(WebApplicationContext)attr;
        }

使用框架中的方法获取对象

ServletContext sc=getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值