以下是spring mvc程序。问题是每当我填写mailid和密码而不是主页时,即使数据库中存在mailid和密码,也会弹出重新登录页面。 它有控制器,daos,用户和几个视图(home,relogin,registered),tiles.xml,ds-servlet.xml,web.xml。 在authenticate方法home和relogin存在时,尽管mailid和密码存在于数据库中,但始终重新显示。
usercontroller.java
package controllers;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
import daos.UserDao;
import models.User;
@Controller
public class Usercontroller {
@Autowired
UserDao dao;
@Autowired
HttpSession session;
@RequestMapping("/regform.spr")
public String register(
@ModelAttribute User user) throws Exception
{
dao.save(user);
return "registered";
}
@RequestMapping("/login.spr")
public String authenticate(
@ModelAttribute User user)
{
String str="relogin";
try
{
if(dao.findUser(user))
{
str="home";
session.setAttribute("user",user);
}
}catch(Exception e)
{
System.out.println(e);
}
return str;
}
保存方法工作正常,但问题来自finduser
userdao.java
package daos;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.DataSource;
import models.User;
public class UserDao {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void save(User user) throws Exception
{
Connection con=dataSource.getConnection();
PreparedStatement stmt=con.prepareStatement("insert into UserMaster (name,mailId,password) values(?,?,?)");
stmt.setString(1, user.getName());
stmt.setString(2, user.getMailId());
stmt.setString(3, user.getPassword());
stmt.executeUpdate();
stmt.close();
}
public boolean findUser(User user) throws Exception
{
boolean flag=false;
//Connection is obtained
Connection con=dataSource.getConnection();
//P.S. is created for the insert query
PreparedStatement stmt=con.prepareStatement(
"select id,name from UserMaster where mailId=? and password=?");
//param values are set in the query
stmt.setString(1, user.getMailId());
stmt.setString(2, user.getPassword());
//query is executed.
ResultSet rset=stmt.executeQuery();
if(rset.next())
{
flag=true;
//id & name are loaded into the object
user.setId(rset.getInt(1));
user.setName(rset.getString(2));
}
//connection is closed.
con.close();
return flag;
}
}
这是家庭和重新上传的观点
home.jsp
<div class="jumbotron">
<h3>welcome java-ians!! lets get started.</h3>
<p>Welcome, <b>${user.name}</b></p>
<h4>click to move forward!!</h4>
<button class="btn btn-info">forward</button>
</div>"
relogin.jsp
<div >
<h1>emailid or password is incorrect</h1>
<jsp:include page="login.jsp"></jsp:include>
</div>
tiles.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="registered" template="/views/registered.jsp">
<put-attribute name="title" value="registered"/>
<put-attribute name="contents" value="/views/registered.jsp"/>
</definition>
<definition name="home" template="/views/home.jsp">
<put-attribute name="contents" value="/views/home.jsp"/
</definition>
<definition name="relogin" template="/views/relogin.jsp">
<put-attribute name="title" value="relogin"/>
<put-attribute name="contents" value="/views/relogin.jsp"/>
</definition>
</tiles-definitions>