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

发布于 2024-10-26 15:11:28 字数 4923 浏览 4 评论 0原文

我正在尝试进行此集成: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;
}
}

I'm trying do this integration: Struts2 + Spring + JPA (Hibernate).
This sample run into a common struts exception that for instance, I'm not able to solve by myself.
On submit i have: HTTP Status 404 - There is no Action mapped for namespace / and action name save.

Thanks to anyone that can show me where I'm wrong.
Here's the code I'm using:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花桑 2024-11-02 15:11:37

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

The problem has a very easy solution: exporting struts.xml file in WEB-INF/classes folder. Now Spring can recognize Struts actions.

似狗非友 2024-11-02 15:11:37

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

In your <action> element, the class attribute must be a fully qualified class name it.vigorelli.action.PersonAction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文