spring整合web

Posted 小小程序员

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring整合web相关的知识,希望对你有一定的参考价值。

目录

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的主要内容,如果未能解决你的问题,请参考以下文章

spring和struts整合

spring框架学习:Spring整合Web项目整合Mybatis

007-shiro与spring web项目整合基础搭建

spring整合web

Spring:概述,IOC(Bean管理),整合Web项目,整合JUnit单元测试

Spring与Struts整合