spring+struts 使用注解配置(简单的登陆模块)

  1. 首先配xml,并且导入jar包.(因为就是开始,所以只导入核心的包).
<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <context-param>

        <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>

    <filter>

        <filter-name>struts2</filter-name>

        <filter-class>

            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

    </filter>

    <filter-mapping>

        <filter-name>struts2</filter-name>

        <url-pattern>*.action</url-pattern>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

    <welcome-file-list>

        <welcome-file>Public/login.jsp</welcome-file>

    </welcome-file-list>

</web-app>

2第二部开始为工程建包,为了省事就不都弄了,不然应该有一个page包和symbol包,service也应该先建接口。

image

3配置applicationContext.xml和struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xsi:schemaLocation="

    http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 

    http://www.springframework.org/schema/context 

    http://www.springframework.org/schema/context/spring-context-3.0.xsd

    http://www.springframework.org/schema/aop 

    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

    http://www.springframework.org/schema/task   

    http://www.springframework.org/schema/task/spring-task-3.0.xsd   

    ">

    <!-- Spring 获取配置文件中定义的bean     -->

    <context:annotation-config />

    <!--

        引入属性文件 <context:property-placeholder

        location="classpath:config.properties" />

    -->

    <aop:aspectj-autoproxy proxy-target-class="true" />

    <!-- 自动扫描-->

    <context:component-scan base-package="com.sky.*" />



    <!-- 数据源 -->

    <!-- mysql -->

    <!--

        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

        destroy-method="close"> <property name="driverClassName"

        value="com.mysql.jdbc.Driver" /> <property name="url"

        value="jdbc:mysql://localhost:3306/studentmanagement" /> <property

        name="username" value="root" /> <property name="password" value="8023"

        /> <property name="initialSize" value="10" /> <property

        name="maxActive" value="10000" /> <property name="maxIdle" value="10"

        /> <property name="minIdle" value="10" /> </bean>

    -->

    <!-- oracle -->



    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

        destroy-method="close">

        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>



        <property name="url" value="jdbc:mysql://localhost/school"></property>



        <property name="username" value="root"></property>



        <property name="password" value="hjy"></property>

        <!--最大连接数-->

        <property name="maxActive" value="10"></property>



        <!-- 空闲时最大连接数 ,无论何时都有两条连接-->

        <property name="maxIdle" value="2"></property>



        <!-- 最长等待时间 -->

        <property name="maxWait" value="5000"></property>



    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

        <constructor-arg ref="dataSource" />

    </bean>

</beans>
<?xml version="1.0" encoding="UTF-8"?>



<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->

    <constant name="struts.i18n.encoding" value="UTF-8" />



    <!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。

        如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->

    <constant name="struts.action.extension" value=",do,action" />



    <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->

    <constant name="struts.serve.static.browserCache" value="false" />



    <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->

    <constant name="struts.configuration.xml.reload" value="true" />



    <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->

    <constant name="struts.devMode" value="true" />



    <!-- 默认的视图主题 -->

    <constant name="struts.ui.theme" value="simple" />



    <!--解决乱码    -->

    <constant name="struts.i18n.encoding" value="UTF-8" />



    <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) -->

    <constant name="struts.multipart.maxSize" value="10701096" />



    <!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir -->

    <constant name="struts.multipart.saveDir " value="d:/tmp" />



    <package name="default" namespace="/" extends="struts-default">

        <!-- Action -->

        <action name="loginAction"

            class="com.sky.action.LoginAction">

            <result name="success">index.jsp</result>

            <result name="fail">fail.jsp</result>

        </action>

    </package>

</struts>

4为自己已经写好的程序添加注解

<1>首先是dao的实现类

import java.sql.ResultSet;

import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowMapper;

import org.springframework.stereotype.Component;

import org.springframework.stereotype.Repository;



import com.sky.bean.User;

import com.sky.dao.LoginDao;



/*

 * dao层主要做数据库的增删改查

 * 对数据库的操作通常都在dao中完成

 */

@Repository

public class Loginimpl implements LoginDao {

    @Autowired

    private JdbcTemplate jdbcTemplate;



    User user2 = null;



    public boolean findByLoginService(User user) {

        int count = this.jdbcTemplate

                .queryForInt("select count(*) from user where name='"

                        + user.getName() + "'");

        if (count <= 0) {

            return false;

        }

        // 判断用户是否存在

        System.out.println(user.getName() + "  " + user.getPass());

        user2 = (User) this.jdbcTemplate.queryForObject(

                "select * from user where name='" + user.getName() + "'",

                new UserResult());

        System.out.println(user2.getName() + "  " + user2.getPass());

        // 登录名存在的话,判断密码是否正确

        if (user.getName().equals(user2.getName())

                && user.getPass().equals(user2.getPass())) {

            System.out.println("ok");

            System.out.println(user.getName() + "  " + user.getPass());

            return true;

        } else {

            return false;

        }

    }



    /**

     * 

     * @ClassName: UserResult

     * @Description: TODO(为user服务的RowMapper)

     * @author A18ccms a18ccms_gmail_com

     * @date 2014-11-19 下午09:05:09

     * 

     */

    public class UserResult implements RowMapper<User> {

        public User mapRow(ResultSet rs, int id) throws SQLException {

            User groupPageBean = new User();

            User user = new User();

            user.setName(rs.getString("name"));

            user.setPass(rs.getString("pass"));

            return user;

        }



    }

}

<2>然后是service的实现类package com.sky.service;

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.sky.bean.User; import com.sky.dao.LoginDao; /** * * @ClassName: LoginService * @Description: TODO(我是这样理解的,使用dao的方法实现服务的类) * @author A18ccms a18ccms_gmail_com * @date 2014-11-20 下午02:43:14 * */ @Service public class LoginService { @Autowired public LoginDao loginimpl ; 

public boolean dovalidate(User user)
{
System.out.println(
loginimpl.findByLoginService(user)+"----"); 
return
loginimpl.findByLoginService(user); } 
}

<3>最后是action类

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;

import com.sky.bean.User;

import com.sky.service.LoginService;



@Controller

public class LoginAction implements ModelDriven<User> {

    private User user = new User();

    @Autowired

    public LoginService loginService;



    public String dologin() throws IOException {

        boolean flag = loginService.dovalidate(user);

        if (flag) {

            return "success";

        } else {

            return "fail";

        }

    }



    public User getModel() {

        // TODO Auto-generated method stub

        return user;

    }



}

5再来就是前台页面了。

<!DOCTYPE HTML >

<html>

    <head>

        <title>登陆界面a</title>

        <script type="text/javascript" src="js/jquery-1.3.1.js"></script>

        <script type="text/javascript">

        </script>

    </head>    



    <body>

        <form Method="POST" id="form1" action="loginAction!dologin.action">

            username:

            <input type="text" name="name" id="name">

            <br>

            <br>

            password:

            <input type="password" name="pass">

            <br>

            <br>

            <input type="submit" value="ok" />

        </form>

    </body>

</html>

你可能感兴趣的:(spring)