我正在使用Spring MVC和Netbeans开发一个项目。我使用bootstrap作为我的CSS(以前有PHP经验)。当用户(管理员)键入URL" http://localhost:8035/HMS/adminportal.htm"时,他会收到管理员登录表单。我使用spring标签来处理表单。处理程序有一个GET方法,用于填充"指定"选择框和处理表单的POST方法。如果他成功登录,那么他将被带到" adminpanel.jsp"。这是问题的开始。在此页面上,他可以选择更新酒店详细信息,添加房间,添加员工和更新员工角色。所有这些都将使用bootstrap模态和spring form标签完成。每个表单都有不同的操作,所有这些操作都将由一个控制器处理(当然使用模型属性)。成功登录后,页面不会打开为"既不是绑定结果也不是....."因为页面在控制器之前加载,我认为会抛出异常。我想自从EmployeeLoginController返回" adminpanel"所以EmployeeController永远不会被触发,因此发生异常。 因此,经过大量研究后,我得出的结论是,我需要将控制权从EmployeeLoginController转移到EmployeeController。使用我编写的代码,TomCat找不到adminpanel.jsp(找不到请求的资源),我看到以下URL:" http://localhost:8035/HMS/adminpanel?message=Login+successful"。我尝试过使用ModelAndView但无济于事。有人可以帮帮我吗? 感谢
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/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-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<!--<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>-->
</web-app>
分配器一servlet.xml中
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<context:annotation-config />
<context:component-scan base-package="HMS" />
<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
AdminPortal.java
package HMS.Controller;
import HMS.DAO.Employee;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ModelAttribute;
@Controller
@RequestMapping({"/","/adminportal.htm"})
public class AdminPortal {
@RequestMapping(method = RequestMethod.GET)
public String redirectAdminPortal(ModelMap model) {
Employee adminForm = new Employee();
model.put("adminForm", adminForm);
List<String> roleList = new ArrayList<>();
roleList.add("Admin");
roleList.add("Manager");
roleList.add("Staff");
model.put("roleList", roleList);
return "adminportal";
}
}
adminportal.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Administration portal</title>
</head>
<body>
<h2 style="color:red">${message}</h2>
<form:form action="emplogin.htm" modelAttribute="adminForm" method="POST">
Name:<form:input path="ename" />
<br>
Password:<form:password path="epass" />
<br>
Designation:<form:select path="erole" items="${roleList}" />
<br>
<input type="submit" value="Submit">
<br>
</form:form>
</body>
</html>
AdminLoginController.java
package HMS.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import HMS.DAO.EmployeeDAO;
import HMS.DAO.Employee;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.ModelAttribute;
@Controller
@RequestMapping({"/","/emplogin.htm"})
public class AdminLoginController {
@RequestMapping(method = RequestMethod.GET)
public String viewRegistration(ModelMap model) {
Employee adminForm = new Employee();
model.put("adminForm", adminForm);
List<String> roleList = new ArrayList<>();
roleList.add("Admin");
roleList.add("Manager");
roleList.add("Staff");
model.put("roleList", roleList);
return "adminportal";
}
@RequestMapping(method = RequestMethod.POST)
public String processLogin(@ModelAttribute(value="adminForm") Employee employee,
ModelMap model) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("Beans.xml");
EmployeeDAO dao=(EmployeeDAO)ctx.getBean("employee");
Employee emp=new Employee();
emp.setEname(employee.getEname());
emp.setEpass(employee.getEpass());
emp.setErole(employee.getErole());
Employee status=dao.checkLogin(emp);
if(status.isValid())
{
model.put("message","Login successful");
return "redirect:/adminpanel";
}
else
{
model.put("message","Login failed");
return "redirect:/adminportal";
}
}
}
adminpanel.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<form:form id="frmAddEmp" class="form-horizontal" role="form" method="POST" action="addemployee.htm" modelAttribute="empForm">
</form:form>
EmployeeController.java
package HMS.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import HMS.DAO.EmployeeDAO;
import HMS.DAO.Employee;
import org.springframework.web.bind.annotation.ModelAttribute;
@Controller
@RequestMapping({"/","/addemployee.htm"})
public class EmployeeController {
@RequestMapping(method = RequestMethod.GET)
//@RequestMapping(value = "{'/','/addemployee.htm'}", method=RequestMethod.GET)
public String viewEmpDetails(ModelMap model) {
Employee empForm = new Employee();
model.put("empForm", empForm);
return "adminpanel";
}
@RequestMapping(method = RequestMethod.POST)
//@RequestMapping(value = "{'/','/addemployee.htm'}", method=RequestMethod.POST)
public String addEmployee(@ModelAttribute(value="empForm") Employee employee,ModelMap model) {
return "adminpanel";
}
}
答案 0 :(得分:0)
问题在于:
http://localhost:8035/HMS/adminpanel?的消息=登录+成功强>
message=Login+successful
网址/adminpanel
正在寻找@RequestParam(value = "message")
因此,如果您需要message
,只需在您的控制器方法中添加@RequestParam(value = "message")
,然后由您决定是否使用OR,
如果您在登录页面后不需要该消息,则可以通过替换它来使用RedirectView:
return "redirect:/adminpanel";
用这个:
RedirectView rw=new RedirectView(request.getContextPath()+"/adminpanel");
rw.setExposeModelAttributes(false);
return rw;
添加强>
@RequestMapping(method = RequestMethod.GET)
public String viewEmpDetails(@RequestParam(value = "message", required = false) String message,ModelMap model) {
Employee empForm = new Employee();
model.put("empForm", empForm);
return "adminpanel";
}
@RequestMapping(method = RequestMethod.POST)
public String addEmployee(@RequestParam(value = "message", required = false) String message,@ModelAttribute(value="empForm") Employee employee,ModelMap model) {
return "adminpanel";
}