2017-03-15 24 views
-1

這是控制器包下我的家控制器Spring MVC的發送郵件遇到錯誤(檢查圖像)

EmailController.java

package org.convey.example.controller; 
import org.convey.example.email.EmailSender; 
import org.convey.example.model.EmailMessage; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bi[enter image description here][1]nd.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 
import java.util.Map; 


/** 
* $LastChangedDate: $ 
* $LastChangedBy: $ 
* $LastChangedRevision: $ 
*/ 
@Controller 

public class EmailController { 

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("emailConfiguration.xml"); 
     EmailSender emailSender=(EmailSender)context.getBean("emailSenderBean"); 


     final static Logger logger = LoggerFactory.getLogger(EmailController.class); 


     @RequestMapping(value="/emailForm",method= RequestMethod.GET) 
     public ModelAndView displayEmailForm(Map<String, Object> map) 
     { 

      logger.debug(" setting up the Email form "); 

      ModelAndView view=new ModelAndView("EmailFormView"); 

      //setting up the required parameter value in the request scope for CommandName parameter 
      map.put("email", new EmailMessage()); 

      return view; 

     } 



     @RequestMapping(value="/sendEmail",method= RequestMethod.POST) 
     public ModelAndView sendEmailUsingGmail(@ModelAttribute("email")EmailMessage email){ 


      logger.debug(" ********************* ready to send the email **********************"); 
      logger.debug(" receiver email address [{}]", email.getReceiverEmailAddress()); 
      logger.debug(" email subject [{}]", email.getSubject()); 
      logger.debug(" email body [{}]", email.getMessageBody()); 

      ModelAndView view=new ModelAndView("EmailView"); 

      view.addObject("emailAddress",email.getReceiverEmailAddress()); 
      emailSender.sendEmail(email); 
      logger.debug(" ********************* email was sent **********************"); 

      return view; 

    } 
} 

這是我的email包

EmailSender下第二類。 java

package org.convey.example.email; 

import org.convey.example.model.EmailMessage; 
import org.springframework.mail.MailSender; 
import org.springframework.mail.SimpleMailMessage; 

/** 
* $LastChangedDate: $ 
* $LastChangedBy: $ 
* $LastChangedRevision: $ 
*/ 

public class EmailSender { 

    private MailSender mailSender; 

     public void setMailSender(MailSender mailSender) { 
      this.mailSender = mailSender; 
     } 


     public void sendEmail(EmailMessage emailMessage){ 

      SimpleMailMessage message = new SimpleMailMessage(); 

      message.setTo(emailMessage.getReceiverEmailAddress()); 
      message.setSubject(emailMessage.getSubject()); 
      message.setText(emailMessage.getMessageBody()); 
      //sending the message 
      mailSender.send(message); 

    } 

} 

這是我的第三堂課下的型號包

EmailMessage.java

package org.convey.example.model; 

import org.springframework.stereotype.Component; 

/** 
* $LastChangedDate: $ 
* $LastChangedBy: $ 
* $LastChangedRevision: $ 
*/ 
@Component 
public class EmailMessage { 


    private String receiverEmailAddress; 
     private String subject; 
     private String messageBody; 


     public void setMessageBody(String messageBody){ 

      this.messageBody=messageBody; 
     } 

     public String getMessageBody(){ 

      return this.messageBody; 
     } 

     public void setReceiverEmailAddress(String receiverEmailAddress){ 

      this.receiverEmailAddress=receiverEmailAddress; 
     } 

     public String getReceiverEmailAddress(){ 

      return this.receiverEmailAddress; 
     } 


     public void setSubject(String subject) { 

      this.subject=subject; 
     } 

     public String getSubject(){ 

      return this.subject; 
    } 
} 

這是在該電子郵件的形式創建

EmailFormView.jsp我EmailFormView.jsp頁

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ page session="false" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 
<html> 
<head> 
    <title>Email Form</title> 
