我想在我的春季网络应用中实现类似http://www.host.abc/action?view=jobs的网址编码,但无法通过我的策略完成工作,这是
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
return "home";
}
@RequestMapping(value = "/home/action?view=jobs", method = RequestMethod.GET)
public String showJobs(Model model) {
//some stuff goes here
return ("/home/action?view=jobs");
}
}
home.jsp是
<c:if test="${param.view == 'jobs' }">
<!-- List of Jobs -->
</c:if>
这给了我警告
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/jobsnetwork/home/action] in DispatcherServlet with name 'springDispatcher'
最后我将maping添加到WebApplicationInitializer类中作为
public class AppInit implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext context) {
XmlWebApplicationContext rootContext =
new XmlWebApplicationContext();
rootContext.setConfigLocation("/WEB-INF/spring/root-context.xml");
context.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
XmlWebApplicationContext servletContext =
new XmlWebApplicationContext();
servletContext.setConfigLocation("/WEB-INF/spring/appServlet/servlet-context.xml");
// add the dispatcher servlet and map it to /
ServletRegistration.Dynamic dispatcher =
context.addServlet("springDispatcher", new DispatcherServlet(servletContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
dispatcher.addMapping("/home/action");// added mapping here
}
}
以上内容无效
答案 0 :(得分:0)
您的映射应仅包含路径("/jobsnetwork/home/action"
),而不是请求参数("?view=jobs"
):
@RequestMapping(value = "/jobsnetwork/home/action", method = RequestMethod.GET)
public String showJobs(@RequestParam("view") String view, Model model) {
if (view.equals("jobs")) {
// do stuff if ?view=jobs
} else {
// do stuff if not ?view=jobs
}
}
答案 1 :(得分:-1)
尝试重定向: -
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
return "home";
}
@RequestMapping(value = "/action", method = RequestMethod.GET)
public String showJobs(@RequestParam("view") String view,Model model) {
//some stuff goes here
return "redirect:/action?view=jobs";
}
}