为什么JSP页面无法在另一个JSP页面中传递值?

时间:2016-12-17 03:59:40

标签: spring jsp spring-mvc

我想在JSP上开发一个简单的注册表单页面。我是春天的新人。我试图这样做,如果我把值放在JSP页面上,那么另一个JSP页面就会获得值并显示它们。这是我的LoginController.java:

public class LoginController extends HttpServlet{


     @RequestMapping(value="/user",method = RequestMethod.GET)
      /*  public String viewRegistration(Map<String, Object> model) {
            User userForm = new User();    
            model.put("userForm", userForm);  
            return "index";
        }*/

       public ModelAndView student() {

          return new ModelAndView("user", "user", new User());
       }

        @RequestMapping(value="/Login",method = RequestMethod.POST)
        public String  processCredentials(@RequestParam("fullName")String fullName,@RequestParam("fName")String fName
                                               ,@RequestParam("mName")String mName,@RequestParam("nationality")String nationality
                                               ,@RequestParam("occupation")String occupation,@RequestParam("address")String address
                                               ,@RequestParam("about")String about , HttpServletRequest request, 
                                                 HttpServletResponse response, Object command , @ModelAttribute("user") User user,
                                                 ModelMap model) {
            String message = "Invalid credentials";
            /*if(userName.equals("admin") && password.equals("123")) {

                    message = "Welcome " + userName + "!!";

            }*/
        /*  Map<String,String>data = new HashMap<String,String>();
             data.put("fullName",fullName);
             data.put("fName",fName);
             data.put("mName",mName);
             data.put("nationality",nationality);
             data.put("occupation",occupation);
             data.put("address",address);
             data.put("about",about);
             */
             request.getAttribute(fullName);
             request.getAttribute(fName);
             request.getAttribute(mName);
             request.getAttribute(nationality);
             request.getAttribute(occupation);
             request.getAttribute(address);
             request.getAttribute(about);
             model.addAttribute(user.getFullName());
             model.addAttribute(user.getfName());
             model.addAttribute(user.getmName());
             model.addAttribute(user.getfName());
             model.addAttribute(user.getNationality());
             model.addAttribute( user.getAddress());
             model.addAttribute(user.getAbout());
             model.addAttribute(user.getOccupation());

             //User user2 = new User();
                model.addAttribute("user", user);
                return "welcome";

             // return new ModelAndView("welcome" , "request",request);

        }

我的index.jsp是:

<!-- <html> 

<head> <title>Spring MVC login example.</title> 
</head>   
<body> 
<form action="login" method="post"> 
UserName:<input type="text" name="userName"/> <br/><br/> 
Password:<input type="password" name="password"/> <br/><br/> 
<input type="submit" value="login"/> 
</form> 
</body> 
</html>  -->
<%-- 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring Login Form</title>
</head>
<body>
    <form action="login" method="POST">
        <div align="center">
            <table>
                <tr>
                    <td>User Name</td>
                    <td><input type="text" name="userName" /></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><input type="password" name="password" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Submit" /></td>
                </tr>
            </table>
            <c:choose>
            <c:when test="${request.getAttribute("message")!='null'"}">
            <%=  request.getAttribute("message")  %>
            </c:when>
            </c:choose>
        </div>
    </form>
</body>
</html>


 --%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
td{ 
    clear: left; 
    width: 100px; 
    text-align: right; 
    margin-right: 10px; 
    font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:14px; 
    color:red;
    } 

    body {
    background-color:#b6e1a0;

} 
</style>
<title>Spring Login Form</title>
</head>
<body>
    <form action="/LoginSystemPAL/Login" method="POST" commandName="user" >
        <div align="center">
            <table>
                <tr>
                    <td>Full Name</td>
                    <td><input type="text" name="fullName" path="fullName" /></td>
                </tr>
                <tr>
                    </br></br></td>
                </tr>
                 <tr>
                    <td>Father's Name</td>
                    <td><input type="text" name="fName" path="fName"/></td>
                </tr>
                <tr>
                    </br></br></td>
                </tr>
                 <tr>
                    <td>Mother's Name</td>
                    <td><input type="text" name="mName" path="mName" /></td>
                </tr>
                <tr>
                    </br></br></td>
                </tr>
                <tr>
                    <td>Nationality</td>
                    <td><input type="text" name="nationality" path="nationality"/></td>
                </tr>
                <tr>
                    </br></br></td>
                </tr>
                 <tr>
                    <td>Occupation</td>
                    <td><input type="text" name="occupation" path="occupation"/></td>
                </tr>
                <tr>
                    </br></br></td>
                </tr>
                  <tr>
                    <td>Address</td>
                    <td><input type="text" name="address" path="address"/></td>
                </tr>
                <tr>
                    </br></br></td>
                </tr>
                 <tr>
                    <td>About</td>
                    <td><input type="text" name="about" path="about" /></td>
                </tr>
                <tr>
                    </br></br></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Submit"  /></td>
                </tr>
            </table>

        </div>
    </form>
