@Controller public class UserAction extends ActionSupport implements ModelDriven<User>{ private static final long serialVersionUID = 1L; User user=new User(); public User getModel() { return user; }我一般是这样写的。struts2会自动封装id,name,password属性到user中了。 这些都是基础。多多加油。
applicationContext.xml中没有配置其他bean了。你说把sid封装成一个对象这可以,但是我的数据都是页面提交上来的。spring也可以给某个对象注入属性值,这是可以的。我这儿全部是用的注解。
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="no"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="org.course"/>
<context:annotation-config/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="course" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
你这里<form action="/course/studentRegisterAction" method="post">
是不是有问题啊?改成studentRegisterAction试试
提交成功了的。并且Action进行了处理。可就是这个字段为null. 表示快疯了。
这句话不需要加
struts默认的就是spring
@Controller
这个都不用写的,在web.xml中配置好就行了
像这样:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<!-- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> -->
<init-param>
<param-name>config</param-name>
<param-value>
struts-default.xml,struts-plugin.xml,config/struts/struts.xml
</param-value>
</init-param>
</filter>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!-- classpath:config/spring/applicationContext*.xml -->
classpath:config/spring/applicationContext.xml
</param-value>
</context-param> <filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
写一个Stuedent类,作为实体bean;
在Action中声明一个Student属性。提供get,set方法,就能自动封装数据了。
public class UserAction extends ActionSupport implements ModelDriven<User>{
private static final long serialVersionUID = 1L;
User user=new User();
public User getModel() {
return user;
}我一般是这样写的。struts2会自动封装id,name,password属性到user中了。
这些都是基础。多多加油。
你这上面的配置不就是启动struts和spring吗?没见什么特别的啊。你还说事务都不加?什么啊没明白
试试这个
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>
<action name="studentRegisterAction" class="studentRegisterHandler">
<result>/index.jsp</result>
</action>
</package>