login.jsp 页面报错
org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="http://www.springframework.org/tags"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form:form action="multiaction.do" method="get" commandName="command">
<input type="hidden" value="select" name="method"/>
<s:bind path="command.name">
用户名:<input type="text" name="${status.expression}" value=""><br/>
</s:bind>
<%-- 密 码:<form:input path="pwd"/><br/>--%>
<input type="submit" value="提交" ><br/>
</form:form>
<%--<form action="multiaction.do">
<input type="hidden" value="select" name="method"/>
用户名:<input type="text" name="name" value=""><br/>
密 码:<input type="text" name="pwd" value=""><br/>
<input type="submit" value="提交" ><br/>
</form>--%>
</body>
</html>
controller
package org.springmvcone.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springmvcone.entity.User;
/**
* 多个方法控制器
* @author Z.Qiong
*
*/
public class MutialController extends MultiActionController{
private String successview;
private String failview;
public String getSuccessview() {
return successview;
}
public void setSuccessview(String successview) {
this.successview = successview;
}
public String getFailview() {
return failview;
}
public void setFailview(String failview) {
this.failview = failview;
}
public MutialController() {
}
public ModelAndView insert(HttpServletRequest request,HttpServletResponse response,User command){
ModelAndView modelandview=new ModelAndView();
System.out.println("insert-------------"+command.getName());
if(!"".equals(command.getName())&&!"".equals(command.getPwd())){
modelandview.addObject("sucess", "插入成功");
modelandview.setViewName(getSuccessview());
}else{
modelandview.setViewName(getFailview());
}
return modelandview;
}
public ModelAndView select(HttpServletRequest request,HttpServletResponse response,User command){
ModelAndView modelandview=new ModelAndView();
System.out.println("select-------------"+command.getName());
modelandview.setViewName(getFailview());
if(!"".equals(command.getName())&&!"".equals(command.getPwd())){
modelandview.addObject("sucess", "登陆成功");
HttpSession session=request.getSession();
session.setAttribute("user",command);
modelandview.setViewName(getSuccessview());
}else{
modelandview.setViewName(getFailview());
}
return modelandview;
}
// @Override
// public void bind(HttpServletRequest request, Object command)
// throws Exception {
// // TODO Auto-generated method stub
// User user=(User)command;
// super.bind(request, command);
// }
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<description><![CDATA[请求servlet配置]]></description>
<servlet-name>dispatcherservlet</servlet-name>
<!-- 记载请求接受类-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<!-- 配置servlet加载顺序 值越小加载就越靠前默认是0 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--配置dispatcherservlet适用范围 -->
<servlet-mapping>
<servlet-name>dispatcherservlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
application.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 控制器类 -->
<bean id="hellocontroller" class="org.springmvcone.controller.HelloController">
<property name="successview" value="success"></property>
<property name="failview" value="index"></property>
</bean>
<bean id="mutialcontroller" class="org.springmvcone.controller.MutialController">
<!--数据验证类 -->
<property name="validators" ref="uservalidator"></property>
<!-- 多方法解析器引用 -->
<property name="methodNameResolver" ref="parameterMethodNameResolver"></property>
<property name="successview" value="success"></property>
<property name="failview" value="login"></property>
</bean>
<!-- 配置试图解析类 -->
<bean id="viewresolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 支持jstl标签的试图 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<!-- 配置试图前缀 -->
<property name="prefix" value="/"></property>
<!-- 配置试图后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="parameterMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<!-- 方法的参数 -->
<property name="methodParamNames" value="insert,select,update"></property>
<property name="paramName" value="method"></property>
</bean>
<!--请求分发类 -->
<bean id="controllerResolver" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!-- 拦截器
<property name="interceptors" ref="logininterceporter">
</property>-->
<property name="mappings">
<props>
<prop key="/login.do">hellocontroller</prop>
<prop key="/multiaction.do">mutialcontroller</prop>
</props>
</property>
</bean>
<!-- 拦截器 -->
<bean id="logininterceporter" class="com.interceporter.LoginInterceporter">
<property name="backview" value="/index.jsp"></property>
<property name="pathmathes" value="/springmvcone/multiaction.do\?method=select"></property>
</bean>
<!-- 验证bean -->
<bean id="uservalidator" class="org.springmvcone.validator.UserValidator"></bean>
</beans>
validate验证类
package org.springmvcone.validator;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springmvcone.entity.User;
public class UserValidator implements Validator{
public boolean supports(Class<?> clazz) {
// TODO Auto-generated method stub
return clazz.equals(User.class);
}
public void validate(Object target, Errors errors){
User user=(User)target;
if("".equals(user.getName())){
//键入用户名错误信息
errors.rejectValue("name","error","用户名不能为空");
}
if("".equals(user.getPwd())){
//键入密码错误信息
errors.rejectValue("name","error","密码不能为空");
}
}
}
说什么没有command bean 难道配置实体bean ,还是要绑定对象
相关推荐
在实际应用中,你可以创建一个继承自`SimpleFormController`的类,并覆盖这些方法以实现特定的业务逻辑。 其次,`MultiActionController`则是另一种控制器,它允许在一个控制器类中定义多个处理方法,每个方法对应...
3. **创建控制器**:在`src`目录下的Java源代码包中,我们可以创建一个名为`MultiActionController`的类,继承自Spring MVC的`MultiActionController`。然后,为每个功能(查询、保存、上传文件)添加对应的方法,...
为了确保用户输入数据的有效性,Spring提供了Validator接口来实现自定义的数据验证逻辑。 #### 二十五、Controller组件继承架构 Spring MVC支持通过继承来扩展Controller的功能。 #### 二十六、...
与Struts不同,Spring的控制器可以不需要继承特定的基础类,而是直接处理请求。此外,Spring还引入了命令对象(Command Object)的概念,这些对象可以绑定HTTP表单数据,无需实现特定接口,增强了代码的可复用性。 ...
- **定义**:对Controller类进行单元测试,验证其逻辑是否正确。 - **工具**:常用工具包括JUnit、Mockito等。 - **示例**:编写测试用例,模拟HTTP请求并验证Controller的响应结果。 #### SimpleFormController类 ...
- **强大的配置能力**:可以将框架类和应用程序类作为 JavaBean 进行配置。 - **高度的可定制性**:可以根据需求选择不同的控制器子类。 - **可重用的业务代码**:可以直接使用现有业务对象,无需继承特定框架的基类...
- **数据校验**:Spring MVC支持对绑定的数据进行校验,确保数据的有效性。 **4.5 Web页面显示** - **页面布局**:通过使用Spring MVC的视图解析器,可以方便地定义和管理页面布局。 - **使用Tiles做页面布局**:...
认识 Spring 来... AbstractController MultiActionController 与 ParameterMethodNameResolver MultiActionController 与 PropertiesMethodNameResolver ParameterizableViewController ...