我正在尝试在我的spring应用程序中构建一个功能,该功能将本地语言更改为切换语言。我以为我的逻辑是对的,但我猜不是。当我点击设置区域设置按钮时,它提示我为404
用户将在JSP中选择一个按钮,如Allbeers.jsp
所示。 <form action="setLocale">
<select name="locale">
<option value="en"><spring:message code="lang.english"/></option>
<option value="es"><spring:message code="lang.spanish"/></option>
</select>
<input type="submit" value="<spring:message code="button.setLocale"/>"/>
</form>
form行为会将您带到控制器beerController.java
@RequestMapping(value = "setLocale", method = RequestMethod.GET)
public ModelAndView SetLocale(@RequestParam("locale") String locale, HttpServletRequest request, HttpServletResponse response)
{
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
localeResolver.setLocale(request, response, StringUtils.parseLocaleString(locale));
return new ModelAndView("redirect:/beer");
}
我的config.xml中的我的bean localeResolver
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en_IE" />
</bean>
这是我所有的控制器代码,以防需要知道如何映射方法
@Controller
@RequestMapping("/beer")
@SessionAttributes("beer")
public class beerController {
@Autowired
beerService service;
@RequestMapping("")
public ModelAndView getBeers() {
//int x = Integer.parseInt("abc");
return new ModelAndView("/Allbeers", "beerList", service.getAllBeers());
}
@RequestMapping(value = "setLocale", method = RequestMethod.GET)
public ModelAndView SetLocale(@RequestParam("locale") String locale, HttpServletRequest request, HttpServletResponse response)
{
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
localeResolver.setLocale(request, response, StringUtils.parseLocaleString(locale));
return new ModelAndView("redirect:/beer");
}
答案 0 :(得分:0)
jsp中的form操作没有正确的路径
<form action="/Assignment3/beer/setLocale">
<select name="locale">
<option value="en"><spring:message code="lang.english"/></option>
<option value="es"><spring:message code="lang.spanish"/></option>
</select>
<input type="submit" value="<spring:message code="button.setLocale"/>"/>
</form>