- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
案例1:我收到以下错误:
org.apache.jasper.JasperException: An exception occurred processing JSP page /login.jsp at line 14
11: <body>
12:
13:
14: <form:form method="get" commandName="user" action="login">
15:
16: <form:label path="Username" /><form:input path="uname"/>
17: <form:label path="Password" /><form:input path="password"/>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:424)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:90)
org.springframework.web.servlet.support.RequestContextUtils.getWebApplicationContext(RequestContextUtils.java:85)
org.springframework.web.servlet.support.RequestContext.initContext(RequestContext.java:209)
org.springframework.web.servlet.support.JspAwareRequestContext.initContext(JspAwareRequestContext.java:74)
org.springframework.web.servlet.support.JspAwareRequestContext.<init>(JspAwareRequestContext.java:48)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:77)
org.apache.jsp.login_jsp._jspx_meth_form_005fform_005f0(login_jsp.java:111)
org.apache.jsp.login_jsp._jspService(login_jsp.java:76)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
当 IAM 使用以下表格时:
<form:form method="post" commandName="user" action="login">
<form:label path="Username" /><form:input path="uname"/>
<form:label path="Password" /><form:input path="password"/>
<input type="submit" value="Submit" />
</form:form>
案例2:当我提交以下表单后,我收到 404,我的意思是这个网址:http://localhost:8443/BugTrackingSystem/login
当 IAM 使用以下表格时:
<form action="login" method="post">
Username: <input type="text" name="uname">
Password: <input type="text" name="password">
<input type="submit" value="Submit">
</form>
其他文件:*登录 Controller :*
package com.bts.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractFormController;
import org.springframework.web.servlet.mvc.SimpleFormController;
import com.bts.vo.User;
public class LoginController extends SimpleFormController{
public LoginController(){
setCommandClass(User.class);
setCommandName("user");
}
protected ModelAndView onSubmit(Object obj) throws ServletException {
User user = (User) obj;
System.out.println("username: "+user.getUname());
return new ModelAndView("index","user", user);
}
}
WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>BugTrackingSystem</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.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>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
USER.JAVA
package com.bts.vo;
import java.util.ArrayList;
import java.util.List;
public class User {
public User() {
countries = new ArrayList<String>();
countries.add("India");
countries.add("US");
}
private String uname;
private String password;
private List<String> countries;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<String> getCountries() {
return countries;
}
public void setCountries(List<String> countries) {
this.countries = countries;
}
}
DISPATCHER-SERVLET.XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="1">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
CONTROLLERS-BEANS.XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="login" class="com.bts.controller.LoginController">
<property name="successView" value="index" />
</bean>
<bean name="productDetails"
class="com.bts.controller.ProductDetailsController">
</bean>
</beans>
application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<import resource="dispatcher-servlet.xml"/>
<import resource="controllers-beans.xml"/>
<import resource="datasource.xml"/>
<import resource="hibernate-beans.xml"/>
</beans>
我现在正在学习Spring,所以我遇到了上述问题,所以请给我一个解决方案,等我理解这个问题后我会使用Annotations..
最佳答案
我推荐以下链接:
http://duckranger.com/2012/04/spring-mvc-dispatcherservlet/
http://viralpatel.net/blogs/spring-3-mvc-handling-forms/
http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html
这个设置对我有用:
web.xml
<web-app version="3.0" 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_3_0.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>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
[...]
</web-app>
application-context.xml
<beans xmlns="http://www.springframework.org/schema/beans">
<import resource="myOtherContext.xml"/>
</beans>
myOtherContext.xml
(为了进行一些导入)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="login" class="test.LoginController"/>
</beans>
dispatcher-context.xml
(请注意,这是自动寻址的,因为我的调度程序 servlet 称为 dispatcher
并且它附加了 -context.xml
自动)
<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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.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">index</prop>
<prop key="login.htm">login</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="index"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
login.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>Login</title>
</head>
<body>
<form:form method="post" commandName="user" action="login.htm">
<form:label path="uname" /><form:input path="uname"/>
<form:label path="password" /><form:input path="password"/>
<input type="submit" value="Submit" />
</form:form>
</body>
</html>
LoginController.java
public class LoginController extends SimpleFormController {
public LoginController() {
setCommandClass(User.class);
setCommandName("user");
}
@Override
protected org.springframework.web.servlet.ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel) throws Exception {
return super.showForm(request, response, errors, controlModel);
}
@Override
protected ModelAndView onSubmit(Object obj) throws ServletException {
User user = (User) obj;
System.out.println("username: " + user.getUname());
return new ModelAndView("index", "user", user);
}
}
关于java - SpringMVC 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16768928/
基于SpringBoot版本如下: org.springframework.boot spring-boot-starter-parent 2.5.2
@RestController public class TestController { @GetMapping("/download") public ResponseEntit
概述 记得之前跟前端同事联调接口的时候,后端SpringMVC需要接收数组类型的参数,然后跟前端说需要传数组类型过来。后来前端童鞋传了数组,但是后端接收不成功,联调失败。那时候由于时间关系没有仔细研究
web.xml 片段: contextConfigLocation /WEB-INF/applicationContext-security.xml a
目录 相关准备 功能清单 具体功能:访问首页 ①配置view-controller ②创建页面
Spring mvc是一个非常轻量的mvc框架,注解可以大大减少配置,让请求的拦截变得比较简单。这次记录下@RequestBody 注解接收参数尤其是数组参数的用法。 关于容器的配置不再多说,这里
目录 SpringMVC默认处理的几种异常 @ResponseStatus 异常处理的顺序 自定义异常类(SpringMVC的异常处理)
目录 SpringMVC 接收前端传递的参数四种方式 @RequestParam 获取注解 @PathVariable获取注解 Sp
目录 @PathVariable的用法解析 问题描述 解析过程 动态参数使用@PathVariable
目录 SpringMVC @NotNull校验不生效 加了两个依赖问题解决 @NotNull注解失效原因之一 Lo
springmvc―handlermapping三种映射 handlermapping负责映射中央处理器转发给controller的映射策略,简单说就是控制中央处理器的请求触发哪一个control
目录 使用ModelAndView向request域对象共享数据 使用Model向request域对象共享数据 使用map向request域对象共享数据
整合SSM 环境要求 环境: IDEA MySQL5.7.19 Tomcat9 Maven3.6 要求: 需要熟练掌握MySQL数据库,Spring,Ja
目录 1、SpringMVC简介 2、工作流程与介绍 3、代码截图 以下组件通常使用框架提供实现: 1、Di
简介 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。开发者可以自己定义一些拦截器来实现特定的功能。 过滤器
背景 举个例子,出现中文乱码的例子:提交表单的时候。 表单 ?
请求进入DispatcherServlet的doDispatch后,获取HandlerMethod。然后根据HandlerMethod来确认HandlerApapter,确认后执行HandlerAd
实现需求: 1.用户未登录,跳转到登录页,登录完成后会跳到初始访问页。 2.用户自定义处理(如需要激活),跳转到激活页面,激活完成后会跳到初始访问页。 使用到的框架 springmvc 的拦
为了实现用户登录拦截你是否写过如下代码呢? 1. 基于Filter ?
springmvc dao层和service层的区别 首先解释面上意思,service是业务层,dao是数据访问层 这个问题我曾经也有过,记得以前刚学编程的时候,都是在service里直接调用d
我是一名优秀的程序员,十分优秀!