码迷,mamicode.com
首页 > 其他好文 > 详细

SSH框架整合1

时间:2018-06-22 22:41:27      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:tor   遇到   enable   cto   text   span   BMI   basic   dcl   

 

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

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

    <!-- Struts核心过滤器 -->
    <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>
</web-app>





=============================spring.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--开启注解-->
    <context:annotation-config/>

    <!--告知spring要去哪个包找注解-->
    <context:component-scan base-package="com.ibeifeng.ssh"/>

    <aop:aspectj-autoproxy/>

    <bean name="myDataSource" 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/houserent"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--配置session信息-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <!--由于这里我们使用的是注解的方式得到属性,因此使用packagesToScan,
        若在hibernate中使用hibernate.cfg.xml则在此处就使用mappingResources,
        作用是指定从哪个包中查找注解的model-->
        <property name="packagesToScan">
            <value>com.ibeifeng.ssh.model</value>
        </property>
        <!--配置hibernate属性-->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
</beans>





======================struts.xml====================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.devMode" value="true"/>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <package name="default" extends="struts-default">

        <!--使用通配符-->
        <action name="*_*" class="{1}Controller" method="{2}">
            <result name="success">{2}.jsp</result>
        </action>
    </package>
</struts>




=========================Group.java(model层实体类)===============
package com.ssh.model;

import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;

@Entity
@Table(name="t_group")
public class Group {
    private int id;
    private String name;

    public Group(){}

    public Group(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Id
    @GenericGenerator(name = "increment",strategy = "increment")
    @GeneratedValue(generator = "increment")
    public int getId() {
        return id;
    }

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

    @Column(name="name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}



========================GroupDao.java(dao层实现类)===================
package com.ssh.dao;

import com.ssh.model.Group;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class GroupDao implements IGroupDao {
    private SessionFactory sessionFactory;

    @Resource
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void add(Group group) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(group);
        session.getTransaction().commit();
    }

    @Override
    public Group load() {
        return null;
    }
}



========================IGroupDao.java(dao层接口)====================
package com.ssh.dao;

import com.ssh.model.Group;

public interface IGroupDao {
    public void add(Group group);
    public Group load();
}




===================GroupService.java(service层实现类)===========
package com.ibeifeng.ssh.service;

import com.ssh.dao.IGroupDao;
import com.ssh.model.Group;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class GroupService implements IGroupService {
    private IGroupDao groupDao;

    @Resource
    public void setGroupDao(IGroupDao groupDao) {
        this.groupDao = groupDao;
    }

    @Override
    public void save(Group group) {
        groupDao.add(group);
    }

    @Override
    public Group load(int id) {
        return null;
    }
}




===================IGroupService.java(service层接口)===========
package com.ssh.service;

import com.ssh.model.Group;

public interface IGroupService {
    public void save(Group group);
    public Group load(int id);
}




===================IGroupController.java(Controller层实现类)===========
package com.ssh.controller;

import com.ssh.model.Group;
import com.ssh.service.IGroupService;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;

@Controller("groupController")
public class GroupController {
    private IGroupService groupService;
    private Group group;

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    @Resource
    public void setGroupService(IGroupService groupService) {
        this.groupService = groupService;
    }

    public String add(){
        groupService.save(group);
        return "success";
    }
}



==================================add.jsp(前端页面)===========================
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>add</title>
</head>
<body>
<form method="post" action="group_add">
组名:<input type="text" name="group.name">
<input type="submit" value="保存">
</form>
</body>
</html>





============================================遇到的问题=============================
Unable to instantiate Action, groupController,  defined for ‘group_add‘ in namespace ‘‘groupController - action - 

file:/E:/ssh_basic/out/artifacts/ssh_basic_war_exploded/WEB-INF/classes/struts.xml:8:63
	com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:317)
	com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:398)
	com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
	org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
	org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
	com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
	org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:565)
	org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter

(StrutsPrepareAndExecuteFilter.java:99)
root cause

java.lang.ClassNotFoundException: groupController
	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1928)
	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1771)
	com.opensymphony.xwork2.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:152)
	com.opensymphony.xwork2.ObjectFactory.getClassInstance(ObjectFactory.java:135)
	com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:246)
	com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:163)
	com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:178)
	com.opensymphony.xwork2.factory.DefaultActionFactory.buildAction(DefaultActionFactory.java:22)
	com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:148)
	com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:298)
	com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:398)
	com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
	org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
	org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
	com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
	org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:565)
	org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter

(StrutsPrepareAndExecuteFilter.java:99)

解决方法:
struts.xml配置文件使用通配符方式时,要先开启struts.enable.DynamicMethodInvocation

Unable to instantiate Action, groupController,  defined for ‘group_add‘ in namespace ‘‘groupController - action - 
file:/E:/ssh_basic/out/artifacts/ssh_basic_war_exploded/WEB-INF/classes/struts.xml:8:63com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:317)com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:398)com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:565)org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter
(StrutsPrepareAndExecuteFilter.java:99)root cause
java.lang.ClassNotFoundException: groupControllerorg.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1928)org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1771)com.opensymphony.xwork2.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:152)com.opensymphony.xwork2.ObjectFactory.getClassInstance(ObjectFactory.java:135)com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:246)com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:163)com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:178)com.opensymphony.xwork2.factory.DefaultActionFactory.buildAction(DefaultActionFactory.java:22)com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:148)com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:298)com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:398)com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:565)org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter
(StrutsPrepareAndExecuteFilter.java:99)

SSH框架整合1

标签:tor   遇到   enable   cto   text   span   BMI   basic   dcl   

原文地址:https://www.cnblogs.com/helloworldlx/p/9215531.html

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