Struts2 + Spring + JPA(Hibernate):动作映射问题

时间:2011-03-24 11:46:09

标签: java spring jpa struts2 struts-config

我正在尝试进行这种集成:Struts2 + Spring + JPA(Hibernate)。 这个示例遇到了一个常见的struts异常,例如,我自己无法解决。 在提交时我有: HTTP状态404 - 没有映射名称空间/和操作名称保存的动作。

感谢任何可以告诉我错在哪里的人。 这是我正在使用的代码:

的persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="punit">

</persistence-unit>

struts.xml中

         

<package name="person" extends="struts-default">

    <action name="list" method="execute" class="personAction">
        <result>pages/list.jsp</result>
        <result name="input">pages/list.jsp</result>
    </action>

    <action name="save" class="personAction" method="save">
        <result>pages/list.jsp</result>
        <result name="input">pages/list.jsp</result>
    </action>

</package>

的applicationContext.xml

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="personService" class="it.vigorelli.service.PersonServiceImpl" />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL" />
            <property name="showSql" value="true" />
        </bean>
    </property>
</bean>

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

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="personAction" scope="prototype" class="it.vigorelli.action.PersonAction">
    <constructor-arg ref="personService" />
</bean>

的web.xml

    <filter>
    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    <filter-class>
        org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.FilterDispatcher
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

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

的index.jsp

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>

<body>
    <s:form action="save" validate="true">
        <s:textfield name="firstName" required="true" label="First Name"></s:textfield>
        <s:textfield name="lastName" required="true" label="Last Name"></s:textfield>
        <s:submit />

    </s:form>
</body>

PersonAction.java

package it.vigorelli.action;

import java.util.List;

import it.vigorelli.model.Person;
import it.vigorelli.service.PersonService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

public class PersonAction implements Preparable {

private PersonService service;
private List<Person> persons;
private Person person;
private Integer id;

public PersonAction(PersonService service) {
    this.service = service;
}

public String execute() {
    this.persons = service.findAll();
    return Action.SUCCESS;
}

public String save() {
    this.service.save(person);
    this.person = new Person();
    return execute();
}

public String remove() {
    service.remove(id);
    return execute();
}

public List<Person> getPersons() {
    return persons;
}

public Integer getId() {
    return id;
}

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

public void prepare() throws Exception {
    if (id != null)
        person = service.find(id);
}

public Person getPerson() {
    return person;
}

public void setPerson(Person person) {
    this.person = person;
}
}

2 个答案:

答案 0 :(得分:2)

问题有一个非常简单的解决方案:在WEB-INF / classes文件夹中导出struts.xml文件。现在Spring可以识别Struts动作。

答案 1 :(得分:-1)

<action>元素中,class属性必须是完全限定的类名it.vigorelli.action.PersonAction