Struts2 +春天 + JPA(Hibernate):动作映射问题
我正在尝试进行此集成: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该问题有一个非常简单的解决方案:导出 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.
在
元素中,class
属性必须是完全限定的类名it.vigorelli.action.PersonAction
。In your
<action>
element, theclass
attribute must be a fully qualified class nameit.vigorelli.action.PersonAction
.