Spring MVC Java配置
我想从SpringWebApp设置一个简单的响应主体。 我的问题很简单,就是给定了一个web错误 我的POM.xml是:Spring MVC Java配置,java,spring-mvc,Java,Spring Mvc,我想从SpringWebApp设置一个简单的响应主体。 我的问题很简单,就是给定了一个web错误 我的POM.xml是: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pt.dummy</groupId>
<artifactId>dummy</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>dummy Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>
<tomcat.servlet.version>7.0.42</tomcat.servlet.version>
<log4j.version>1.7.5</log4j.version>
<java.version>1.7</java.version>
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.servlet.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>${tomcat.servlet.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.servlet.version}</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-framework.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-framework.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>dummy</finalName>
<testResources>
<testResource>
<!-- declared explicitly so Spring config files can be placed next to their corresponding JUnit test class
(see example with ValidatorTests) -->
<directory>${project.basedir}/src/test/java</directory>
</testResource>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Web配置:
package dummy.web.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"dummy.web.controller"})
public class WebConfig {
}
现场控制员:
package dummy.web.controller;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/")
public class SiteController {
// private static final Logger LOG = LoggerFactory.getLogger(SiteController.class);
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String getHome() {
// LOG.debug("Home to ResponseBody");
return "Response Body";
}
}
有人能指出为使这项工作正常进行所需的更改吗?我相信这是因为Tomcat。web上的大多数教程都将SpringMVCServlet直接放在应用程序上下文中。这对我从来都不起作用 在Tomcat7上(即使使用XML配置),您也必须创建两个上下文:一个用于整个应用程序,另一个用于SpringWebMVC。这和
@RequestMapping("/")
服务器将“/”映射分配给默认的内置servlet。这是你希望他(或它或她)做的事情。但您还需要SpringMVC来映射“/”
也许他们(架构师)认为springmvc是一个特定的servlet,不应该映射根上下文。相反,它应该在他自己的映射下(例如“/springmvc/”)。然后它期望我们有一个真正的调度器,它在springmvc和任何其他servlet之间进行调度
出于某种神奇的原因,在Tomcat 7.0.29中,如果您试图劫持“/”它甚至无法“发送”。在最新版本中,映射“/”是有效的。但为此,您需要一个单独的web mvc上下文/根上下文
我不使用AbstractAnnotationConfigDispatchersServletInitializer,您必须翻译下面的代码。
这是根据从XML迁移到Javaconfig的教程改编的
公共类WebInit实现WebApplicationInitializer{
私有静态最终字符串调度程序\u SERVLET \u NAME=“spring mvc”;
私有静态最终字符串调度程序_SERVLET_映射=“/”;
@凌驾
启动时公共无效(ServletContext ServletContext)
抛出ServletException{
//如果要使用XML配置,请注释下面两行。
AnnotationConfigWebApplicationContext appContext=新的AnnotationConfigWebApplicationContext();
register(CoreConfig.class);
appContext.setDisplayName(“删除的客户名称”);
//如果要使用XML配置,请取消注释以下行。
//XmlWebApplicationContext rootContext=新的XmlWebApplicationContext();
//setConfigLocation(“classpath:mvcservlet.xml”);
AnnotationConfigWebApplicationContext mvcContext=新的AnnotationConfigWebApplicationContext();
mvcContext.register(ServletConfig.class);
ServletRegistration.DynamicSpringMVC=
addServlet(DISPATCHER\u SERVLET\u名称,
新DispatcherServlet(mvcContext));
springmvc.setLoadOnStartup(1);
addMapping(DISPATCHER\u SERVLET\u映射);
EnumSet dispatcherTypes=EnumSet.of(DispatcherType.REQUEST,DispatcherType.FORWARD);
CharacterEncodingFilter CharacterEncodingFilter=新的CharacterEncodingFilter();
characterEncodingFilter.setEncoding(“UTF-8”);
characterEncodingFilter.setForceEncoding(true);
FilterRegistration.DynamicCharacterEncoding=servletContext.addFilter(“characterEncoding”,characterEncodingFilter);
characterEncoding.addMappingForUrlPatterns(dispatcherTypes,true,“/*”;
FilterRegistration.Dynamic security=servletContext.addFilter(“springSecurityFilterChain”,新的DelegatingFilterProxy());
addMappingForUrlPatterns(dispatcherTypes,true,“/*”);
addListener(新的ContextLoaderListener(appContext));
}
什么是简单的web错误?此开发的预期结果是一个包含以下文本的网页:响应正文
您没有bes calypso web的url映射您没有/dummy/
的url映射。很抱歉,从原始应用发送了反馈。您没有/dummy/
的url映射n我的情况是因为我使用的是Tomcat版本7.0.12。谢谢
package dummy.web.controller;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/")
public class SiteController {
// private static final Logger LOG = LoggerFactory.getLogger(SiteController.class);
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String getHome() {
// LOG.debug("Home to ResponseBody");
return "Response Body";
}
}
@RequestMapping("/")
public class WebInit implements WebApplicationInitializer {
private static final String DISPATCHER_SERVLET_NAME = "spring-mvc";
private static final String DISPATCHER_SERVLET_MAPPING = "/";
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
//If you want to use the XML configuration, comment the following two lines out.
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(CoreConfig.class);
appContext.setDisplayName("removed customer name");
//If you want to use the XML configuration, uncomment the following lines.
//XmlWebApplicationContext rootContext = new XmlWebApplicationContext();
//rootContext.setConfigLocation("classpath:mvc-servlet.xml");
AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
mvcContext.register(ServletConfig.class);
ServletRegistration.Dynamic springmvc =
servletContext.addServlet(DISPATCHER_SERVLET_NAME,
new DispatcherServlet(mvcContext));
springmvc.setLoadOnStartup(1);
springmvc.addMapping(DISPATCHER_SERVLET_MAPPING);
EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
FilterRegistration.Dynamic characterEncoding = servletContext.addFilter("characterEncoding", characterEncodingFilter);
characterEncoding.addMappingForUrlPatterns(dispatcherTypes, true, "/*");
FilterRegistration.Dynamic security = servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy());
security.addMappingForUrlPatterns(dispatcherTypes, true, "/*");
servletContext.addListener(new ContextLoaderListener(appContext));
}