SSH框架整合1

 

======================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.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.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.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)html

相關文章
相關標籤/搜索