控制器 @RequestMapping 错误 URL
Controller @RequestMapping error URLs
我的应用程序不适用于请求映射,请求始终使用相同的方法 handleRequest,控制器是 RestController.java 并且 URLs 在具有路径 /REST2/ 时重定向到它*,它运作良好,但始终使用相同的方法。但是当我将请求发送到 locahost:9080/myapp/REST2/rrr 并且如果我更改 URL 即 localhost:9080/myapp/REST2/XXX 时,方法 "update" 永远不会工作该请求将转到 handleRequest 方法。
RequestMapping 效果不佳,我尝试了其他解决方案但效果不佳..
你有什么想法吗?
Restcontroller.java
public class RestController implements Controller{
private Configuration config;
private static String[] requestHeaders = {"accept", "pragma"};
private static String[] responseHeaders = {};
public void setConfig(Configuration config)
{
this.config = config;
}
@RequestMapping(value="/rrr") public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws Exception
{
String asset_name = request.getParameter("assetid");
response.setContentType("text/plain"); // Set content type of the response so that jQuery knows what it can expect.
response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
String values = "Working"+ asset_name;
response.setContentLength(values.length());
PrintWriter out = response.getWriter();
out.println(values);
return null;
}
@RequestMapping(value ="/home")public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
{
Arrays.sort( requestHeaders );
Arrays.sort( responseHeaders );
String values;
String asset_name = request.getParameter("assetid");
...
response.setContentLength(values.length());
//
PrintWriter out = response.getWriter();
out.println(values);
return null;
}}
spring-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- View resolver. Prepends prefix /WEB-INF/jsp and suffix .jsp to view names. -->
<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>
<!-- Always use full path when mapping URLs to bean names. -->
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
</bean>
<!-- Home page. -->
<bean name="/home.app" class="com.fatwire.wem.sample.HomeController">
<property name="config" ref="config" />
</bean>
<!-- Installation page. -->
<bean name="/install.app" class="com.fatwire.wem.sample.InstallController">
<property name="config" ref="config" />
</bean>
<!-- Layout page. -->
<bean name="/layout.app" class="com.fatwire.wem.sample.LayoutController" />
<!-- REST proxy page. -->
<bean name="/REST/**" class="com.fatwire.wem.sample.ProxyController">
<property name="config" ref="config" />
</bean>
<!-- REST proxy page. -->
<bean name="/REST2/**" class="com.fatwire.wem.sample.RestController">
<property name="config" ref="config" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
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" >
<!-- Bootstrap Spring configuration to be used by both SSO and MVC -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- WEM SSO Listener -->
<listener>
<listener-class>com.fatwire.wem.sso.SSOListener</listener-class>
</listener>
<!-- Configure Spring MVC -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.app</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/REST/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/REST2/*</url-pattern>
</servlet-mapping>
<!-- WEM SOO Filter -->
<filter>
<filter-name>WEM SSO Filter</filter-name>
<filter-class>com.fatwire.wem.sso.SSOFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>WEM SSO Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
请求AJAX
function load_form(data){
var asset_id = data;
$.post('REST2/home', {
assetid : data,
}, function(responseText) {
alert(responseText);
});
}
function update_form(){
var asset_id = "XXX";
$.post('REST2/rrr', {
assetid : asset_id,
}, function(responseText) {
alert(responseText)
});
尝试向 RestController
类添加 @Controller
和 @RequestMapping
注释,如下所示:
@Controller
@RequestMapping(value="/REST2")
public class RestController implements Controller{
并添加 method=RequestMethod.POST
方法 @RequestMapping
如下:
@RequestMapping(value ="/home", method=RequestMethod.POST)
...
@RequestMapping(value ="/rrr", method=RequestMethod.POST)
我的应用程序不适用于请求映射,请求始终使用相同的方法 handleRequest,控制器是 RestController.java 并且 URLs 在具有路径 /REST2/ 时重定向到它*,它运作良好,但始终使用相同的方法。但是当我将请求发送到 locahost:9080/myapp/REST2/rrr 并且如果我更改 URL 即 localhost:9080/myapp/REST2/XXX 时,方法 "update" 永远不会工作该请求将转到 handleRequest 方法。
RequestMapping 效果不佳,我尝试了其他解决方案但效果不佳..
你有什么想法吗?
Restcontroller.java
public class RestController implements Controller{
private Configuration config;
private static String[] requestHeaders = {"accept", "pragma"};
private static String[] responseHeaders = {};
public void setConfig(Configuration config)
{
this.config = config;
}
@RequestMapping(value="/rrr") public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws Exception
{
String asset_name = request.getParameter("assetid");
response.setContentType("text/plain"); // Set content type of the response so that jQuery knows what it can expect.
response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
String values = "Working"+ asset_name;
response.setContentLength(values.length());
PrintWriter out = response.getWriter();
out.println(values);
return null;
}
@RequestMapping(value ="/home")public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
{
Arrays.sort( requestHeaders );
Arrays.sort( responseHeaders );
String values;
String asset_name = request.getParameter("assetid");
...
response.setContentLength(values.length());
//
PrintWriter out = response.getWriter();
out.println(values);
return null;
}}
spring-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- View resolver. Prepends prefix /WEB-INF/jsp and suffix .jsp to view names. -->
<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>
<!-- Always use full path when mapping URLs to bean names. -->
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
</bean>
<!-- Home page. -->
<bean name="/home.app" class="com.fatwire.wem.sample.HomeController">
<property name="config" ref="config" />
</bean>
<!-- Installation page. -->
<bean name="/install.app" class="com.fatwire.wem.sample.InstallController">
<property name="config" ref="config" />
</bean>
<!-- Layout page. -->
<bean name="/layout.app" class="com.fatwire.wem.sample.LayoutController" />
<!-- REST proxy page. -->
<bean name="/REST/**" class="com.fatwire.wem.sample.ProxyController">
<property name="config" ref="config" />
</bean>
<!-- REST proxy page. -->
<bean name="/REST2/**" class="com.fatwire.wem.sample.RestController">
<property name="config" ref="config" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
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" >
<!-- Bootstrap Spring configuration to be used by both SSO and MVC -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- WEM SSO Listener -->
<listener>
<listener-class>com.fatwire.wem.sso.SSOListener</listener-class>
</listener>
<!-- Configure Spring MVC -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.app</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/REST/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/REST2/*</url-pattern>
</servlet-mapping>
<!-- WEM SOO Filter -->
<filter>
<filter-name>WEM SSO Filter</filter-name>
<filter-class>com.fatwire.wem.sso.SSOFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>WEM SSO Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
请求AJAX
function load_form(data){
var asset_id = data;
$.post('REST2/home', {
assetid : data,
}, function(responseText) {
alert(responseText);
});
}
function update_form(){
var asset_id = "XXX";
$.post('REST2/rrr', {
assetid : asset_id,
}, function(responseText) {
alert(responseText)
});
尝试向 RestController
类添加 @Controller
和 @RequestMapping
注释,如下所示:
@Controller
@RequestMapping(value="/REST2")
public class RestController implements Controller{
并添加 method=RequestMethod.POST
方法 @RequestMapping
如下:
@RequestMapping(value ="/home", method=RequestMethod.POST)
...
@RequestMapping(value ="/rrr", method=RequestMethod.POST)