私は春に取り組んでおり、単一の例で立ち往生しています.1つのエラーを解決すると、別のエラーが発生します...
次のように UserController2.java という名前のコントローラープログラムがあります
package project2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
@Controller
public class UserController2 extends MultiActionController {
private UserDAO userDAO;
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
@RequestMapping(params = "add", method = RequestMethod.POST)
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response, User user) throws Exception {
userDAO.saveUser(user);
return new ModelAndView("redirect:list.htm");
}
@RequestMapping(params = "delete", method = RequestMethod.POST)
@Transactional
public ModelAndView delete(HttpServletRequest request,
HttpServletResponse response, User user) throws Exception {
userDAO.deleteUser(user);
return new ModelAndView("redirect:list.htm");
}
public ModelAndView list(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelMap modelMap = new ModelMap();
modelMap.addAttribute("userList", userDAO.listUser());
modelMap.addAttribute("user", new User());
return new ModelAndView("userForm", modelMap);
}
}
jsp ページの userForm.jsp は次のようになります。
<body>
<center> WELCOME TO CUSTOMER ACCESS SITE.PLEASE ENTER THE FOLLOWING
INFORMATION</center>
<form:form method="POST" action="add.htm" modelAttribute="user">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="id">id</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td><form:label path="password">password</form:label></td>
<td><form:input path="password" /></td>
</tr>
<tr>
<td><form:label path="gender">Gender</form:label></td>
<td><form:input path="gender" /></td>
</tr>
<tr>
<td><form:label path="country">Country</form:label></td>
<td><form:input path="country" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
</body>
および UserController2-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema
/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema
/tx/spring-tx-2.5.xsd">
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-
method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/indi"/>
<property name="username" value="admin"/>
<property name="password" value=""/>
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>Spring.project2.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="myUserDAO" class="project2.UserDAOImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean name="/user/*.htm" class="project2.UserController2" >
<property name="userDAO" ref="myUserDAO" />
</bean>
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="userForm" />
</beans>
web.xml ファイルは次のとおりです。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>UserController2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>UserController2</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
今、次のエラーが発生しています
org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither
BindingResult nor plain target object for bean name 'user' available as request
attribute
私はjspとコントローラークラスの両方で「ユーザー」のみを使用しました。次のコード行を変更するように言われた人もいました
modelMap.addAttribute("user", new User());
これに
modelMap.addAttribute("user", new user());
しかし、私はエラーが発生しています
user cannot be resolved to a type
ネットを徹底的に検索し、さまざまな例を調べましたが、コードを解決できませんでした。何か提案はありますか??