我有一个表单,当表单填写并提交时,我希望请求为http://localhost:8080/restroo/admin/adminLog
,但它会给http://localhost:808/adminLog
并收到404错误。我不知道为什么我遇到这个问题,实际上我在春天使用两个控制器时遇到了问题。
web.xml
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-
class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
我有spring-servlet.xml
admin.jsp
<form method="post" action="/adminLog" modelAttribute="adminUser">
First Name: <input type = "text" name = "userName">
<br />
password <input type = "password" name = "password" />
<input type = "submit" value = "Submit" />
</form>
AdminPageController.java
@Controller
@RequestMapping("/admin/*")
public class AdminPageController {
@Autowired
AdminUser adminUser;
@Autowired
MenuItems menuItems;
@Autowired
MenuItemsDao menuItemsDao;
@Autowired
AdminLoginDao adminLoginDao;
@RequestMapping(value="", method=RequestMethod.GET)
public ModelAndView addMenuItems(@ModelAttribute MenuItems menuItems){
// if(menuItems != null){
// menuItemsDao.addItems(menuItems);
// }
return new ModelAndView("admin");
}
@RequestMapping(value="/adminLog", method=RequestMethod.POST)
public ModelAndView adminLogin(@ModelAttribute("adminUser") AdminUser ad){
List<AdminUser> adminUser = adminLoginDao.adminLogin();
int len = adminUser.size();
for(int i=1;i<=len;i++){
String userN = adminUser.get(i).getUserName();
String pass = adminUser.get(i).getPassword();
if(userN.equals(ad.getUserName()) && (pass.equals(ad.getPassword()))){
return new ModelAndView("adminLogin");
}
}
return new ModelAndView("admin");
}
}
答案 0 :(得分:0)
您必须通过为整个应用程序的API添加前缀来更改servlet映射:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/restroo</url-pattern>
</servlet-mapping>
答案 1 :(得分:0)
您正在使用内部资源视图解析程序无法在Web-INF Floader中获取视图。
查找http://www.baeldung.com/spring-mvc-view-resolver-tutorial。