我的InternalResourceViewResolver无法正常工作

时间:2015-03-26 06:43:03

标签: spring spring-mvc java-ee spring-jdbc

我正在尝试使用带有Annotation的Spring Framework开发一个小应用程序,但我真的无法识别我犯了InternalResourceViewResolver类没有处理来自.jsp页面的请求的错误。这是我的项目细节。

  

的index.jsp

<jsp:forward page="login.form" ></jsp:forward>
  

弹簧servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="com.nody.spring.controller"></context:component-scan>
    <context:annotation-config></context:annotation-config>
    <!-- Handler Mapping -->
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMappping"></bean>

    <!-- View Resolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix"   value="/"></property>
    <property name="suffix"   value=".jsp"></property>
    </bean>

    <bean id="jt"  class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource"  ref="ds"></property>
    </bean>

    <bean id="ds"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName"  value="oracle.jdbc.driver.OracleDriver"></property>
    <property name="url"  value="jdbc:oracle:thin:@localhost:1521:xe"></property>
    <property name="username"  value="scott"></property>
    <property name="password"  value="tiger"></property>
    </bean>
   </beans>
  

LoginController.java

package com.nody.spring.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {

        private LoginService loginService;

        @Autowired
        public void setLoginService(LoginService loginService) {
            this.loginService = loginService;
        }

        @RequestMapping(value="/login.form", method=RequestMethod.GET)
        public ModelAndView getLoginPage()
        {
            return new ModelAndView("login");
        }

        @RequestMapping(value="check", method=RequestMethod.POST)
        public ModelAndView checkLogin(@RequestParam("uname")String s1,@RequestParam("pword")String s2)
        {
            boolean b=loginService.check(s1,s2);
            if(b)
                return new ModelAndView("success");
            else
                return new ModelAndView("failure");
        }

    }
  

LoginService.java

package com.nody.spring.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class LoginService {

    private JdbcTemplate jt;
    @Autowired
    @Qualifier("jt")
    public void setJt(JdbcTemplate jt)
    {
        this.jt=jt;
    }
    public boolean check(String s1,String s2)
    {
        @SuppressWarnings("deprecation")
        int i=jt.queryForInt("select count(*) from users where uname=? and pword=?",s1,s2);
        if(i==1)
            return true;
        else
            return false;

    }


}
  

罐子清单

aspectj-weaver.jar
atomikos-transactions-api.jar
atomikos-transactions-jta.jar
atomikos-util.jar
commons-logging-api-1.1.1.jar
javax.transaction-3.1.jar
jta.jar
mysql-connector-java-5.1.6.jar
ojdbc14.jar
spring-aop-4.1.4.RELEASE.jar
spring-beans-4.1.4.RELEASE.jar
spring-context-4.1.4.RELEASE.jar
spring-context-support-4.1.4.RELEASE.jar
spring-core-4.1.4.RELEASE.jar
spring-expression-4.1.4.RELEASE.jar
spring-jdbc-4.1.4.RELEASE.jar
spring-tx-4.1.4.RELEASE.jar
transactions-3.7.0.jar
transactions-jdbc-3.7.0.jar
aopalliance.jar
aspectjrt-1.8.5.jar
  

failure.jsp

<form action="check.form" method="POST">
Username:<input type=text name="uname"><br>
Password<input type=password name="pword"><br>
<input type=submit value="SUBMIT">
</form>
  

的success.jsp

<h1>Login Success !!!</h1>
  

failure.jsp

<h1>Login Success !!!</h1>
  

和Oracle表名称是“用户”,有2列'uname'和'pword'

1 个答案:

答案 0 :(得分:0)

我不知道,如果我理解正确,但你在加载.jsp页面时出错了吗?

<!-- View Resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"   value="/"></property>
<property name="suffix"   value=".jsp"></property>
</bean>

看起来你要说春天,在直接目录中寻找.jsp页面(可能是webapp)。你在哪里保存.jsp文件?您应该提供包含。<beans:property name="prefix" value="/WEB-INF/views/" />

等.jsp文件的确切文件夹