Announcement Announcement Module
Collapse
No announcement yet.
JSP not found after redirect Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JSP not found after redirect

    Hi all,

    I am new to Spring MVC. I have been trying to follow MVC Spring tutorial simplification.

    I am having a problem with displaying jsp's, according to provided code I am trying to redirect the page to view page (which exists) after successful creation of account. Even though the logs are saying that the redirect is happening the tomcat is returning:
    Code:
    HTTP Status 404 - /account/WEB-INF/views/account/view.jsp
    What could be the reason of this issue?

    My code:

    web.xml

    Code:
      
    <servlet>
            <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:/spring/app-config.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    app-config.xml:


    Code:
       <context:annotation-config/>
        
        <context:component-scan base-package="com.fex.app"/>
    
        <mvc:annotation-driven/>
    
        <mvc:view-controller path="/" view-name="index"/>
    
        <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    AccountControllerImpl.java

    Code:
    @Controller
    @RequestMapping(value = "/account")
    public class AccountControllerImpl implements AccountController {
    
        @Autowired
        private AccountDao _accountDao;
    
        @RequestMapping(method = RequestMethod.GET)
        public String getCreateForm(Model model) {
            model.addAttribute(new Account());
            return "account/createAccount";
        }
    
        @RequestMapping(value = "/login", method = RequestMethod.GET)
        public String getLoginForm(Model model) {
            model.addAttribute(new Account());
            return "account/login";
        }
    
        @RequestMapping(value = "/account", method = RequestMethod.POST)
        public String create(@Valid Account account, BindingResult result) {
            if (result.hasErrors()){
                return "account/createAccount"; 
            }
            _accountDao.addAccount(account);
            return "redirect:/account/" + account.getId();
        }
    
        @RequestMapping(value = "{id}", method = RequestMethod.GET)
        public String getView(@PathVariable Long id, Model model) {
            Account account = _accountDao.findAccount(id);
            if (account == null) {
                throw new ResourceNotFoundException(id);
            }
            model.addAttribute(account);
            return "account/view";
        }
    
    }
    The log say:
    Code:
    01:01:13,926 DEBUG JstlView:236 - Forwarding to resource [WEB-INF/views/account/view.jsp] in InternalResourceView 'account/view'
    01:01:13,927 DEBUG DispatcherServlet:674 - Successfully completed request
    However tomcat returns:
    Code:
    HTTP Status 404 - /account/WEB-INF/views/account/view.jsp
    Thanks for all answers.

  • #2
    Can you try the following:

    Code:
    <servlet-mapping>
            <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    Note the /*

    Comment


    • #3
      The same issue with /* . But this time I cannot even be redirected to welcome page.

      It seems that my problem is identical with this one: http://stackoverflow.com/questions/4...-are-not-found

      Unfortunately no good recipes found there :/

      Comment


      • #4
        I think one of your core problems is that you need one more tag when mapping the default servlet. Try adding <mvc:default-servlet-handler/> to your Spring config and see if that makes any difference.

        (Basically, what's happening right now is the DispatcherServlet is processing EVERYTHING, but it doesn't know how to process the JSP requests themselves. That tag will let it pass on to Tomcat)

        Also, I'd make sure this is working without the redirect first. Precreate an account and just send GETs to /account/{id} first. Once THAT is working, then give the create method a try.

        Hope this helps
        - Don

        Comment


        • #5
          Thanks guys for the response.

          I have restarted my machine and suddenly it started to work correctly. I didn't change a thing in my code. I think it could be some cache of IDE that caused this issue. I don't have any other explanation.

          Comment

          Working...
          X