Spring mvc 表单提交 - 使用新的附加属性加载相同的 jsp(Spring MVC 注释)
Spring mvc Form Submit - To load the same jsp with new additional attributes (Spring MVC annotations)
我的 jsp 页面中有几个 select 下拉菜单。一旦用户从下拉列表中选择值并单击 Go,相同的 jsp 应该重新加载下面的另一个 table(以及基于下拉列表中 selected 选项的详细信息起伏)。我的 jsp 如下:
<form:form method="post" modelAttribute="clientForm" commandName = "clientForm" action="/editFeeMaintenance">
<table>
<tbody>
<tr>
<td><strong><bean:message key="Select Client" /> </strong></td>
<td width="8%" height="6"> </td>
<td align="left"><form:select path="client">
<form:option value="0">------Select Client ------ </form:option>
<form:options items="${clientsList}">
</form:options>
</form:select></td>
<td><form:errors path="client" cssStyle="color: #ff0000;" /></td>
</tr><br>
<tr>
<td>
 <input type="submit" value="Search" onclick="getClient()">
</td>
</tr>
</tbody>
</table>
</form:form>
<script language = "JavaScript">
function getClient()
{
var clientObj = document.getElementById("client");
console.log("client value : "+clientObj);
clientObj.actionType.value = submitType;
return false;
//formObj.submit();
}
</script>
这是我的控制器:
@Controller
public class MyController {
@Autowired
private MyService myService;
@RequestMapping(value="/addFeeMaintainance.do",method = RequestMethod.GET)
protected ModelAndView populateValues(HttpServletRequest request, HttpServletResponse response) throws Exception {
MyForm = new MyForm();
MyForm.setClientList(MyService.getClientList();
ModelAndView model = new ModelAndView("feeMaintenance");
model.addObject("clientForm",MyForm);
model.addObject("clientsList",Myform.getClientList());
return model;
}
@RequestMapping(value="/editFeeMaintainance",method = RequestMethod.POST)
protected ModelAndView selectValues(@RequestParam String client,
@ModelAttribute("clientForm") MyForm paswForm ) throws Exception {
System.out.println("Account selected : "+paswForm.getClient());
ModelAndView model = new ModelAndView("feeMaintenance");
model.addObject("clientForm",MyForm);
return model;
}
}
我无法刷新表单并转到 POST 方法。让我知道缺少什么。
web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
调度程序servlet文件如下:
<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.controller,com.serviceImpl,com.daoImpl" />
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"
id="viewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
id="tilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/plugin/impl/tiles/springtiles-defs.xml</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/myDS" />
</bean>
<bean
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
id="sessionFactory">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation">
<value>/WEB-INF/config/hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<!-- <prop key="current_session_context_class">thread</prop> -->
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect </prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
您的代码从客户端和服务器端看起来都不错(我没有检查您是否正确映射了您的表单对象,如果您的控制器具有 RequestMapping 值,您还没有发布),但唯一的问题是表单的操作,其中您没有在操作中应用指定的 URL ,因此每当您单击该按钮时,它只是调用 JS 函数而不提交表单。所以如果你能改变
<form:form method="post" modelAttribute="clientForm" commandName = "clientForm" action="">
到
<form:form method="post" modelAttribute="clientForm" commandName = "clientForm" action="YOUR_URL">
并查看相关答案here
或查看本教程here,其中只要表单在同一页面中提交,他就会重定向
编辑:
在您的 web.xml 中,您配置了一个名为 Dispatcher 的 servlet,并在下面将请求映射应用到:
<servlet-mapping>
<servlet-name>paswDispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
如果您有 2 个 servlet,那么您缺少一些配置,否则如果这是唯一的 servlet,请将其更改为
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
我的 jsp 页面中有几个 select 下拉菜单。一旦用户从下拉列表中选择值并单击 Go,相同的 jsp 应该重新加载下面的另一个 table(以及基于下拉列表中 selected 选项的详细信息起伏)。我的 jsp 如下:
<form:form method="post" modelAttribute="clientForm" commandName = "clientForm" action="/editFeeMaintenance">
<table>
<tbody>
<tr>
<td><strong><bean:message key="Select Client" /> </strong></td>
<td width="8%" height="6"> </td>
<td align="left"><form:select path="client">
<form:option value="0">------Select Client ------ </form:option>
<form:options items="${clientsList}">
</form:options>
</form:select></td>
<td><form:errors path="client" cssStyle="color: #ff0000;" /></td>
</tr><br>
<tr>
<td>
 <input type="submit" value="Search" onclick="getClient()">
</td>
</tr>
</tbody>
</table>
</form:form>
<script language = "JavaScript">
function getClient()
{
var clientObj = document.getElementById("client");
console.log("client value : "+clientObj);
clientObj.actionType.value = submitType;
return false;
//formObj.submit();
}
</script>
这是我的控制器:
@Controller
public class MyController {
@Autowired
private MyService myService;
@RequestMapping(value="/addFeeMaintainance.do",method = RequestMethod.GET)
protected ModelAndView populateValues(HttpServletRequest request, HttpServletResponse response) throws Exception {
MyForm = new MyForm();
MyForm.setClientList(MyService.getClientList();
ModelAndView model = new ModelAndView("feeMaintenance");
model.addObject("clientForm",MyForm);
model.addObject("clientsList",Myform.getClientList());
return model;
}
@RequestMapping(value="/editFeeMaintainance",method = RequestMethod.POST)
protected ModelAndView selectValues(@RequestParam String client,
@ModelAttribute("clientForm") MyForm paswForm ) throws Exception {
System.out.println("Account selected : "+paswForm.getClient());
ModelAndView model = new ModelAndView("feeMaintenance");
model.addObject("clientForm",MyForm);
return model;
}
}
我无法刷新表单并转到 POST 方法。让我知道缺少什么。 web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
调度程序servlet文件如下:
<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.controller,com.serviceImpl,com.daoImpl" />
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"
id="viewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
id="tilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/plugin/impl/tiles/springtiles-defs.xml</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/myDS" />
</bean>
<bean
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
id="sessionFactory">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation">
<value>/WEB-INF/config/hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<!-- <prop key="current_session_context_class">thread</prop> -->
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect </prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
您的代码从客户端和服务器端看起来都不错(我没有检查您是否正确映射了您的表单对象,如果您的控制器具有 RequestMapping 值,您还没有发布),但唯一的问题是表单的操作,其中您没有在操作中应用指定的 URL ,因此每当您单击该按钮时,它只是调用 JS 函数而不提交表单。所以如果你能改变
<form:form method="post" modelAttribute="clientForm" commandName = "clientForm" action="">
到
<form:form method="post" modelAttribute="clientForm" commandName = "clientForm" action="YOUR_URL">
并查看相关答案here
或查看本教程here,其中只要表单在同一页面中提交,他就会重定向
编辑:
在您的 web.xml 中,您配置了一个名为 Dispatcher 的 servlet,并在下面将请求映射应用到:
<servlet-mapping>
<servlet-name>paswDispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
如果您有 2 个 servlet,那么您缺少一些配置,否则如果这是唯一的 servlet,请将其更改为
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>