</head> 
<body> 
<form:form commandName="email" method="POST" action="sendEmail"> 

    <p>Email Form </p> 
    <br/><br/> 

    Receiver Email 
    <form:input path="receiverEmailAddress"/> 
    <br/><br/> 

    Subject 
    <form:input path="subject"/> 
    <br/><br/> 

    Message Body 
    <form:input path="messageBody"/> 
    <br/><br/> 

    </br> 
    <input type="submit" value="Send Email" /> 

</form:form> 
</body> 
</html> 

這是我EmailView.jsp頁面顯示給發件人的電子郵件已成功發送的消息

EmailView.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!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=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<h2>Email was successfully sent to the ${emailAddress}</h2> 
</body> 
</html> 

這是我applicationContext.xml文件

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <!-- the below bean is configured to define external property file for defining property values for the application. 
      the following bean is used for configuring a resource bundle for this application based on different locales--> 
    <!--<bean id="messageSource"--> 
    <!--class="org.springframework.context.support.ReloadableResourceBundleMessageSource">--> 
    <!--<property name="basename" value="classpath:properties/messages" />--> 
    <!--<property name="defaultEncoding" value="UTF-8"/>--> 
    <!--</bean>--> 


</beans> 

這是我的電子郵件-servlet.xml文件

電子郵件servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <!-- this will scan the @Component models defined in the given package (including sub packages) and it will automatically create instances. 
     if you have used @Component annotation, it is not required to define the beans in the XML configuration files --> 
    <!-- Discover POJO @Components --> 
    <!--<context:component-scan base-package="org.convey.registration" />--> 

    <import resource="applicationContext.xml"/> 

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


    <bean id="handlerMappingC" 
      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
     <property 
       name="mappings"> 
      <props> 
       <prop 
         key="/emailForm"> 
        emailController 
       </prop> 

       <prop 
         key="/sendEmail"> 
        emailController 
       </prop> 

      </props> 
     </property> 
    </bean> 


    <bean id="emailController" class="org.convey.example.controller.EmailController"/> 

</beans> 

This is my web.xml file 

web.xml 

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>log4jConfigLocation</param-name><param-value>/resources/log4j.xml</param-value> 
    </context-param> 


    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>email</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 

     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>email</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

這是我的index.jsp頁面

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!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=ISO-8859-1"> 
<title>Bhushan</title> 
</head> 
<body> 
<h2> <a href="/email/emailForm">Click here to Load Email Form</a></h2> 
</body> 
</html> 

這是我的pom.xml文件

的pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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.spring1</groupId> 
    <artifactId>mypackage</artifactId> 
    <name>SpringMvcEmail</name> 
    <packaging>war</packaging> 
    <version>1.0.0-BUILD-SNAPSHOT</version> 
    <properties> 
     <java-version>1.6</java-version> 
     <org.springframework-version>3.1.1.RELEASE</org.springframework-version> 
     <org.aspectj-version>1.6.10</org.aspectj-version> 
     <org.slf4j-version>1.6.6</org.slf4j-version> 
    </properties> 
    <dependencies> 
     <!-- Spring --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>${org.springframework-version}</version> 
      <exclusions> 
       <!-- Exclude Commons Logging in favor of SLF4j --> 
       <exclusion> 
        <groupId>commons-logging</groupId> 
        <artifactId>commons-logging</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${org.springframework-version}</version> 
     </dependency> 

     <!-- AspectJ --> 
     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjrt</artifactId> 
      <version>${org.aspectj-version}</version> 
     </dependency> 

     <!-- Logging --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>${org.slf4j-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>jcl-over-slf4j</artifactId> 
      <version>${org.slf4j-version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>${org.slf4j-version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>1.2.15</version> 
      <exclusions> 
       <exclusion> 
        <groupId>javax.mail</groupId> 
        <artifactId>mail</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>javax.jms</groupId> 
        <artifactId>jms</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>com.sun.jdmk</groupId> 
        <artifactId>jmxtools</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>com.sun.jmx</groupId> 
        <artifactId>jmxri</artifactId> 
       </exclusion> 
      </exclusions> 
      <scope>runtime</scope> 
     </dependency> 
    <!-- Java Mail API --> 
     <dependency> 
      <groupId>javax.mail</groupId> 
      <artifactId>mail</artifactId> 
      <version>1.4.3</version> 
     </dependency> 

     <!-- @Inject --> 
     <dependency> 
      <groupId>javax.inject</groupId> 
      <artifactId>javax.inject</artifactId> 
      <version>1</version> 
     </dependency> 
     <dependency> 
      <groupId>taglibs</groupId> 
      <artifactId>standard</artifactId> 
      <version>1.1.2</version> 
</dependency>  
     <!-- Servlet --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>jsp-api</artifactId> 
      <version>2.1</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 

     <!-- Test --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.7</version> 
      <scope>test</scope> 
     </dependency>   
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-eclipse-plugin</artifactId> 
       <version>2.9</version> 
       <configuration> 
        <additionalProjectnatures> 
         <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> 
        </additionalProjectnatures> 
        <additionalBuildcommands> 
         <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> 
        </additionalBuildcommands> 
        <downloadSources>true</downloadSources> 
        <downloadJavadocs>true</downloadJavadocs> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.5.1</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
        <compilerArgument>-Xlint:all</compilerArgument> 
        <showWarnings>true</showWarnings> 
        <showDeprecation>true</showDeprecation> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>exec-maven-plugin</artifactId> 
       <version>1.2.1</version> 
       <configuration> 
        <mainClass>org.test.int1.Main</mainClass> 
       </configuration> 
      </plugin> 
     </plugins> 
     <finalName>email</finalName> 
    </build> 

</project> 

我的問題是, ,我想風格給我的消息體如字體,粗體,文字大小如何改變我的程序?

+1

錯誤信息在哪裏? – Jens

+1

請發佈您的錯誤堆棧追蹤。目前這個話題還不清楚。 – Reborn

+0

詢問關於您的編輯的另一個問題。這與你的第一個問題沒有關係,造型你的消息體不是Spring的問題。這是一個CSS問題。 – akuma8

回答

0

請不要插入圖片來顯示您的錯誤。檢查IDE控制檯中的堆棧跟蹤並將其複製粘貼到此處。

除此之外,你有一個404錯誤,這意味着請求的資源不可用,因爲你發送了一個錯誤的URL。您發送此email/emailForm並且您的控制器中有任何處理程序來處理您的請求。在你的情況下,你應該發送/emailForm

編輯

我錯過了你做,這是一個錯誤,初學者往往使誤差。 我再次查看了您的圖片,並在地址欄中顯示:localhost:8080/email/emailForm。如果你在訪問stackoverflow網站時看看你的瀏覽器地址欄,你有這樣的:stackoverflow.com/...這裏stackoverflow.com是域,換句話說就是應用程序的根。

就你而言,該域缺失,你應該有像localhost:8080/yourDomain/email/emailForm

您有幾種方法可以添加它的網址:

一)它添加手動

B)使用JSTL標籤這樣<a href="<c:url value="/email/emailForm"/>">....</a>這是我最喜歡的解決方案,因爲<c:url >標記添加的自動根應用。

C)Get請求上下文路徑:<a href="${request.contextPath}/email/emailForm">....</a>

d)寫的這個<a href="email/emailForm">.....</a>代替<a href="/email/emailForm">...</a>。實際上斜槓改變了東西,/email/emailFormemail/emailForm不一樣。我讓你做測試,你會感到驚訝。

這些應該可以幫到你。

除此之外,請修改您的應用程序的配置,在SpringMVC應用程序中,您有兩個主要上下文:根上下文和servlet上下文。
在你的配置中你只有一個,我非常喜歡Spring讓你這樣做。這可能會導致問題,如果你想添加其他功能,如管理應用程序中的安全性。

+0

在你的堆棧跟蹤中沒有任何異常。您是否更改了網址? – akuma8

+0

不,我不是在URL改變 –