**解析来自handler [com.controller.EmpController@77cb027f]的异常:org.springframework.beans.TypeMismatchException:无法转换类型' java.lang.String'的值。要求的类型' int&#39 ;;嵌套异常是java.lang.NumberFormatException:对于输入字符串:" save" **
这是控制器部分
@Controller
public class EmpController {
private static final Logger logger = Logger
.getLogger(EmpController.class);
public EmpController() {
System.out.println("EmpController()");
}
@Autowired
private EmpService service;
@Autowired
private UserService userservice;
public UserService getUserservice() {
return userservice;
}
public void setUserservice(UserService userservice) {
this.userservice = userservice;
}
public EmpService getService() {
return service;
}
public void setService(EmpService service) {
this.service = service;
}
@RequestMapping(value="login", method= RequestMethod.GET)
public ModelAndView showlogin(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav= new ModelAndView("login");
mav.addObject("login", new User() );
return mav;
}
@RequestMapping(value="loginProcess", method= RequestMethod.POST)
public ModelAndView loginProcess(HttpServletRequest request, HttpServletResponse response,
Model model,@ModelAttribute("login") User user) {
ModelAndView mav = null;
boolean user1 = userservice.validateUser(user);
if(false != user1) {
List<Emp> list = service.getAllEmployees();
model.addAttribute("list", list);
mav= new ModelAndView("empform","command",new Emp());
mav.addObject("userId", user.getUserId());
} else {
mav= new ModelAndView("login");
mav.addObject("message","UserId and Password is wrong");
}
return mav;
}
@RequestMapping(value="/empform", method= RequestMethod.GET)
public ModelAndView showform(Model model,@ModelAttribute("emp") Emp emp) throws IOException {
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
List<Emp> list = service.getAllEmployees();
model.addAttribute("list", list);
return new ModelAndView("empform","command",new Emp());
}
/* @RequestMapping(value="/newEmp", method= RequestMethod.GET)
public ModelAndView newContact(ModelAndView model) {
Emp emp = new Emp();
model.addObject("emp", emp);
model.setViewName("empform");
return model;
} */
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveEmployee(@ModelAttribute Emp emp,Model model) {
System.out.println(">>>>welcome to save>>>>>");
if (emp.getEmpId() == 0) {
service.save(emp);
} else {
service.update(emp);
}
List<Emp> list = service.getAllEmployees();
model.addAttribute("list", list);
return new ModelAndView("redirect:/empform");
}
@RequestMapping(value="/delete/{empId}", method= RequestMethod.GET)
public ModelAndView delete(Model model,@PathVariable int empId) {
service.delete(empId);
List<Emp> list = service.getAllEmployees();
model.addAttribute("list", list);
return new ModelAndView("redirect:/empform");
}
@RequestMapping(value="/edit/{empId}")
public ModelAndView edit(Model model,@PathVariable int empId) {
System.out.println(">>>>>>>>>>>edit");
Emp emp= service.getEmpById(empId);
System.out.println(">>>>>>>>> continue edit>>>>");
return new ModelAndView("empform","command", emp);
}
}
这是spring-srvlet.xml
<context:component-scan base-package="com.controller">
</context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://10.125.129.252:3306/nehaempdb">
</property>
<property name="username" value="root"></property>
<property name="password" value="admin"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop
key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="com.model"></property>
</bean>
<bean id="service" class="com.service.EmpService">
</bean>
<bean id="userservice" class="com.service.UserService">
</bean>
<bean id="dao" class="com.dao.EmpDao">
</bean>
<bean id="userdao" class="com.dao.UserDao">
</bean>
<!-- Transaction -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
这是.jsp
<div align="center">
<h1>New/Edit Employee</h1>
<form:form action="save" method="post" >
<table>
<form:hidden path="empId"/>
<tr>
<td>Name:</td>
<td><form:input path="empName" /></td>
</tr>
<tr>
<td>Salary:</td>
<td><form:input path="salary" /></td>
</tr>
<tr>
<td>DeptId:</td>
<td><form:input path="deptId" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Save"></td>
</tr>
</table>
</form:form>
</div>
value="Save"></td>
</tr>
</table>
</form:form>
</div>
答案 0 :(得分:0)
听起来像你可能有一个@RequestMapping到一个类似“/ {id}”的资源,当你试图点击你的“/ save”资源而不是把它映射到你在你的控制器方法中显示的时候问题是它试图将“save”一词解析为资源“/ {id}”的整数id。
要解决此问题,您应该将之前的资源映射从“/ {id}”重命名为具有合适前缀“/ something / {id}”的内容。
答案 1 :(得分:0)
我遇到了同样的问题,Plog对于拥有数字路线图是正确的。
但是,不要改变&#34; / {id}&#34;到&#34; / something / {id}&#34;,您可以更改它&#34; / {id:\\ d +}&#34;然后它不会匹配&#34; / save&#34;。
我将数字正则表达式用于所有&#34; / id&#34;因此我不会遇到这个问题。例如&#34; / users / 25&#34;和&#34; / users / forgotpassword&#34;。
答案 2 :(得分:0)
我遇到了同样的问题,但对我来说解决方案很简单:
在以下代码中,当我使用 /
/ admin / products
中的第一个 history.push ('/ admin / products');
我的代码:
useEffect(() => {
if (isEditing) {
makeRequest({ url: `/products/${productId}` })
.then(response => {
setValue('name', response.data.name);
setValue('price', response.data.price);
setValue('description', response.data.description);
setValue('imgUrl', response.data.imgUrl);
})
}
}, [productId, isEditing, setValue]);
const onSubmit = (data: FormState) => {
makePrivateRequest({
url: isEditing ? `/products/${productId}` : '/products',
method: isEditing ? 'PUT' : 'POST',
data
})
.then(() => {
toast.info('Produto salvo com sucesso!');
history.push('/admin/products');
})
.catch(() => {
toast.error('Erro ao salvar produto!');
})
}