2014-08-30 116 views
0

我是Spring MVC的新手。我最終沒有查看渲染應有的價值,但渲染$ {}

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0"> 
    <display-name>SpringMvcFormExample</display-name> 
    <context-param> 
     <param-name>contextClass</param-name> 
     <param-value> 
      org.springframework.web.context.support.AnnotationConfigWebApplicationContext 
     </param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

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

    <session-config> 
     <session-timeout>30</session-timeout> 
    </session-config> 
</web-app> 

SpringDispatcher-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" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:component-scan base-package="net.codejava.*" /> 

    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" 
      value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix"> 

      <value>/WEB-INF/views/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 

</beans> 

RegistrationController.jsp

package net.codejava.spring.controller; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Map; 

import net.codejava.spring.model.User; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
@RequestMapping(value = "/register") 
public class RegisterController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String initForm(Model model) { 
     User user = new User();  
     user.setUsername("xyz"); 
     model.addAttribute("user",user); 

     System.out.println("up model "+ model.asMap().toString()); 

     return "Registration"; 
    } 

    @RequestMapping(method = RequestMethod.POST) 
    public String submitForm(Model model,@ModelAttribute User user) { 
     System.out.println("user : "+ user.getUsername()); 
     model.addAttribute(user); 

     // implement your own registration logic here... 


     System.out.println("model" +model.asMap().toString()); 

     return "RegistrationSuccess"; 
    } 
} 

註冊.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/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>Registration</title> 
</head> 
<body> 
    <div align="center"> 
    <h1>reached here</h1> 
     <form:form action="register" method="POST" commandName="user"> 
      <table border="0"> 
       <tr> 
        <td colspan="2" align="center"><h2>Spring MVC Form Demo - Registration</h2></td> 
       </tr> 
       <tr> 
        <td>User Name:</td> 
        <td><form:input path="username" /></td> 
       </tr> 
       <tr> 
        <td>Password:</td> 
        <td><form:password path="password" /></td> 
       </tr> 
       <tr> 
        <td>E-mail:</td> 
        <td><form:input path="email" /></td> 
       </tr> 
       <tr> 
        <td>Birthday (mm/dd/yyyy):</td> 
        <td><form:input path="birthDate" /></td> 
       </tr> 
<!--    <tr> --> 
<!--     <td>Profession:</td> --> 
<%--     <td><form:select path="profession" items="${professionList}" /></td> --%> 
<!--    </tr> --> 
       <tr> 
        <td colspan="2" align="center"><input type="submit" value="Register" /></td> 
       </tr> 
      </table> 
     </form:form> 
<h1>reached here</h1> 
    </div> 
</body> 
</html> 

RegistrationSucess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/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>Registration Success</title> 
</head> 
<body> 
    <div align="center"> 

     <table border="0"> 
      <tr> 
       <td colspan="2" align="center"><h2>Registration Succeeded!</h2></td> 
      </tr> 
      <tr> 
       <td colspan="2" align="center"> 
        <h3>Thank you for registering! Here's the review of your details:</h3> 
       </td> 
      </tr> 
      <tr> 
       <td>User Name:</td> 
       <td>${user.username}</td> 
      </tr> 
      <tr> 
       <td>E-mail:</td> 
       <td>${email}</td> 
      </tr> 
      <tr> 
       <td>Birthday:</td> 
       <td>${user.birthDate}</td> 
      </tr> 
      <tr> 
       <td>Profession:</td> 
       <td>${user.profession}</td> 
      </tr> 

     </table> 
    </div> 
</body> 
</html> 

現在最後的RegistrationSucess頁面將顯示有關信息:( 請幫我在哪裏,我在做錯誤的。提前致謝。

RegistrationSucess.jsp Snap

+0

嘗試修復你的web.xml聲明(你是混合2.5和3.0版本)。以此爲例:http://javahowto.blogspot.fr/2010/07/sample-webxml-servlet-30-with-ejb-ref.html。另外,你在哪裏部署應用程序? – 2014-08-30 15:15:55

+0

嗨JB我改變了web.xml到下面,但沒有運氣,仍然得到相同的輸出:( AKumar 2014-08-30 16:47:43

回答

0

不知何故,EL評價正在得到您的設置禁用(我猜的servlet容器的版本不匹配和web.xml中聲明)。嘗試通過它設置爲false添加isELIgnored屬性和檢查

<%@ page isELIgnored="false" %> 
+0

你得到100/100謝謝 – AKumar 2014-08-30 20:20:45

0

這是從樣品中項目(工作)複製的碼。只刪除了其他幾個bean定義。

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

<context:component-scan base-package="com.classifieds" /> 
<context:component-scan base-package="com.classifieds.controller" /> 
<context:component-scan base-package="com.classifieds.service" /> 

<context:annotation-config /> 
<mvc:annotation-driven /> 
<mvc:resources location="/resources/,classpath:/META-INF/" 
    mapping="/resources/**" /> 

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="order" value="2" /> 
    <property name="prefix"> 
     <value>/WEB-INF/pages/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
</bean> 

+0

它不再工作..感謝您的時間 – AKumar 2014-08-30 17:20:00