我有一个Web应用程序,我们目前正在为REST服务应用spring MVC.我们希望我们的休息服务出现在下面${contextPath}/rest/**,但是当我设置它时,我们得到:
在DispatcherServlet中找不到带有URI [/ myapp/rest/testSvc/message]的HTTP请求的映射,名称为"Spring MVC Dispatcher Servlet"
我web.xml有:
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/servlet-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/rest</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
servlet-context.xml,这很好,并在注册启动时注册服务.
<context:component-scan base-package="com.mycompany.myapp.rest" />
<mvc:annotation-driven />
Run Code Online (Sandbox Code Playgroud)
我的控制器如下所示:
@Controller
@RequestMapping(value = "/rest/testService")
public class TestREST {
@RequestMapping(value="message", method=RequestMethod.GET)
public @ResponseBody String getMessage() {
return "REST working";
}
Run Code Online (Sandbox Code Playgroud)
如果我cahnge的url-pattern在web.xml为*.rest和我的请求映射message到message.rest它的工作原理.
我使用Spring 3,基于java的配置,使用BootStrap.
我已经下载了bootstrap并将css和js放在resources目录下.
我不能在freemarker页面中使用这些.css的问题.但是我进口了它们.当我使用基于java的配置时,我添加了"addResourceHandler",如下所示:
WebAppConfig:
@Configuration
@EnableWebMvc
@ComponentScan("com.springway")
public class WebConfig implements WebApplicationInitializer {
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.setServletContext(servletContext);
root.scan("com.springway");
root.refresh();
final ServletRegistration.Dynamic servlet = servletContext.addServlet("spring", new DispatcherServlet(root));
servlet.setLoadOnStartup(1);
servlet.addMapping("/*");
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
Run Code Online (Sandbox Code Playgroud)
Tomcat日志说:
"警告:没有找到带URI的HTTP请求的映射
名为'spring'的DispatcherServlet中的[/springway/resources/css/bootstrap-responsive.css]
目录 :
-SpringWay
> -src
> - main
> -webapp
> -resources
-WEB-INF
-welcome.ftl
-springway.ftl
Run Code Online (Sandbox Code Playgroud)
welcome.ftl:
[#ftl /]
[#include "springway.ftl" /]
<ul class="breadcrumb">
<li>
<a href="[@spring.url '/test'/]">Test</a> <span …Run Code Online (Sandbox Code Playgroud) 我有一个页面,其中包含指向应用程序中其他页面的多个链接.当用户访问网站时,显示系统页面,即使用户未登录也可以访问.但是,如果用户点击其他链接,则需要用户登录,用户被正确地重定向到登录页面.但是,登录后,用户不会被重定向到单击的链接,而是将用户带到系统页面(系统页面是默认目标URL).
这是spring配置文件的片段 -
<logout logout-url="/logout"
logout-success-url="/login"
invalidate-session="true" />
<form-login login-page="/login"
login-processing-url="/j_security_check"
always-use-default-target="false"
authentication-failure-url="/login?failure=true"
username-parameter="username"
password-parameter="password"/>
Run Code Online (Sandbox Code Playgroud)
我尝试使用authentication-success-handler-ref属性来引用一个SavedRequestAwareAuthenticationSuccessHandler实例,但是,它也没有用.
有什么投入?
我有src/main/resources以下文件
bpp-dev.properties
bpp-prod.properties
bpp-test.properties
Run Code Online (Sandbox Code Playgroud)
通过我的STS,我可以在两个位置定义密钥envB。
VM argument如-DenvB=devEnvironment如变量 envB和价值 prod如果在Configuration课堂上,我有以下内容。
@Configuration
@PropertySource("classpath:/com/manuel/jordan/properties/bpp-${envB}.properties")
public class PropertiesConfiguration {
Run Code Online (Sandbox Code Playgroud)
它工作正常,但始终优先于System Propertiesover Environment Variables,这是默认行为。我在这里没问题。
但是,如果我想与显式地工作Environment Variables,以下失败
@Configuration
@PropertySource("classpath:/com/manuel/jordan/properties/bpp-#{systemEnvironment['envB']}.properties")
public class PropertiesConfiguration {
Run Code Online (Sandbox Code Playgroud)
我总是收到:
Caused by: java.io.FileNotFoundException:
class path resource
[com/manuel/jordan/properties/bpp-#{systemEnvironment['envB']}.properties]
cannot be opened because it does not exist
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
如果我使用该功能@PropertySource并且只是在同一@Configuration堂课上玩,那么我将使用以下内容:
@Value("#{systemProperties['envB']}")
private String propertiesEnvB;
@Value("#{systemEnvironment['envB']}")
private String environmentEnvB;
Run Code Online (Sandbox Code Playgroud)
待稍后打印,两者都可以正常工作。
在工作中,我们有一个(大约1到1年的开发,团队中有十几个人)项目即将开始,"高层"正在考虑将Spring 3作为Web框架(营销热点). .. 随你).
这不是一个简单的Web应用程序,我们没有Spring 3的经验(虽然我们已经有了一些Spring 2的经验......但我们绝不是专家).我们应该学习Spring 3并开始编码.
但是我有这种感觉,我们会很难过.
Spring 3 GA已经存在了一年左右,书籍似乎还没有赶上.到目前为止,我发现Pro Spring 3和Spring的第三版正在进行中,它涵盖了Spring 3.在线教程并不是很复杂,似乎我们所有的参考资料都是基础(这是一个简洁的基础)文献).
正如我所说,我有一种不好的感觉.不要误会我的意思我不介意学习Spring 3(简单或艰难),但我不确定"项目的安全性"(大项目,小时间框架......死亡之气的气味)如果我们选择Spring 3.
我正在呼吁你的经历.你有没有用Spring 3创建大型项目?你遇到了什么问题?
当我从springsource.org的下载页面下载Spring Framework(最新的GA版本:3.0.5.RELEASE)时,ZIP文件不包含spring-webmvc-3.0.5.RELEASE.jar或类似的东西.我在这里错过了什么吗?
实际的ZIP文件位于:http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-3.0.5.RELEASE.zip
我正在准备一个豆子,它存在于我正在使用的一个罐子里.
该类具有setter方法,这些方法不符合Spring框架期望执行注入的标准setter方法名称(例如,该属性userName具有setter方法addUserName而不是setUserName).
有没有办法在注入属性时指定setter方法名?
我在我的控制器上使用Spring 3.0.5 <context:component-scan>和@RequestMapping注释.这样可以工作,并且包扫描会注册URL.
但是当我在XML配置中定义了处理程序映射时会出现问题.该@RequestMapping注释不再拾起.
我已将问题分离到一个简单的应用程序.
如果我有以下控制器:
package test.pack.age;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping(value="/test")
public String showTestPage() {
return "testPage";
}
}
Run Code Online (Sandbox Code Playgroud)
以及以下配置:
<context:component-scan base-package="test.pack.age" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Run Code Online (Sandbox Code Playgroud)
应用程序正常工作,URL /test已注册并正常工作.
18/09/2011 20:02:55 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping INFO Mapped URL path [/test] onto handler 'testController'
18/09/2011 20:02:55 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping INFO Mapped URL path [/test] onto handler 'testController'
Run Code Online (Sandbox Code Playgroud)
但是如果我添加一个映射到XML的处理程序,它就不再起作用了.即使是这样简单的东西:
<bean id="handlerMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" …Run Code Online (Sandbox Code Playgroud) 在supports(Class clazz)方法中RoleVoter,它总是返回true表示
此实现支持任何类型的类,因为它不查询呈现的安全对象.
什么是" 呈现的安全对象 ".另一方面,只有在子类型为的情况下才返回true 的supports(Class clazz)方法.这里是"呈现的安全对象",为什么选民必须支持它?WebExpressionVoterclazzFilterInvocationFilterInvocation
如果我@Secured对我的方法使用注释并为具有WebExpressionVoter选民之一的全局方法安全性配置访问决策管理器,则会出现错误
AccessDecisionManager不支持安全对象类:interface org.aopalliance.intercept.MethodInvocation
这是因为访问决策管理器的所有选民(当配置为方法安全性时)必须支持上述类,而RoleVoter其他人则WebExpressionVoter需要支持子类型FilterInvocation.
SPEL@PreAuthorize标签中的表达式也需要WebExpressionVoter,并且还需要支持MethodInvocation类,而不是.但它确实有效.那我在这里错了什么?
春季版是3.2
在Spring in Action,第三版,3.1.1.陈述了四种自动装配,即
构造函数自动装配与byType共享相同的限制.当Spring找到多个与构造函数的参数匹配的bean时,它不会尝试猜测哪个bean要自动装配.此外,如果一个类有多个构造函数,其中任何一个都可以通过自动装配来满足,Spring将不会尝试猜测要使用哪个构造函数.
这最后一句让我感到困惑.我有以下bean定义:
<bean id="independentBean" class="autowiring.IndependentBean" scope="prototype" />
<bean id="weirdBean" class="autowiring.WeirdBean" scope="prototype" />
<bean id="dependentBeanAutowiredByName" class="autowiring.DependentBean" autowire="byName" />
<bean id="dependentBeanAutowiredByType" class="autowiring.DependentBean" autowire="byType" />
<bean id="dependentBeanAutowiredByConstructor" class="autowiring.DependentBean" autowire="constructor" />
Run Code Online (Sandbox Code Playgroud)
IndependentBean和WeirdBean是空类.
DepentendBean定义如下:
package autowiring;
public class DependentBean {
private IndependentBean independentBean;
private IndependentBean anotherBean;
private WeirdBean weirdBean;
public DependentBean() {
}
public DependentBean(IndependentBean independentBean) {
super();
this.independentBean = independentBean;
}
public DependentBean(IndependentBean independentBean, IndependentBean anotherBean) {
super();
this.independentBean = independentBean;
this.anotherBean = anotherBean;
}
public DependentBean(IndependentBean independentBean, …Run Code Online (Sandbox Code Playgroud) spring-3 ×10
spring ×8
java ×4
spring-mvc ×4
annotations ×1
autowired ×1
constructor ×1
controller ×1
spring-4 ×1