spring+servlet+jsp|软件架构技术实验一

本文详细介绍了基于Spring的软件架构技术实验,涵盖了com包中的类结构,包括domain层实体类、dao和服务层的注解注入。同时解析了`applicationContext.xml`的组件扫描和bean的注入方式,以及`web.xml`中定义的监听器和上下文位置。此外,还提及了jsp页面如index.jsp的基本元素和HTTP请求处理。实验中存在未使用接口和Junit测试的不足,提供了一些代码生成快捷键的小技巧。

文件结构

简单描述实验一中web工程的文件结构。
文件结构
com文件夹中放置各层类的代码,详细如下:
各层的类
本次实验使用的lib包括:
在这里插入图片描述
web页面包括:
web页面
主要讲解以下四个部分:

  1. com包中各类
  2. src/applicationContext.xml
  3. WEB-INF/web.xml
  4. jsp页面

1.com 中各类

domain层中是实体类,实体类包含一些属性及其setter、getter和String方法。
userDao使用Repository注解注入,userService使用ServIce注解注入。
SpringContextListener代码:

package com.servlet;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class SpringContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
//        定义配置信息
//        配置完web。xml再进行值的填写
        String config = context.getInitParameter("contextLocation");
//        获取application context对象
        ApplicationContext app = new ClassPathXmlApplicationContext(config);

        context.setAttribute("ApplicationContext",app);

    }
}

ServletTest3代码:

package com.servlet;

import com.domain.User;
import com.service.UserService;
import org.springframework.context.ApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/ServletTest3")
public class ServletTest3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = getServletContext();
        ApplicationContext app = (ApplicationContext) context.getAttribute("ApplicationContext");
        UserService userService = app.getBean("userService", UserService.class);
        User user = app.getBean("user",User.class);


        user.setName(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));

        if(userService.login(user)==1){
//            out.println("login successfully...");
//            登录成功 重定向到success.jsp
            response.sendRedirect("/spring/success.jsp");

        }
        else{
//            out.println("login fail...");
//            登录失败 转发到error.jsp
            request.getRequestDispatcher("error.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

2.src/applicationContext.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"
       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">
       
    <context:component-scan base-package="com"></context:component-scan>

    <bean id="user" class="com.domain.User">
            <property name="name" value="default_name"/>
            <property name="password" value="default_password"></property>
    </bean>
</beans>

compotent-scan用于注解注入。
user的是xml注入中的set方法注入。
该xml

3.WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <listener>
        <listener-class>com.servlet.SpringContextListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
</web-app>
  1. 定义了listener的位置
  2. 定义了listener中contextlocation的位置

4.jsp页面

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>Spring Ioc实验</title>
  </head>
  <body>
<div align="center">
  <form action="ServletTest3" method="POST">
   用户名称:<input type="text" name="username">
    <br>
    密码:<input type="text" name="password" />
    <br>
    <input type="checkbox" name="auto_name"  /> 自动记录名称
    <br>
    <input type="checkbox" name="auto_psd"  /> 自动记录密码
    <br>
    <input type="submit" value="登录" /> <input type="submit" value="重置" />
  </form>
</div>
  </body>
</html>

html 居中

div align = "center"

method=POST处理POST的HTTP请求;br换行。

其他

不足:
没有使用接口
没有使用Junit测试

小技巧:
Alt+Insert 生成setter、getter和String方法。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值