使用spring在网页上显示数据

时间:2014-02-03 14:13:43

标签: spring

伙计我想在网页上显示数据,为此我写了一些控制器代码,将详细信息传递给网页,但问题在这里我不知道如何传递它。

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dz.hrportal.beans.EmpRegistrationForm;
import com.dz.hrportal.constants.GlobalConstants;
import com.dz.hrportal.worker.ProfileWorker;
@Controller
public class ProfileController 
{

    static Logger log = Logger.getLogger(ProfileController.class.getName());


    @RequestMapping(value = "/viewProfile" ,method = RequestMethod.GET)
    public String processForm(@ModelAttribute("viewProfile")EmpRegistrationForm profileForm, BindingResult result , HttpServletRequest request, HttpServletResponse response, ModelMap model)
    {

        if(result.hasErrors()){
            return GlobalConstants.ERRORPAGE;
        }

        ProfileWorker worker=new ProfileWorker();
        boolean status=worker.getUserDetails(profileForm);
        if(status)
        {
            /*if("Admin".equalsIgnoreCase(loginForm.getUserType())){
                return GlobalConstants.HOME_PAGE;
            }else{
                return GlobalConstants.EMP_HOME_PAGE;
            }*/
            return GlobalConstants.HOME_PAGE;
        }
        else
        {
            return GlobalConstants.LOGIN_PAGE;
        }
    }

}

这里是与数据库交互的worker.java:

public class ProfileWorker{

    private Connection con;
    private ResultSet rs=null;
    private String query=null;
        public boolean getUserDetails(EmpRegistrationForm profileForm){

            try{
                con=DBConnection.getConnection();


//                  query = "select emp_id,emp_name,designation,location,department,DOJ from register where user_id=?";
                query = "select * from register where user_id=?";


                PreparedStatement pstmt=con.prepareStatement(query);
                //pstmt.setString(1, loginForm.getUserName());
                //pstmt.setString(2, loginForm.getPassword());
                //int i = stmt.executeUpdate("insert into hrlogin values('oooo','pppp')");
                rs= pstmt.executeQuery();
                 if(rs.next()){
                     return true;
                 }

            }catch(Exception e)
                {
                e.printStackTrace();
                    System.out.println(e);
                }
            return false;
        }
}

我想在profileVeiw页面上显示数据:

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<html>
<head>
<title>Spring Example</title>
    <link rel="stylesheet" href="CSS/style.css">
    <link rel="stylesheet" href="CSS/Master.css">
</head>
<body>
<span style="float: right">Choose your language
    <a href="?lang=en">English</a>
    <a href="?lang=de">Marathi</a>
</span>
<%@include file="layout.jsp"%>
<div id="viewProfile">
    <form:form method="POST" action="login.do" commandName="viewProfile" modelAttribute="viewProfile">
    </form:form>
    </div>
    <div id="footer"><%@include file="/jsp/footer.jsp"%></div>
    </body>
    </html>

1 个答案:

答案 0 :(得分:1)

您可以使用模型的addAttribute方法。 对于exmaple ..

model.addAttribute("result", "success");

然后你可以在jsp页面上使用它,就像这样

${result}