SSH搭建

  1、首先在lib中导入ssh需要的jar包:

如图:

SSH搭建

2、其次配置web.xml (在WEB-INF目录下)

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

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

xmlns="http://java.sun.com/xml/ns/j2ee"

 xmlns:web="http://java.sun.com/xml/ns/javaee" 

 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_9" version="2.4">

  <!-- struts2配置 -->

  <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> /* </url-pattern>

  </filter-mapping>

  

  

  <!-- Spring 配置  Spring应用程序上下文监听器-->

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/classes/applicationContext.xml</param-value>

  </context-param>

  

  <!--首页面设置  -->

  <welcome-file-list>

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

  </welcome-file-list>

</web-app>

 3、配置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:p="http://www.springframework.org/schema/p"

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



    xsi:schemaLocation="http://www.springframework.org/schema/beans 

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

    

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

    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 

    

    ">

<!-- 启用spring注解支持 

    <context:annotation-config />-->

 



    <!--配数据源 -->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"

        destroy-method="close">

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

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

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

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

    </bean>



   <!--实例化sessionFactory  -->

   <bean id="sessionFactory"

        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <property name="dataSource" ref="dataSource"></property>



        <property name="mappingResources">

            <list>

                <value>bean/User.hbm.xml</value>            

                <value>bean/CalendarEvent.hbm.xml</value>              

            </list>

        </property>



    </bean>

    

<bean id="dbhelper" class="util.DBHelper">

    <constructor-arg ref="sessionFactory"></constructor-arg>

</bean>



<bean id="userdao" class="dao.UserDao" parent="dbhelper"></bean>

<bean id="calendardao" class="dao.CalendarEventDao" parent="dbhelper"></bean>

    

  <!--Spring 实例化action  -->

    <bean id="loginaction" class="action.LoginAction" scope="prototype" ></bean>

    <bean id="registeraction" class="action.RegisterAction" scope="prototype">

      <property name="dao" ref="userdao"></property>

    </bean>

     <bean id="calendaraction" class="action.CalendarEventAction" scope="prototype">

      <property name="dao" ref="calendardao"></property>

    </bean>

</beans>

    

    

4.配置struts.xml

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

<!-- 应用Spring管理Struts action  -->

<constant name="struts.objectFactory" value="spring"></constant>
<constant name="struts.devMode" value="true" /> <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 --> <constant name="struts.configuration.xml.reload" value="true" />
<package name="default" extends="struts-default"> <action name="registerAction" class="registeraction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> </action> <action name="LoginAction" class="loginaction"> <result name="success">/show/index.jsp</result> <result name="input">/login.jsp</result> </action> <action name="fileUpload" class="action.FileUploadAction"> <result name="success">/FileUpload.jsp</result> </action> </package>

<!-- ajax请求的action url是calendarAction_方法名 --> <package name="datalist" extends="struts-default,json-default"> <action name="calendarAction_*" class="calendaraction" method="{1}"> <!-- class的值 是 bean的id --> <result type="json"> <param name="root">list</param> <!-- list 通过set,get方法返回 --> </result> </action> </package> </struts>

5、这两个配置文件都放在src目录下

如图:

SSH搭建

6、CalendarEventAction.java

public class CalendarEventAction extends ActionSupport {



    /**

     * 

     */

    private static final long serialVersionUID = 1L;

    private CalendarEvent event;

    private CalendarEventDao dao;

    private List<CalendarEvent> list;



    public List<CalendarEvent> getList() {  

        return list;

    }



    public void setList(List<CalendarEvent> list) {

        this.list = list;

    }



    /*public CalendarEventDao getDao() {

        return dao;

    }

*/

    public void setDao(CalendarEventDao dao) {

        this.dao = dao;

    }



    public CalendarEvent getEvent() {

        return event;

    }



    public void setEvent(CalendarEvent event) {

        this.event = event;

    }



    public String insertCalendar(){

        HttpServletRequest request=    ServletActionContext.getRequest();

        HttpServletResponse response=ServletActionContext.getResponse();

        

            try {

                request.setCharacterEncoding("utf-8");

            } catch (UnsupportedEncodingException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            response.setContentType("text/json; charset=utf-8");

            boolean flag=dao.insetCalendarEvent(event);

            try {

                PrintWriter out = response.getWriter();

                out.print(flag);

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            

        

        return null;

    }

    

    public String getCalendar(){

        

        list=dao.getCalendarEvent();    

        

        return "success";

    }

    

    



}

 

你可能感兴趣的:(ssh)