request mapping url pattern in spring mvc3 - spring

I am using spring mvc3 in my project,and I am confused with the request mapping pattern (the last slash in the url)
Take the following controller method for example:
#RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
public String edit(#PathVariable int id, Model model) {
model.addAttribute(postDao.query(id));
return "posts/edit";
}
It works when get the url "http://localhsot/app/posts/3/edit",however it can not the method if get the url "http://localhsot/app/posts/3/edit/".
I know I can set the request mapping value like this:
#RequestMapping(value = {"/{id}/edit","/{id}/edit/"})
But I wonder if there is any other solution? It seems that rails will ignore the last slash in the url.
UPDATE:
servlet-context.xml:
<mvc:annotation-driven />
<mvc:view-controller path="/" view-name="home" />
<context:component-scan base-package="com.king.controller" />
<mvc:resources mapping="/res/**" location="/res/" />
<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>
web.xml
<filter>
<filter-name>httpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpMethodFilter</filter-name>
<servlet-name>modelServlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>modelServlet</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>modelServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

I think you are trying to solve the wrong problem.
If you match a URL both with and without a trailing slash, you will get a bad rating from search engines because of duplicate content.
What I would do is to add a Filter that sends all requests without trailing slash a redirect with trailing slash (or vice-versa) using status code HttpServletResponse.SC_MOVED_PERMANENTLY
Here is a minimal implementation of such a filter:
public class CanonicalUrlFilter implements Filter {
#Override
public void init(final FilterConfig filterConfig) throws ServletException { }
#Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
final FilterChain filterChain) throws IOException, ServletException {
if (servletRequest instanceof HttpServletRequest) {
HttpServletRequest hsr = (HttpServletRequest) servletRequest;
if (hsr.getMethod().equals("GET") && !hsr.getRequestURI().endsWith("/") && (hsr.getQueryString() == null)) {
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.sendRedirect(hsr.getRequestURI() + "/");
return;
}
}
filterChain.doFilter(servletRequest, servletResponse);
}
#Override
public void destroy() { }
}

Related

not able to get value in view from controller in spring

I have this simple code to get data in view but I am not able to get nor I am getting any stack trace
#Controller
public class ControllerClass {
#Autowired
RepoisitiryClass repoisitiryClass;
#RequestMapping("testData")
public String getView(Model model) {
model.addAttribute("getData", repoisitiryClass.setData());
return "domain";
}
}
public class Domain {
private String domain;
public Domain(String domain) {
this.domain = domain;
}
public String getDomain() {
return domain;
}
}
#Repository
public class RepoisitiryClass {
private String dataDomain = "";
public RepoisitiryClass() {
System.out.println("inside Repo");
Domain domain = new Domain("this is domain");
dataDomain = domain.getDomain();
}
public String setData() {
return dataDomain;
}
}
<mvc:annotation-driven />
<context:component-scan base-package="com" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Now When ever I try to run url : http://localhost:8080/Test_path_Variable/testData it is getting me error 404 page not found.
Is there anyway to solve this error or I am doing somewere wrong?
and project Structure

springmvc and url-pattern

