这是我在控制器包下的家庭控制器
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;
}
}
这是我在电子邮件包
下的第二堂课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>
这是我的email-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>
我的问题是,我想在我的邮件正文中设置样式,如字体,粗体,文字大小我如何更改程序???
答案 0 :(得分:0)
请勿插入图像以显示错误。检查IDE控制台中的堆栈跟踪并将其复制粘贴到此处。
除此之外,您有404错误,这意味着请求的资源不可用,因为您发送了错误的网址。您发送此email/emailForm
并且控制器中有任何处理程序来处理您的请求。在您的情况下,您应该发送/emailForm
。
编辑
我错过了您所犯的错误,这是初学者经常犯的错误。
我再次查看了您的图片,并在地址栏中显示:localhost:8080/email/emailForm
。如果您在访问stackoverflow网站时查看浏览器地址栏,请执行以下操作:stackoverflow.com/...
此处stackoverflow.com
是域名,换句话说就是应用程序的根目录。
在您的情况下,域名丢失,您应该有localhost:8080/yourDomain/email/emailForm
之类的内容。
您可以通过多种方式在网址中添加它:
a)手动添加
b)使用像<a href="<c:url value="/email/emailForm"/>">....</a>
这样的JSTL标记,这是我最喜欢的解决方案,因为<c:url >
标记会自动添加应用程序的根目录。
c)获取请求上下文路径:<a href="${request.contextPath}/email/emailForm">....</a>
d)写下<a href="email/emailForm">.....</a>
而不是<a href="/email/emailForm">...</a>
。事实上,斜杠改变了事物,/email/emailForm
与email/emailForm
不同。我让你做考试,你会感到惊讶。
这些应该对你有帮助。
除此之外,请修改应用程序的配置,在SpringMVC应用程序中,您有两个主要的上下文:根上下文和servlet上下文。
在你的配置中,你只有一个,我非常喜欢Spring,让你这么做。如果要在应用程序中添加管理安全性等其他功能,则可能会出现问题。