码迷,mamicode.com
首页 > 编程语言 > 详细

spring整合web

时间:2018-12-19 13:02:42      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:servlet   entity   pass   esrs   org   override   throw   string   asp   

目录

Spring整合Web

导包

<dependencies>
    <!-- spring-ioc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- spring-aop -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>${cglib.version}</version>
    </dependency>

    <!-- Spring整合Web-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- Java EE-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
</dependencies>

tomcat 插件

<build>
    <finalName>spring-web</finalName>
    <plugins>

        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <configuration>
                <port>8888</port>
                <path>/</path>
            </configuration>
        </plugin>
    </plugins>
</build>

dao

@Repository
public class userDaoImpl implements userDao {
    @Override
    public User selectByUsername(String username) {

        User user=new User();
        user.setUsername("toms");
        user.setPassword("123");
        user.setId(1001);
        return user;
    }
}

public interface userDao {

    public User selectByUsername(String username);

}

service



@Service("uesrService")
public class userServiceImpl implements userService {

    @Autowired
    private  userDao userDao;
    @Override
    public User loign(String username, String password) {

        User user =userDao.selectByUsername(username);
        if (user.getPassword().equals(password))
        {
            return user;
        }
        return null;
    }
}
public interface userService {


    public User loign(String username, String password);

}

entity

public class User implements Serializable {

    private Integer id;

    private String username;

    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

servlet


@WebServlet("/login")
public class userServlet extends HttpServlet {

    private userService userService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String username = req.getParameter("username");
        String password = req.getParameter("password");
        userService= (com.itany.service.userService)SpringBeanHoder.getBean("uesrService");
        User loign = userService.loign(username, password);
        if(loign!=null)
        {
            req.setAttribute("username",username);
            req.getRequestDispatcher("WEB-INF/view/success.jsp").forward(req,resp);
        }
        else
        {
            req.setAttribute("username",username);
            req.getRequestDispatcher("WEB-INF/view/fail.jsp").forward(req,resp);
        }

    }
}

SpringBeanHoder

public  class SpringBeanHoder implements ApplicationContextAware {
    public static ApplicationContext ac;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("ApplicationContext.setApplicationContext");
        ac=applicationContext;
        System.out.println("ApplicationContext.endsetApplicationContext");

    }

    public static Object getBean(String bean)
    {
        return  ac.getBean(bean);
    }

    public  static <T> T getBean(Class<T> classz)
    {
        return  ac.getBean(classz);
    }


}

jsp






spring.xml


<context:component-scan base-package="com.itany.dao.impl"/>
<context:component-scan base-package="com.itany.service.impl"/>

<bean class="com.itany.until.SpringBeanHoder"/>

web.xml


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

spring整合web

标签:servlet   entity   pass   esrs   org   override   throw   string   asp   

原文地址:https://www.cnblogs.com/lilihai/p/10142067.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!