我是春天新手。只想知道我们可以验证请求参数密钥。
我已经使用了RequestParam
,因为例如@RequestParam(value= "sourceType")
此处value = "sourceType"
是我的密钥,因此如果用户输入的sourcety
与给定的密钥不匹配,则会400
404
1}}错误,url中缺少sourceType。而不是给出@Scope("request")
@RestController
public class GetOperatorSeries{
@RequestMapping(value = "test", method = RequestMethod.GET)
public String getOperatorSeries(HttpServletResponse response, @RequestParam(value = "mobno") long mobno,
@RequestParam(value = "sourceType") String sourceType,
@RequestParam(value = "responseType") String responseType)
{
}
错误,我希望它只在url或无效的sourceType中丢失sourceType?
这是我的控制器,
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"
xmlns:web1="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="voiceBank" version="2.5">
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<servlet>
<servlet-name>testmemspring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>testmemspring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/testmemspring-servlet.xml
</param-value>
</context-param>
文件
<?xml version="1.0" encoding="UTF-8"?>
<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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:security="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
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-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.one97.org" />
<context:annotation-config />
<mvc:annotation-driven />
<bean id="testSpring" class="com.one97.org.controller.TestSpring"
scope="request">
</bean>
<bean id="internalViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>
</beans>
这是我的弹簧配置文件
$.post( "check.php", { user: $("#username").val() }, function (data){
if(data=='1'){
//do 1
}
elseif(data=='0'){
//do 0
}
});
答案 0 :(得分:0)
您可以将请求参数声明为可选。然后您可以手动检查它是否存在。根据它,您可以显示自定义错误消息。例如 -
@Scope("request")
@RestController
public class GetOperatorSeries{
@RequestMapping(value = "test", method = RequestMethod.GET)
public String getOperatorSeries(HttpServletResponse response, @RequestParam(value = "mobno") long mobno,
@RequestParam(value = "sourceType", required=false) String sourceType,
@RequestParam(value = "responseType") String responseType, Model model)
{
if (sourceType == null) {
model.addAttribute("errorMessage", "Source Type is missing");
return "viewName"; // Now show the error message in view. You can also add this error as flash attribute. You can also show a new error page.
}
}