web.xml
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
controller
#Controller
#RequestMapping("/car/*")
public class CarController extends BaseController {
#RequestMapping("baojia.html")
public ModelAndView baojia() {
ModelAndView view = new ModelAndView();
view.setViewName("baojia");
return view;
}
when i visit http://mydomain/car/baojia.html and has this error:
[carloan]2016-04-21 09:01:31,177 WARN [org.springframework.web.servlet.PageNotFound] - <No mapping found for HTTP request with URI [/views/baojia.jsp] in DispatcherServlet with name 'springMVC'>
spring.xml ViewResolver
<bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="cache" value="false"/>
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
and i have file in /views/boajia.jsp
whether i writer, it don't work
<mvc:resources mapping="/views/" location="/views/**" />
and i have another question, i wan't to matching this url-pattern: /api/*
and the controller is:
#Controller
#RequestMapping("/api/*")
public class CarApiController extends BaseController {
#RequestMapping("get")
#ResponseBody
public JsonResult getCars()
but it can't work
try #RequestMapping("/car") instead of #RequestMapping("/car/*")
And check below two links to understand, how request mapping defined.
can anybody explain me difference between class level controller and method level controller..?
http://duckranger.com/2012/04/advanced-requestmapping-tricks-controller-root-and-uri-templates/
URL mapping declaration is not proper use #RequestMapping("/car") and #RequestMapping("/baojia.html")

Spring Boot UTF-8 encoding issue. I suspect it tries to encode ISO-8859-1 to UTF-8

I'd like to use UTF-8 character encoding but I read somewhere that the controller's default encoding's ISO-8859-1.
I'm using spring boot with velocity.
So what I did, I tried to add the following ones (one at a time) to the header (None of them worked.)
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
Plus added to the application.properties the following lines:
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.velocity.charset=UTF-8
spring.velocity.content-type=text/html
server.tomcat.uri-encoding = UTF-8
I even tried to add the following line to the controller:
#RequestMapping(value = "/", method = RequestMethod.GET, produces={"text/html; charset=UTF-8"})
Plus tried to add the following bean to the application class:
#Bean
public HttpMessageConverter<String> responseBodyConverter() {
HttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
return converter;
}
This's a sample text that I included in the velocity template:
A sötét lovag igazi főhőse azonban valahogy ezúttal mégsem a mostanság nőnemű családtagjait szállodaszobábkban riogató Christian Bale, azaz a denevérember lett - hanem az ellenfél.
And that's the output I get:
A sötét lovag igazi fÅhÅse azonban valahogy ezúttal mégsem a mostanság nÅnemű családtagjait szállodaszobábkban riogató Christian Bale, azaz a denevérember lett - hanem az ellenfél.
Edit:
This's the controller I'm using currently:
#RequestMapping(value = "/", method = RequestMethod.GET, produces={"text/html; charset=UTF-8"})
public String homepage(Map<String, Object> model) {
return "homepage";
}
And I have a homepage.vm file at templates. Which has a header partial that contains this line:
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
Could be due to default Eclipse encoding?
Window -> Preferences -> General -> Workspace : Text file encoding
To resolve same problem, I have used VelocityConfigurer with the following bean definition:
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/pages/"/>
<property name="configLocation" value="/WEB-INF/velocity.properties"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="false"/>
<property name="prefix" value=""/>
<property name="suffix" value=".vm"/>
<property name="contentType" value="text/html; charset=UTF-8"/>
</bean>
Please note the contentType property set in viewResolver.
When first tried providing it via VelocityEngineFactoryBean, I have realised late that my properties file was ignored.
Also, in velocity.properties make sure you have:
input.encoding=utf-8
output.encoding=utf-8
I didn't need anything else in here for simple case.
In web.xml:
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
That was it, I had no use for "produces" attribute in controller, no http meta needed, no xml charset encoding specified in the template file and no messing with messageconverter.
EDIT:
I don't have a playground setup with Spring Boot so can't test the exact implementation for you right now. However, web.xml here only defines a filter which can also be achieved programatically with Spring Boot:
import org.springframework.stereotype.Component;
import javax.servlet.*;
import java.io.IOException;
#Component
public class MyFilter implements Filter {
#Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//action here
filterChain.doFilter(servletRequest, servletResponse);
}
#Override
public void init(FilterConfig filterConfig) throws ServletException {}
#Override
public void destroy() {}
}
I believe Spring Boot scans the sources for #Component automatically, otherwise package with filter's implementation should be added to component scan.
As for ideas on what the filter should do within doFilter(), you could explore the source code of Spring's CharacterEncodingFilter: https://github.com/spring-projects/spring-framework/blob/v4.2.1.RELEASE/spring-web/src/main/java/org/springframework/web/filter/CharacterEncodingFilter.java
Note that the above filter already extends other classes (such as OncePerRequestFilter).
I suppose you could instead add a method returning the filter instance:
#Bean
public Filter getCharacterEncodingFilter() {
org.springframework.web.filter.CharacterEncodingFilter characterEncodingFilter = new org.springframework.web.filter.CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
return characterEncodingFilter;
}

Spring mvc controller handling all request and should not do that

I have two controllers:
UserController and AdminController.
The AdminController is handling all requests that have not created. Also handle the request to access to my resources.
Also I'm using spring-security and maven to compile all.
Examples:
Request: .../appName/login --> return login view.
Request: .../appName/home --> return home view.
Request: .../appName/fahsjhgasghdjfg --> return admin view.
Request: .../appName/dasjdha/fhfashjfs --> return admin view.
Request: .../appName/resources/css/one.css --> return admin view.
If I remove the AdminController and create a new controller with name ExamplefasjkasController it happens the same.
Code:
Controllers:
#Controller
public class AdminController {
#RequestMapping(name = "/panel")
public ModelAndView adminPanel() {
return new ModelAndView("admin");
}
}
#Controller
public class UserController extends GenericController {
#Autowired
private IUserService userService;
#RequestMapping(value = { "/", "/home" })
public ModelAndView home(HttpServletRequest request, Principal principal) {
ModelAndView model = new ModelAndView(Name.VIEW_HOME);
model.setViewName(Name.VIEW_HOME);
return model;
}
#RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(HttpServletRequest request,
#RequestParam(value = Name.PARAM_ERROR, required = false) String error,
#RequestParam(value = Name.PARAM_LOGOUT, required = false) String logout) {
ModelAndView model = new ModelAndView();
model.setViewName(Name.VIEW_LOGIN);
if (error != null) {
addError(model, getErrorMessage(request, "SPRING_SECURITY_LAST_EXCEPTION"));
}
if (logout != null) {
addInfo(model, Message.INFO_USER_LOGOUT);
}
return model;
}
}
In the AdminController I try to get Request with
#Controller
#RequestMapping(name = "/admin"}
public class AdminController {
#RequestMapping(name = "/panel")
...
}
But I get an exception:
javax.servlet.ServletException: No adapter for handler [com.base.controller.AdminController#3f114da7]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler
XMLs:
web.xml
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/spring-mvc-dispatcher.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</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/spring/spring-database.xml,
/WEB-INF/spring/spring-security.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
spring-mvc-dispatcher.xml
<mvc:resources mapping="/web_resources/bower_components/**" location="classpath:/web_resources/bower_components/" />
<mvc:resources mapping="/web_resources/layouts/**" location="classpath:/web_resources/layouts/" />
<mvc:resources mapping="/web_resources/elements/**" location="classpath:/web_resources/elements/" />
<context:component-scan base-package="com.base.*" />
<mvc:annotation-driven />
<context:annotation-config />
<import resource="spring-messages.xml" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
I could share the entire application if someone request.
Thanks in advance and best regards.
You have to fix your request mapping as below:
#Controller
#RequestMapping(value = "/admin"}
public class AdminController {
#RequestMapping(value = "/panel")
...
}

Spring MVC mapping not working: PageNotFound

When i visit localhost:8080/home - i get:
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/home] in DispatcherServlet with name 'appServlet'
When i visit localhost:8080/ or localhost:8080/index all look ok.
Why one path works, and another don't?
And thing, that confuse me: localhost:8080/homepage.html - return me my home view.
So my project here: https://github.com/IRus/jMusic
my web.xml
<!-- Base servlet handles all requests to the application. -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
servlet-context.xml - i using tiles
<annotation-driven/>
<resources mapping="/resources/**" location="/resources/"/>
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/"/>
<beans:property name="suffix" value=".jsp"/>
<beans:property name="order" value="1" />
</beans:bean>
<beans:import resource="controllers.xml"/>
<beans:import resource="tiles.xml" />
<beans:import resource="i18n.xml"/>
<beans:import resource="themes.xml"/>
tiles.xml
<bean id="tilesviewResolver" class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">
<property name="order" value="0"/>
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/views/tiles-definitions.xml</value>
</list>
</property>
</bean>
ErrorController
#Controller
public class ErrorController {
#RequestMapping("/403")
public String error403() {
return "403";
}
#RequestMapping("/404")
public String error404() {
return "404";
}
}
UserController
#Controller
public class UserController {
#Autowired
private UserService userService;
#RequestMapping("/")
public String index() {
return "redirect:/index";
}
#RequestMapping("/home")
public String home() {
return "home";
}
#RequestMapping("/login")
public String login() {
return "login";
}
#RequestMapping("/index")
public String listUsers(Map<String, Object> map) {
map.put("user", new User());
map.put("userList", userService.listUser());
return "user";
}
#RequestMapping(value = "/add", method = RequestMethod.POST)
public String addUser(#ModelAttribute("user") User user,
BindingResult result) {
userService.addUser(user);
return "redirect:/index";
}
#RequestMapping("/delete/{idUser}")
public String deleteUser(#PathVariable("idUser") Long idUser) {
userService.removeUser(idUser);
return "redirect:/index";
}
}
Logs
Here: https://gist.github.com/IRus/2ac97c66070001247011
Interested moment in logs:
Mapped URL path [/homepage.html] into handler 'userController'
I added, and delete thats #RequestMapping in controller, but it still alive
I work in Idea 12.0.4
The problem was in the cache/IDE.
Class file is not updated when I deploy project.
First time i get trouble like this. Just restart IDE and clean tomcat webapps folder(delete my project files from here).
Now everything works as expected.

Resources