</body>
</html>

我的welcome.jsp是:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
td{ 
    clear: left; 
    width: 100px; 
    text-align: right; 
    margin-right: 10px; 
    font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:14px; 
    color:red;
    } 

    body {
    background-color:#b6e1a0;

} 
</style>
<title>Spring Login Form</title>
</head>
<body>

        <div align="center">
            <table>
                <tr>
                    <td>Full Name</td>
                   <td> <%=request.getAttribute("user.fullName")%></td> 
                <%--    <td> ${fullName} </td> --%>
                </tr>
                <tr>
                    </br></br></td>
                </tr>
                 <tr>
                    <td>Father's Name</td>
                    <%-- <td><%=request.getAttribute("fName")%></td> --%>
                     <td> ${user.fName} </td>
                </tr>
                <tr>
                    </br></br></td>
                </tr>
                 <tr>
                    <td>Mother's Name</td>
                  <%--   <td><%=request.getAttribute("mName")%></td> --%>
                     <td> ${mName} </td>
                </tr>
                <tr>
                    </br></br></td>
                </tr>
                <tr>
                    <td>Nationality</td>
                    <%-- <td><%=request.getAttribute("nationality")%></td> --%>
                      <td> ${nationality} </td>
                </tr>
                <tr>
                    </br></br></td>
                </tr>
                 <tr>
                    <td>Occupation</td>
                   <%--  <td><%=request.getAttribute("occupation")%></td> --%>
                    <td> ${occupation} </td>
                </tr>
                <tr>
                    </br></br></td>
                </tr>
                  <tr>
                    <td>Address</td>
                   <%--  <td><%=request.getAttribute("address")%></td> --%>
                     <td> ${address} </td>
                </tr>
                <tr>
                    </br></br></td>
                </tr>
                 <tr>
                    <td>About</td>
                    <%-- <td><%=request.getAttribute("about")%></td> --%>
                    <td> ${about} </td>
                </tr>
                <tr>
                    </br></br></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Submit" /></td>
                </tr>
            </table>

        </div>

</body>
</html>

我的pom.xml是:

    <project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.pran.pal</groupId>
      <artifactId>LoginSystemPAL</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>LoginSystemPAL Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
       <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.4.RELEASE</version>
       </dependency>
       <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.4.3</version>
       </dependency>
       <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.0.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.1.0.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
      </dependencies>
      <build>
        <finalName>LoginSystemPAL</finalName>
      </build>
    </project>


The web.xml is:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

  <display-name>Archetype Created Web Application</display-name>
  <servlet> 
  <servlet-name>dispatcher</servlet-name> 
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 

  <load-on-startup>1</load-on-startup> 
  </servlet>   
  <servlet-mapping> 
  <servlet-name>dispatcher</servlet-name> 
  <url-pattern>/</url-pattern>
   </servlet-mapping>   
   <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
</servlet-mapping>

</web-app> 
 <!-- <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
    <display-name>Spring MVC Application</display-name>

  <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app> -->

welcomre.jsp没有任何价值。显示如下: this is the welcomee.jsp

康索尔没有错误。我现在可以做什么?

3 个答案:

答案 0 :(得分:0)

尝试使用<c:out value="${modelAttribute}" />代替${modelAttribute},看看它是否有效。

如果你尚未包含它,请包含jstl的taglib。

<%@ taglib prefix="c" 
       uri="http://java.sun.com/jsp/jstl/core" %>

答案 1 :(得分:0)

我建议您使用flash attribute facility of Spring MVC在控制器方法中添加RedirectAttributes,如下所示,

@RequestMapping(value="/Login",method = RequestMethod.POST)
        public String processCredentials(@RequestParam("fullName")String fullName,
                @RequestParam("fName")String fName,
                @RequestParam("mName")String lName,
                @RequestParam("nationality")String nationality,
                @RequestParam("occupation")String occupation,
                @RequestParam("address")String address,
                @RequestParam("about")String about,
                HttpServletRequest request,
                HttpServletResponse response,
                Object command,
                @ModelAttribute("user") User user,
                ModelMap model,
                RedirectAttributes redir) {



             redir.addFlashAttribute("user", user);

             return "redirect:/welcome";
        }

答案 2 :(得分:0)

首先简化您的控制器:

/**
 * Spring MVC framework will automatically bind form fields with name X to corresponding
 * fields name setX() in specified Model attribute.
 *
 */
@RequestMapping(value="/Login",method = RequestMethod.POST)
public String  processCredentials(@ModelAttribute("user") User user) {

    //make Model Attribute available to next view
    return new ModelAndView("welcome", user);
}

其次,不要在JSP页面中使用scriptlet。

How to avoid Java code in JSP files?

在JSP中,您可以使用JSP表达式语言或JSTL标记:EL在这里很好。模式看起来像:

 <tr>
    <td> ${user.about} </td>
</tr>