如何在视图上显示由于@AssertTrue注释而发生的jsp验证错误消息?它不依赖于特定字段,但我使用它来验证字段组合.如果我使用<form:errors path="*"/>它将显示该表单的所有错误?
我已经多次使用Spring的文档,但我似乎无法使@Controller等注释工作.
我在我的POM中加载依赖项和存储库(...是我的具体值):
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>...</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.context</artifactId>
<version>${org.springframework.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.web.servlet</artifactId>
<version>${org.springframework.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>com.springsource.repository.bundles.release</id>
<url>http://repository.springsource.com/maven/bundles/release/</url>
</repository>
<repository>
<id>com.springsource.repository.bundles.external</id>
<url>http://respoitory.springsource.com/maven/bundles/external</url>
</repository>
</repositories>
</project>
Run Code Online (Sandbox Code Playgroud)
在我的web.xml中,我正在设置调度程序servlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
在WEB-INF下我有servlet-context.xml(...是我的控制器包): …
我正在使用 Spring 3.1 开始一个新项目,并且一直在关注有关如何使用 @Controller 注释的所有文档和论坛意见。
我个人不喜欢为 MVC 使用注解;我更喜欢使用 SimpleUrlHandlerMapping 在一个地方提供一个 web 应用程序的所有 URL。
此外,从以前使用 Spring 2.x 的许多工作来看,我非常习惯 BaseCommandController 层次结构。
我一直很喜欢 Spring,因为它赋予了权力而不受限制。现在我发现 Spring MVC 迫使我将 URL 放入 java 源代码中,这意味着 (a) 我无法将控制器映射到多个 URL,以及 (b) 要发现 webapp 中正在使用的 URL,我必须扫描通过不同的 java 源文件,我觉得这是不切实际的。
请推荐将@Controller 与 SimpleUrlHandlerMapping 结合使用的方法是什么?
更新:
嗨戴夫,你是说你可以像这样映射多个 URL(从 petclini.web.ClinicController 改变)?
@RequestMapping({"/vets", "/another"})
public ModelMap vetsHandler() {
Run Code Online (Sandbox Code Playgroud)
如果这有效,那么很好。
我的问题仍然存在:如果我不想在我的 java 源代码中使用 URL,如何最好地将它们与 @Controller 类映射?
问候,
我一直收到一条消息:"org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'userDetailsService'的bean",即使bean被定义为UserDetailsServiceImpl类中的服务.如:
@Service(value="userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService{
Run Code Online (Sandbox Code Playgroud)
我正在使用最新的Spring和Spring Security.
我不确定我做错了什么.我正在粘贴我的spring xml文件的配置.spring-security.xml显示它在使用组件扫描时可以从servlet-context.xml中找到bean.
谢谢
org.springframework.beans.factory.BeanCreationException: Error creating bean with name org.springframework.security.filterChains': Cannot resolve reference to bean org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property sourceList' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name org.springframework.security.web.DefaultSecurityFilterChain#0': Cannot resolve reference to bean org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [2]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean org.springframework.security.authentication.ProviderManager#0' while setting bean property authenticationManager'; nested exception is …Run Code Online (Sandbox Code Playgroud) 我最近几天一直在玩Spring引导,我非常喜欢@WebIntegrationTest注释,它允许启动Tomcat并自动部署Spring启动应用程序.它提供了在随机端口上启动Tomcat的选项,如果您正在测试webapp,这非常容易.但我的情况略有不同.整个源代码可在此处获得:
https://github.com/vincent-fuchs/spring-projects
使用Spring Batch,我需要解析Excel文件,并将项目写入REST webService.当我事先知道编写者需要使用的URL时,它能很好地工作,并且我能够使用Spring Boot在几秒钟内测试整个流程,方法是加载一个"空心"@RestController,它接收请求并使它们可用对断言的测试.
(注意:我确信这对很多人来说都非常有用.如果你试图测试Spring Integration配置,它也可以工作)
这是我要测试的批处理,而不是webService.但是为了测试这批产品的输出,我需要这个"空心"的web服务.
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes =
{
//the webservice client that the batch should use (planning for SOAP impl later)
RestCustomerWsClient.class,
//the "target" web server, with the 'hollow' endpoint listening
TargetRESTSystem.class,
//the application under test and its config
TestSpecificConfiguration.class,
CustomerBatchConfiguration.class}
)
@WebIntegrationTest("server.port:8080")
@IntegrationTest({"spring.batch.job.enabled=false"})
public class CustomerBatchWithRestTest {
@Value("${local.server.port}")
public int targetWebServerPort;
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
private DummyCustomerController endpoint ;
@Autowired
private ConfigurableApplicationContext appCtx ;
@Autowired
private ConfigurableEnvironment env;
@Before
public void loadDynamicProperty() { …Run Code Online (Sandbox Code Playgroud) 我通过注释配置了spring应用程序.这是我配置的一部分
@Configuration
@EnableTransactionManagement
public class JpaSpringConfiguration {
@Bean(destroyMethod = "close")
@Lazy
@Primary
public BasicDataSource dataSource(@Value("${statistics.hostname}") String statisticsHostname) {
final BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
String url = String.format("jdbc:postgresql://%s:5432/statistics-db", statisticsHostname);
dataSource.setUrl(url);
....
return dataSource;
}
@Bean
public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
final PropertyPlaceholderConfigurer placeholderConfigurer = new PropertyPlaceholderConfigurer();
placeholderConfigurer.setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_OVERRIDE);
Properties properties = new Properties();
properties.setProperty("statistics.hostname", "localhost");
placeholderConfigurer.setProperties(properties);
return placeholderConfigurer;
}
Run Code Online (Sandbox Code Playgroud)
直到最近我们才有了xml配置
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="properties">
<props>
<prop key="statistics.hostname">localhost</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="true" destroy-method="close">
<property …Run Code Online (Sandbox Code Playgroud) 我有带有几个(REST)端点的spring控制器.我想提出这个控制器的多个实例,其中每个实例都有几个端点有选择地启用/禁用.
根据我到目前为止的阅读,togglz提供了功能翻转,但它不启用/禁用REST端点(togglz提供API,以便调用者代码可以检查是否启用了功能); ff4j似乎是另一种选择,但是如果它可以启用/禁用REST端点,则从文档中不是很明显
我阅读了线程功能切换Java注释,但它是一个更长的实现.是否有任何软件包可用于指定需要在配置文件中启用/禁用的端点,并在REST端点上使用注释来禁用/启用它们(这样我的方法中的逻辑保持不变并最小化测试)
我不确定为什么,但是它@ControllerAdvice会覆盖Exception使用@ResponseStatus注释在级别定义的响应代码.
例外:
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class GreetException extends RuntimeException {}
Run Code Online (Sandbox Code Playgroud)
控制器:
@RestController
@RequestMapping("/")
public class GreetController {
@RequestMapping(method = RequestMethod.GET)
public String greet() {
throw new GreetException();
}
}
Run Code Online (Sandbox Code Playgroud)
控制建议:
@ControllerAdvice
public class ExceptionConfiguration {
@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler(RuntimeException.class)
public void handleConflict() {}
}
Run Code Online (Sandbox Code Playgroud)
来自GreetController被称为响应的greet方法的响应是409 - CONFLICT.因为我特意提供了异常级别的响应代码,我预计这将是返回的代码(400 - BAD_REQUEST).
当然,这是一个过于简化的示例,但我们定义了一个带有RuntimeException定义的控制器建议,因此我们可以为每个未捕获的异常分配一个id.
实现预期行为的正确方法是什么?
使用jdk 1.8
我遇到了这样的错误
org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping'的bean时出错:init方法的调用失败; 嵌套异常是java.lang.NoSuchMethodError:org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation
我的问题:
为什么错误的鼓励和如何解决它?
web.xml中
<web-app 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"
version="3.1">
<servlet>
<servlet-name>product</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>product</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
产品servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="com.kopylov.spring.controller" />
<mvc:annotation-driven/>
Run Code Online (Sandbox Code Playgroud)
调节器
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ProductController {
@RequestMapping
public String showHome(){
return "home";
}
Run Code Online (Sandbox Code Playgroud)
}
AppInitializer
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
Run Code Online (Sandbox Code Playgroud)
公共类AppInitializer扩展AbstractAnnotationConfigDispatcherServletInitializer { …
我想要一个简单的,最好是基于注释的方法将外部属性注入到java程序中,而不使用spring framework(org.springframework.beans.factory.annotation.Value;)
SomeClass.java
@Value("${some.property.name}")
private String somePropertyName;
Run Code Online (Sandbox Code Playgroud)
application.yml
some:
property:
name: someValue
Run Code Online (Sandbox Code Playgroud)
是否有推荐的方法在标准库中执行此操作?
spring ×8
java ×6
spring-mvc ×5
annotations ×2
spring-boot ×2
eclipse ×1
ff4j ×1
maven-2 ×1
properties ×1
spring-batch ×1
togglz ×1